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

mlange-42 / ark / 13597253646

28 Feb 2025 10:03PM CUT coverage: 96.9% (-0.09%) from 96.988%
13597253646

Pull #82

github

web-flow
Merge 52c6f9ea6 into 5263582b2
Pull Request #82: Unsafe filter and query

234 of 242 new or added lines in 3 files covered. (96.69%)

17 existing lines in 1 file now uncovered.

3626 of 3742 relevant lines covered (96.9%)

50664.41 hits per line

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

96.12
/ecs/column.go
1
package ecs
2

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

8
// column storage for components in an archetype.
9
type column struct {
10
        data       reflect.Value
11
        pointer    unsafe.Pointer
12
        isRelation bool
13
        target     Entity
14
        itemSize   uintptr
15
        len        uint32
16
}
17

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

883✔
24
        return column{
883✔
25
                data:       data,
883✔
26
                pointer:    pointer,
883✔
27
                itemSize:   sizeOf(tp),
883✔
28
                isRelation: isRelation,
883✔
29
                target:     target,
883✔
30
                len:        0,
883✔
31
        }
883✔
32
}
883✔
33

34
// Len returns the number of components in the column.
35
func (c *column) Len() int {
1,019,727✔
36
        return int(c.len)
1,019,727✔
37
}
1,019,727✔
38

39
// Cap returns the current capacity of the column.
40
func (c *column) Cap() int {
1,017,480✔
41
        return c.data.Cap()
1,017,480✔
42
}
1,017,480✔
43

44
// Get returns a pointer to the component at the given index.
45
func (c *column) Get(index uintptr) unsafe.Pointer {
5,131,955✔
46
        return unsafe.Add(c.pointer, index*c.itemSize)
5,131,955✔
47
}
5,131,955✔
48

49
// Add adds a component to the column.
50
func (c *column) Add(comp unsafe.Pointer) (unsafe.Pointer, uint32) {
506,960✔
51
        c.Extend(1)
506,960✔
52
        c.len++
506,960✔
53
        return c.Set(c.len-1, comp), c.len - 1
506,960✔
54
}
506,960✔
55

56
// Alloc allocates memory for the given number of components.
57
func (c *column) Alloc(n uint32) {
510,514✔
58
        c.Extend(n)
510,514✔
59
        c.len += n
510,514✔
60
}
510,514✔
61

62
func (c *column) AddAll(other *column) {
3✔
63
        oldLen := c.len
3✔
64
        c.Alloc(other.len)
3✔
65
        src := other.Get(0)
3✔
66
        dst := c.Get(uintptr(oldLen))
3✔
67
        copyPtr(src, dst, c.itemSize*uintptr(other.len))
3✔
68
}
3✔
69

70
// Set overwrites the component at the given index.
71
func (c *column) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
1,018,617✔
72
        dst := c.Get(uintptr(index))
1,018,617✔
73
        if c.itemSize == 0 {
1,019,072✔
74
                return dst
455✔
75
        }
455✔
76

77
        copyPtr(comp, dst, uintptr(c.itemSize))
1,018,162✔
78
        return dst
1,018,162✔
79
}
80

81
// Remove swap-removes the component at the given index.
82
// Returns whether a swap was necessary.
83
func (c *column) Remove(index uint32, zero unsafe.Pointer) bool {
1,013,077✔
84
        lastIndex := uintptr(c.len - 1)
1,013,077✔
85
        swapped := index != uint32(lastIndex)
1,013,077✔
86

1,013,077✔
87
        if swapped && c.itemSize != 0 {
2,021,819✔
88
                src := unsafe.Add(c.pointer, lastIndex*c.itemSize)
1,008,742✔
89
                dst := unsafe.Add(c.pointer, uintptr(index)*c.itemSize)
1,008,742✔
90
                copyPtr(src, dst, uintptr(c.itemSize))
1,008,742✔
91
        }
1,008,742✔
92
        c.len--
1,013,077✔
93
        if zero != nil {
1,520,349✔
94
                c.Zero(lastIndex, zero)
507,272✔
95
        }
507,272✔
96
        return swapped
1,013,077✔
97
}
98

99
// Extend the column to be able to store the given number of additional components.
100
// Has no effect of the column's capacity is already sufficient.
101
// If the capacity needs to be increased, it will be doubled until it is sufficient.
102
func (c *column) Extend(by uint32) {
1,017,474✔
103
        required := c.Len() + int(by)
1,017,474✔
104
        cap := c.Cap()
1,017,474✔
105
        if cap >= required {
2,034,096✔
106
                return
1,016,622✔
107
        }
1,016,622✔
108
        for cap < required {
1,792✔
109
                cap *= 2
940✔
110
        }
940✔
111
        old := c.data
852✔
112
        c.data = reflect.New(reflect.ArrayOf(cap, old.Type().Elem())).Elem()
852✔
113
        c.pointer = c.data.Addr().UnsafePointer()
852✔
114
        reflect.Copy(c.data, old)
852✔
115
}
116

117
// Zero resets the memory at the given index.
118
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
507,272✔
119
        if c.itemSize == 0 {
507,399✔
120
                return
127✔
121
        }
127✔
122
        dst := unsafe.Add(c.pointer, index*c.itemSize)
507,145✔
123
        copyPtr(zero, dst, uintptr(c.itemSize))
507,145✔
124
}
125

126
// Zero resets a block of storage in one buffer.
127
func (c *column) ZeroRange(start, len uint32, zero unsafe.Pointer) {
2✔
128
        size := uint32(c.itemSize)
2✔
129
        if size == 0 {
3✔
130
                return
1✔
131
        }
1✔
132
        var i uint32
1✔
133
        for i = 0; i < len; i++ {
33✔
134
                dst := unsafe.Add(c.pointer, (i+start)*size)
32✔
135
                copyPtr(zero, dst, c.itemSize)
32✔
136
        }
32✔
137
}
138

139
func (c *column) Reset(zero unsafe.Pointer) {
3✔
140
        len := c.len
3✔
141
        if c.len == 0 {
3✔
142
                return
×
143
        }
×
144
        c.len = 0
3✔
145
        if zero == nil {
4✔
146
                return
1✔
147
        }
1✔
148
        if len <= 64 { // A coarse estimate where manually zeroing is faster
4✔
149
                c.ZeroRange(0, len, zero)
2✔
150
        } else {
2✔
151
                c.data.SetZero()
×
152
        }
×
153
}
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