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

mlange-42 / ark / 13610747635

02 Mar 2025 01:27AM CUT coverage: 97.424%. Remained the same
13610747635

push

github

web-flow
Group and rename pos/vel benchmarks (#99)

3895 of 3998 relevant lines covered (97.42%)

45948.49 hits per line

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

98.06
/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 {
939✔
20
        // TODO: should be use a slice instead of an array here?
939✔
21
        data := reflect.New(reflect.ArrayOf(int(capacity), tp)).Elem()
939✔
22
        pointer := data.Addr().UnsafePointer()
939✔
23

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

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

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

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

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

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

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

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

77
        copyPtr(comp, dst, uintptr(c.itemSize))
992,510✔
78
        return dst
992,510✔
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 {
986,510✔
84
        lastIndex := uintptr(c.len - 1)
986,510✔
85
        swapped := index != uint32(lastIndex)
986,510✔
86

986,510✔
87
        if swapped && c.itemSize != 0 {
1,968,310✔
88
                src := unsafe.Add(c.pointer, lastIndex*c.itemSize)
981,800✔
89
                dst := unsafe.Add(c.pointer, uintptr(index)*c.itemSize)
981,800✔
90
                copyPtr(src, dst, uintptr(c.itemSize))
981,800✔
91
        }
981,800✔
92
        c.len--
986,510✔
93
        if zero != nil {
1,480,522✔
94
                c.Zero(lastIndex, zero)
494,012✔
95
        }
494,012✔
96
        return swapped
986,510✔
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) {
992,040✔
103
        required := c.Len() + int(by)
992,040✔
104
        cap := c.Cap()
992,040✔
105
        if cap >= required {
1,983,184✔
106
                return
991,144✔
107
        }
991,144✔
108
        for cap < required {
1,880✔
109
                cap *= 2
984✔
110
        }
984✔
111
        old := c.data
896✔
112
        c.data = reflect.New(reflect.ArrayOf(cap, old.Type().Elem())).Elem()
896✔
113
        c.pointer = c.data.Addr().UnsafePointer()
896✔
114
        reflect.Copy(c.data, old)
896✔
115
}
116

117
// Zero resets the memory at the given index.
118
func (c *column) Zero(index uintptr, zero unsafe.Pointer) {
494,012✔
119
        if c.itemSize == 0 {
494,150✔
120
                return
138✔
121
        }
138✔
122
        dst := unsafe.Add(c.pointer, index*c.itemSize)
493,874✔
123
        copyPtr(zero, dst, uintptr(c.itemSize))
493,874✔
124
}
125

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

139
func (c *column) Reset(zero unsafe.Pointer) {
18✔
140
        len := c.len
18✔
141
        if c.len == 0 {
24✔
142
                return
6✔
143
        }
6✔
144
        c.len = 0
12✔
145
        if zero == nil {
17✔
146
                return
5✔
147
        }
5✔
148
        if len <= 64 { // A coarse estimate where manually zeroing is faster
14✔
149
                c.ZeroRange(0, len, zero)
7✔
150
        } else {
7✔
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