• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 15069999656

16 May 2025 01:48PM CUT coverage: 99.797%. First build
15069999656

Pull #249

github

web-flow
Merge 611a431d4 into 9f5dff38a
Pull Request #249: Use faster pointer copying for trivial component types

61 of 63 new or added lines in 6 files covered. (96.83%)

8349 of 8366 relevant lines covered (99.8%)

18499.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.82
/ecs/column.go
1
package ecs
2

3
import (
4
        "reflect"
5
        "unsafe"
6
)
7

8
// column storage for components in an table.
9
type column struct {
10
        pointer    unsafe.Pointer // pointer to the first element
11
        data       reflect.Value  // data buffer
12
        itemSize   uintptr        // memory size of items
13
        target     Entity         // target entity if for a relation component
14
        index      uint32         // index of the column in the containing table
15
        isRelation bool           // whether this column is for a relation component
16
}
17

18
// newColumn creates a new column for a given type and capacity.
19
func newColumn(index uint32, tp reflect.Type, itemSize uintptr, isRelation bool, target Entity, capacity uint32) column {
7,377✔
20
        // TODO: should we use a slice instead of an array here?
7,377✔
21
        data := reflect.New(reflect.ArrayOf(int(capacity), tp)).Elem()
7,377✔
22
        pointer := data.Addr().UnsafePointer()
7,377✔
23

7,377✔
24
        return column{
7,377✔
25
                index:      index,
7,377✔
26
                data:       data,
7,377✔
27
                pointer:    pointer,
7,377✔
28
                itemSize:   itemSize,
7,377✔
29
                isRelation: isRelation,
7,377✔
30
                target:     target,
7,377✔
31
        }
7,377✔
32
}
7,377✔
33

34
// Get returns a pointer to the component at the given index.
35
func (c *column) Get(index uintptr) unsafe.Pointer {
1,547,236✔
36
        return unsafe.Add(c.pointer, index*c.itemSize)
1,547,236✔
37
}
1,547,236✔
38

39
func (c *column) SetLast(other *column, ownLen uint32, count uint32, isTrivial bool) {
463✔
40
        start := ownLen - count
463✔
41
        if isTrivial {
926✔
42
                src := other.Get(0)
463✔
43
                dst := c.Get(uintptr(start))
463✔
44
                copyPtr(src, dst, c.itemSize*uintptr(count))
463✔
45
                return
463✔
46
        }
463✔
47
        copyRange(other.data, c.data, int(start), int(count))
×
48
}
49

50
// Set overwrites the component at the given index.
51
func (c *column) Set(index uint32, src *column, srcIndex int, isTrivial bool) {
4,883✔
52
        if c.itemSize == 0 {
4,939✔
53
                return
56✔
54
        }
56✔
55
        if isTrivial {
7,654✔
56
                comp := src.Get(uintptr(srcIndex))
2,827✔
57
                dst := c.Get(uintptr(index))
2,827✔
58
                copyPtr(comp, dst, c.itemSize)
2,827✔
59
                return
2,827✔
60
        }
2,827✔
61
        copyItem(src.data, c.data, srcIndex, int(index))
2,000✔
62
}
63

64
// Zero resets the memory at the given index.
65
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
506,586✔
66
        if c.itemSize == 0 {
506,815✔
67
                return
229✔
68
        }
229✔
69
        dst := unsafe.Add(c.pointer, index*c.itemSize)
506,357✔
70
        copyPtr(zero, dst, uintptr(c.itemSize))
506,357✔
71
}
72

73
// Zero resets a block of storage in one buffer.
74
func (c *column) ZeroRange(start, len uint32, zero unsafe.Pointer) {
876✔
75
        size := uint32(c.itemSize)
876✔
76
        if size == 0 {
907✔
77
                return
31✔
78
        }
31✔
79
        var i uint32
845✔
80
        for i = range len {
11,934✔
81
                dst := unsafe.Add(c.pointer, (i+start)*size)
11,089✔
82
                copyPtr(zero, dst, c.itemSize)
11,089✔
83
        }
11,089✔
84
}
85

86
func (c *column) Reset(ownLen uint32, zero unsafe.Pointer) {
889✔
87
        if ownLen == 0 {
892✔
88
                return
3✔
89
        }
3✔
90
        if ownLen <= 64 { // A coarse estimate where manually zeroing is faster
1,762✔
91
                c.ZeroRange(0, ownLen, zero)
876✔
92
        } else {
886✔
93
                c.data.SetZero()
10✔
94
        }
10✔
95
}
96

97
// entityColumn storage for entities in an table.
98
type entityColumn struct {
99
        pointer unsafe.Pointer // pointer to the first element
100
        data    reflect.Value  // data buffer
101
}
102

103
// newColumn creates a new column for a given type and capacity.
104
func newEntityColumn(capacity uint32) entityColumn {
2,832✔
105
        // TODO: should we use a slice instead of an array here?
2,832✔
106
        data := reflect.New(reflect.ArrayOf(int(capacity), entityType)).Elem()
2,832✔
107
        pointer := data.Addr().UnsafePointer()
2,832✔
108

2,832✔
109
        return entityColumn{
2,832✔
110
                data:    data,
2,832✔
111
                pointer: pointer,
2,832✔
112
        }
2,832✔
113
}
2,832✔
114

115
// Get returns a pointer to the component at the given index.
116
func (c *entityColumn) Get(index uintptr) unsafe.Pointer {
2,006,552✔
117
        return unsafe.Add(c.pointer, index*entitySize)
2,006,552✔
118
}
2,006,552✔
119

120
func (c *entityColumn) SetLast(other *entityColumn, ownLen uint32, count uint32) {
215✔
121
        start := ownLen - count
215✔
122
        copyRange(other.data, c.data, int(start), int(count))
215✔
123
}
215✔
124

125
// Set overwrites the component at the given index.
126
func (c *entityColumn) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
508,891✔
127
        dst := c.Get(uintptr(index))
508,891✔
128

508,891✔
129
        copyPtr(comp, dst, entitySize)
508,891✔
130
        return dst
508,891✔
131
}
508,891✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc