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

mlange-42 / ark / 13683680318

05 Mar 2025 07:11PM CUT coverage: 99.318% (+0.002%) from 99.316%
13683680318

Pull #136

github

web-flow
Merge 0b4eff22d into c73c750d7
Pull Request #136: Add World.NewEntities

14 of 14 new or added lines in 1 file covered. (100.0%)

5824 of 5864 relevant lines covered (99.32%)

35696.89 hits per line

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

100.0
/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  // data buffer
11
        pointer    unsafe.Pointer // pointer to the first element
12
        isRelation bool           // whether this column is for a relation component
13
        target     Entity         // target entity if for a relation component
14
        itemSize   uintptr        // memory size of items
15
        len        uint32         // number of items
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 {
2,392✔
20
        // TODO: should be use a slice instead of an array here?
2,392✔
21
        data := reflect.New(reflect.ArrayOf(int(capacity), tp)).Elem()
2,392✔
22
        pointer := data.Addr().UnsafePointer()
2,392✔
23

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

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

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

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

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

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

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

70
func (c *column) SetLast(other *column, count uint32) {
320✔
71
        start := c.len - count
320✔
72
        src := other.Get(0)
320✔
73
        dst := c.Get(uintptr(start))
320✔
74
        copyPtr(src, dst, c.itemSize*uintptr(count))
320✔
75
}
320✔
76

77
// Set overwrites the component at the given index.
78
func (c *column) Set(index uint32, comp unsafe.Pointer) unsafe.Pointer {
1,053,464✔
79
        dst := c.Get(uintptr(index))
1,053,464✔
80
        if c.itemSize == 0 {
1,054,737✔
81
                return dst
1,273✔
82
        }
1,273✔
83

84
        copyPtr(comp, dst, uintptr(c.itemSize))
1,052,191✔
85
        return dst
1,052,191✔
86
}
87

88
// Remove swap-removes the component at the given index.
89
// Returns whether a swap was necessary.
90
func (c *column) Remove(index uint32, zero unsafe.Pointer) bool {
1,039,486✔
91
        lastIndex := uintptr(c.len - 1)
1,039,486✔
92
        swapped := index != uint32(lastIndex)
1,039,486✔
93

1,039,486✔
94
        if swapped && c.itemSize != 0 {
2,073,887✔
95
                src := unsafe.Add(c.pointer, lastIndex*c.itemSize)
1,034,401✔
96
                dst := unsafe.Add(c.pointer, uintptr(index)*c.itemSize)
1,034,401✔
97
                copyPtr(src, dst, uintptr(c.itemSize))
1,034,401✔
98
        }
1,034,401✔
99
        c.len--
1,039,486✔
100
        if zero != nil {
1,560,117✔
101
                c.Zero(lastIndex, zero)
520,631✔
102
        }
520,631✔
103
        return swapped
1,039,486✔
104
}
105

106
// Extend the column to be able to store the given number of additional components.
107
// Has no effect of the column's capacity is already sufficient.
108
// If the capacity needs to be increased, it will be doubled until it is sufficient.
109
func (c *column) Extend(by uint32) {
1,047,543✔
110
        required := c.Len() + int(by)
1,047,543✔
111
        cap := c.Cap()
1,047,543✔
112
        if cap >= required {
2,092,798✔
113
                return
1,045,255✔
114
        }
1,045,255✔
115
        for cap < required {
4,666✔
116
                cap *= 2
2,378✔
117
        }
2,378✔
118
        old := c.data
2,288✔
119
        c.data = reflect.New(reflect.ArrayOf(cap, old.Type().Elem())).Elem()
2,288✔
120
        c.pointer = c.data.Addr().UnsafePointer()
2,288✔
121
        reflect.Copy(c.data, old)
2,288✔
122
}
123

124
// Zero resets the memory at the given index.
125
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
520,631✔
126
        if c.itemSize == 0 {
520,852✔
127
                return
221✔
128
        }
221✔
129
        dst := unsafe.Add(c.pointer, index*c.itemSize)
520,410✔
130
        copyPtr(zero, dst, uintptr(c.itemSize))
520,410✔
131
}
132

133
// Zero resets a block of storage in one buffer.
134
func (c *column) ZeroRange(start, len uint32, zero unsafe.Pointer) {
588✔
135
        size := uint32(c.itemSize)
588✔
136
        if size == 0 {
605✔
137
                return
17✔
138
        }
17✔
139
        var i uint32
571✔
140
        for i = 0; i < len; i++ {
7,726✔
141
                dst := unsafe.Add(c.pointer, (i+start)*size)
7,155✔
142
                copyPtr(zero, dst, c.itemSize)
7,155✔
143
        }
7,155✔
144
}
145

146
func (c *column) Reset(zero unsafe.Pointer) {
777✔
147
        len := c.len
777✔
148
        if c.len == 0 {
779✔
149
                return
2✔
150
        }
2✔
151
        c.len = 0
775✔
152
        if zero == nil {
961✔
153
                return
186✔
154
        }
186✔
155
        if len <= 64 { // A coarse estimate where manually zeroing is faster
1,177✔
156
                c.ZeroRange(0, len, zero)
588✔
157
        } else {
589✔
158
                c.data.SetZero()
1✔
159
        }
1✔
160
}
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