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

mlange-42 / arche / 12662138174

08 Jan 2025 01:09AM CUT coverage: 100.0%. Remained the same
12662138174

push

github

web-flow
Update benchmarks and README (#462)

Competition benchmarks are outdated, and benchmarks vs. AoS highly depend on the hardware (see #414). Therefore, the benchmarks are removed from the README.

Also, meanwhile Arche is not the fasted Go ECS anymore (if code generation is permitted, see #412). Therefore, the feature list is rewritten.

Closes #329
Closes #414
Closes #461

* Go version upgrade for benchmarks
* use Go 1.23 in CI
* make benchmarking table functionality public
* remove ArrayOfStructs benchmarks from README
* print CPU name in benchmark tables
* upgrade testify
* print arche version above benchmarks
* cleanup README
* remove readme ref from benchmarks
* update user guide and changelog
* remove table benchmarks for deprecated methods
* add a second field to benchmark comps, document it
* add world creation and ID access to benchmarks
* addWorld.Reset to benchmarks
* rework design section ofthe README
* remove statement on OESA models
* update user guide
* remove plotting code for competition benchmarks
* remove competition benchmarks code
* link go-ecs-benchmarks in README and user guide

6510 of 6510 relevant lines covered (100.0%)

113528.92 hits per line

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

100.0
/ecs/archetype.go
1
package ecs
2

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

8
        "github.com/mlange-42/arche/ecs/stats"
9
)
10

11
const layoutChunkSize uint8 = 16
12

13
// layoutSize is the size of an archetype column layout in bytes.
14
var layoutSize uint32 = uint32(unsafe.Sizeof(layout{}))
15

16
// Helper for accessing data from an archetype
17
type archetypeAccess struct {
18
        basePointer          unsafe.Pointer // Pointer to the first component column layout.
19
        entityPointer        unsafe.Pointer // Pointer to the entity storage
20
        Mask                 Mask           // Archetype's mask
21
        RelationTarget       Entity         // Target entity of the archetype (if it has a relation component)
22
        RelationComponent    ID             // Relation component of the archetype
23
        HasRelationComponent bool           // Whether the archetype has a relation
24
}
25

26
// GetEntity returns the entity at the given index
27
func (a *archetypeAccess) GetEntity(index uint32) Entity {
7,430,498✔
28
        return *(*Entity)(unsafe.Add(a.entityPointer, entitySize*index))
7,430,498✔
29
}
7,430,498✔
30

31
// Get returns the component with the given ID at the given index
32
func (a *archetypeAccess) Get(index uint32, id ID) unsafe.Pointer {
2,970,176✔
33
        return a.getLayout(id).Get(index)
2,970,176✔
34
}
2,970,176✔
35

36
// HasComponent returns whether the archetype contains the given component ID.
37
func (a *archetypeAccess) HasComponent(id ID) bool {
493,724✔
38
        return a.getLayout(id).pointer != nil
493,724✔
39
}
493,724✔
40

41
// HasRelation returns whether the archetype has a relation component.
42
func (a *archetypeAccess) HasRelation() bool {
28,919✔
43
        return a.HasRelationComponent
28,919✔
44
}
28,919✔
45

46
// GetLayout returns the column layout for a component.
47
func (a *archetypeAccess) getLayout(id ID) *layout {
5,986,825✔
48
        return (*layout)(unsafe.Add(a.basePointer, layoutSize*uint32(id.id)))
5,986,825✔
49
}
5,986,825✔
50

51
// layout specification of a component column.
52
type layout struct {
53
        pointer  unsafe.Pointer // Pointer to the first element in the component column.
54
        itemSize uint32         // Component/step size
55
}
56

57
// Get returns a pointer to the item at the given index.
58
func (l *layout) Get(index uint32) unsafe.Pointer {
2,970,174✔
59
        if l.pointer == nil {
2,970,175✔
60
                return nil
1✔
61
        }
1✔
62
        return unsafe.Add(l.pointer, l.itemSize*index)
2,970,173✔
63
}
64

65
// archetype represents an ECS archetype
66
type archetype struct {
67
        *archetypeData
68
        node            *archNode // Node in the archetype graph.
69
        archetypeAccess           // Access helper, passed to queries.
70
        len             uint32    // Current number of entities.
71
        cap             uint32    // Current capacity.
72
}
73

74
type archetypeData struct {
75
        entityBuffer reflect.Value   // Reflection array containing entity data.
76
        layouts      []layout        // Column layouts by ID.
77
        buffers      []reflect.Value // Reflection arrays containing component data.
78
        indices      idMap[uint32]   // Mapping from IDs to buffer indices.
79
        index        int32           // Index of the archetype in the world.
80
}
81

82
// Init initializes an archetype
83
func (a *archetype) Init(node *archNode, data *archetypeData, index int32, forStorage bool, layouts uint8, relation Entity) {
1,521✔
84
        if !node.IsActive {
2,879✔
85
                node.IsActive = true
1,358✔
86
        }
1,358✔
87

88
        a.archetypeData = data
1,521✔
89
        a.buffers = make([]reflect.Value, len(node.Ids))
1,521✔
90
        a.indices = newIDMap[uint32]()
1,521✔
91
        a.index = index
1,521✔
92
        a.layouts = make([]layout, layouts)
1,521✔
93

1,521✔
94
        cap := 1
1,521✔
95
        if forStorage {
2,897✔
96
                cap = int(node.initialCapacity)
1,376✔
97
        }
1,376✔
98

99
        for i, id := range node.Ids {
7,141✔
100
                tp := node.Types[i]
5,620✔
101
                size, align := tp.Size(), uintptr(tp.Align())
5,620✔
102
                size = (size + (align - 1)) / align * align
5,620✔
103

5,620✔
104
                a.buffers[i] = reflect.New(reflect.ArrayOf(cap, tp)).Elem()
5,620✔
105
                a.layouts[id.id] = layout{
5,620✔
106
                        a.buffers[i].Addr().UnsafePointer(),
5,620✔
107
                        uint32(size),
5,620✔
108
                }
5,620✔
109
                a.indices.Set(id.id, uint32(i))
5,620✔
110
        }
5,620✔
111
        a.entityBuffer = reflect.New(reflect.ArrayOf(cap, entityType)).Elem()
1,521✔
112

1,521✔
113
        a.archetypeAccess = archetypeAccess{
1,521✔
114
                basePointer:          unsafe.Pointer(&a.layouts[0]),
1,521✔
115
                entityPointer:        a.entityBuffer.Addr().UnsafePointer(),
1,521✔
116
                Mask:                 node.Mask,
1,521✔
117
                RelationTarget:       relation,
1,521✔
118
                RelationComponent:    node.Relation,
1,521✔
119
                HasRelationComponent: node.HasRelation,
1,521✔
120
        }
1,521✔
121

1,521✔
122
        a.node = node
1,521✔
123

1,521✔
124
        a.len = 0
1,521✔
125
        a.cap = uint32(cap)
1,521✔
126
}
127

128
// Add adds an entity with optionally zeroed components to the archetype
129
func (a *archetype) Alloc(entity Entity) uint32 {
1,513,733✔
130
        idx := a.len
1,513,733✔
131
        a.extend(1)
1,513,733✔
132
        a.addEntity(idx, &entity)
1,513,733✔
133
        a.len++
1,513,733✔
134
        return idx
1,513,733✔
135
}
1,513,733✔
136

137
// AllocN allocates storage for the given number of entities.
138
func (a *archetype) AllocN(count uint32) {
27,636✔
139
        a.extend(count)
27,636✔
140
        a.len += count
27,636✔
141
}
27,636✔
142

143
// Remove removes an entity and its components from the archetype.
144
//
145
// Performs a swap-remove and reports whether a swap was necessary
146
// (i.e. not the last entity that was removed).
147
func (a *archetype) Remove(index uint32) bool {
1,507,171✔
148
        swapped := a.removeEntity(index)
1,507,171✔
149

1,507,171✔
150
        old := a.len - 1
1,507,171✔
151

1,507,171✔
152
        if index != old {
2,515,144✔
153
                for _, id := range a.node.Ids {
2,016,192✔
154
                        lay := a.getLayout(id)
1,008,219✔
155
                        size := lay.itemSize
1,008,219✔
156
                        if size == 0 {
1,008,529✔
157
                                continue
310✔
158
                        }
159
                        src := unsafe.Add(lay.pointer, old*size)
1,007,909✔
160
                        dst := unsafe.Add(lay.pointer, index*size)
1,007,909✔
161
                        a.copy(src, dst, size)
1,007,909✔
162
                }
163
        }
164

165
        // Zero the free memory to allow the garbage collector
166
        // to take into account pointers in the removed component.
167
        a.ZeroAll(old)
1,507,171✔
168
        a.len--
1,507,171✔
169

1,507,171✔
170
        return swapped
1,507,171✔
171
}
172

173
// ZeroAll resets a block of storage in all buffers.
174
func (a *archetype) ZeroAll(index uint32) {
1,507,171✔
175
        for _, id := range a.node.Ids {
2,520,836✔
176
                a.Zero(index, id)
1,013,665✔
177
        }
1,013,665✔
178
}
179

180
// Zero resets a block of storage in one buffer.
181
func (a *archetype) Zero(index uint32, id ID) {
1,013,665✔
182
        lay := a.getLayout(id)
1,013,665✔
183
        size := lay.itemSize
1,013,665✔
184
        if size == 0 {
1,016,818✔
185
                return
3,153✔
186
        }
3,153✔
187
        dst := unsafe.Add(lay.pointer, index*size)
1,010,512✔
188
        a.copy(a.node.zeroPointer, dst, size)
1,010,512✔
189
}
190

191
// SetEntity overwrites an entity
192
func (a *archetype) SetEntity(index uint32, entity Entity) {
2,761,728✔
193
        a.addEntity(index, &entity)
2,761,728✔
194
}
2,761,728✔
195

196
// Set overwrites a component with the data behind the given pointer.
197
//
198
// Deprecated: Method is slow and should not be used.
199
func (a *archetype) Set(index uint32, id ID, comp interface{}) unsafe.Pointer {
492,798✔
200
        lay := a.getLayout(id)
492,798✔
201
        dst := a.Get(index, id)
492,798✔
202
        size := lay.itemSize
492,798✔
203
        if size == 0 {
492,871✔
204
                return dst
73✔
205
        }
73✔
206
        rValue := reflect.ValueOf(comp).Elem()
492,725✔
207

492,725✔
208
        valueType := rValue.Type()
492,725✔
209
        valuePtr := reflect.NewAt(valueType, dst)
492,725✔
210
        valuePtr.Elem().Set(rValue)
492,725✔
211

492,725✔
212
        return dst
492,725✔
213
}
214

215
// SetPointer overwrites a component with the data behind the given pointer
216
func (a *archetype) SetPointer(index uint32, id ID, comp unsafe.Pointer) unsafe.Pointer {
8,071✔
217
        lay := a.getLayout(id)
8,071✔
218
        dst := a.Get(index, id)
8,071✔
219
        size := lay.itemSize
8,071✔
220
        if size == 0 {
13,450✔
221
                return dst
5,379✔
222
        }
5,379✔
223

224
        a.copy(comp, dst, size)
2,692✔
225
        return dst
2,692✔
226
}
227

228
// Reset removes all entities and components.
229
//
230
// Does NOT free the reserved memory.
231
func (a *archetype) Reset() {
52,552✔
232
        if a.len == 0 {
77,609✔
233
                return
25,057✔
234
        }
25,057✔
235
        a.len = 0
27,495✔
236
        for _, buf := range a.buffers {
55,024✔
237
                buf.SetZero()
27,529✔
238
        }
27,529✔
239
}
240

241
// Deactivate the archetype for later re-use.
242
func (a *archetype) Deactivate() {
27,427✔
243
        a.Reset()
27,427✔
244
        a.index = -1
27,427✔
245
}
27,427✔
246

247
// Activate reactivates a de-activated archetype.
248
func (a *archetype) Activate(target Entity, index int32) {
27,404✔
249
        a.index = index
27,404✔
250
        a.RelationTarget = target
27,404✔
251
}
27,404✔
252

253
func (a *archetype) ExtendLayouts(count uint8) {
8✔
254
        if len(a.layouts) >= int(count) {
10✔
255
                return
2✔
256
        }
2✔
257
        temp := a.layouts
6✔
258
        a.layouts = make([]layout, count)
6✔
259
        copy(a.layouts, temp)
6✔
260
        a.archetypeAccess.basePointer = unsafe.Pointer(&a.layouts[0])
6✔
261
}
262

263
// IsActive returns whether the archetype is active.
264
// Otherwise, it is eligible for re-use.
265
func (a *archetype) IsActive() bool {
5,102,573✔
266
        return a.index >= 0
5,102,573✔
267
}
5,102,573✔
268

269
// Components returns the component IDs for this archetype
270
func (a *archetype) Components() []ID {
494,853✔
271
        return a.node.Ids
494,853✔
272
}
494,853✔
273

274
// Len reports the number of entities in the archetype
275
func (a *archetype) Len() uint32 {
6,743,105✔
276
        return a.len
6,743,105✔
277
}
6,743,105✔
278

279
// Cap reports the current capacity of the archetype
280
func (a *archetype) Cap() uint32 {
5,100,047✔
281
        return a.cap
5,100,047✔
282
}
5,100,047✔
283

284
// Stats generates statistics for an archetype
285
func (a *archetype) Stats(reg *componentRegistry) stats.Archetype {
113✔
286
        ids := a.Components()
113✔
287
        aCompCount := len(ids)
113✔
288
        aTypes := make([]reflect.Type, aCompCount)
113✔
289
        for j, id := range ids {
225✔
290
                aTypes[j], _ = reg.ComponentType(id.id)
112✔
291
        }
112✔
292

293
        cap := int(a.Cap())
113✔
294
        memPerEntity := 0
113✔
295
        for _, id := range a.node.Ids {
225✔
296
                lay := a.getLayout(id)
112✔
297
                memPerEntity += int(lay.itemSize)
112✔
298
        }
112✔
299
        memory := cap * (int(entitySize) + memPerEntity)
113✔
300

113✔
301
        return stats.Archetype{
113✔
302
                IsActive: a.IsActive(),
113✔
303
                Size:     int(a.Len()),
113✔
304
                Capacity: cap,
113✔
305
                Memory:   memory,
113✔
306
        }
113✔
307
}
308

309
// UpdateStats updates statistics for an archetype
310
func (a *archetype) UpdateStats(node *stats.Node, stats *stats.Archetype, reg *componentRegistry) {
5,099,924✔
311
        cap := int(a.Cap())
5,099,924✔
312
        memory := cap * (int(entitySize) + node.MemoryPerEntity)
5,099,924✔
313

5,099,924✔
314
        stats.IsActive = a.IsActive()
5,099,924✔
315
        stats.Size = int(a.Len())
5,099,924✔
316
        stats.Capacity = cap
5,099,924✔
317
        stats.Memory = memory
5,099,924✔
318
}
5,099,924✔
319

320
// copy from one pointer to another.
321
func (a *archetype) copy(src, dst unsafe.Pointer, itemSize uint32) {
7,304,547✔
322
        dstSlice := (*[math.MaxInt32]byte)(dst)[:itemSize:itemSize]
7,304,547✔
323
        srcSlice := (*[math.MaxInt32]byte)(src)[:itemSize:itemSize]
7,304,547✔
324
        copy(dstSlice, srcSlice)
7,304,547✔
325
}
7,304,547✔
326

327
// extend the memory buffers if necessary for adding an entity.
328
func (a *archetype) extend(by uint32) {
1,541,372✔
329
        required := a.len + by
1,541,372✔
330
        if a.cap >= required {
3,082,683✔
331
                return
1,541,311✔
332
        }
1,541,311✔
333
        for a.cap < required {
144✔
334
                a.cap *= 2
83✔
335
        }
83✔
336
        a.cap = max(a.cap, a.node.initialCapacity)
61✔
337

61✔
338
        old := a.entityBuffer
61✔
339
        a.entityBuffer = reflect.New(reflect.ArrayOf(int(a.cap), entityType)).Elem()
61✔
340
        a.entityPointer = a.entityBuffer.Addr().UnsafePointer()
61✔
341
        reflect.Copy(a.entityBuffer, old)
61✔
342

61✔
343
        for _, id := range a.node.Ids {
121✔
344
                lay := a.getLayout(id)
60✔
345
                if lay.itemSize == 0 {
68✔
346
                        continue
8✔
347
                }
348
                index, _ := a.indices.Get(id.id)
52✔
349
                old := a.buffers[index]
52✔
350
                a.buffers[index] = reflect.New(reflect.ArrayOf(int(a.cap), old.Type().Elem())).Elem()
52✔
351
                lay.pointer = a.buffers[index].Addr().UnsafePointer()
52✔
352
                reflect.Copy(a.buffers[index], old)
52✔
353
        }
354
}
355

356
// Adds an entity at the given index. Does not extend the entity buffer.
357
func (a *archetype) addEntity(index uint32, entity *Entity) {
4,275,461✔
358
        dst := unsafe.Add(a.entityPointer, entitySize*index)
4,275,461✔
359
        src := unsafe.Pointer(entity)
4,275,461✔
360
        a.copy(src, dst, entitySize)
4,275,461✔
361
}
4,275,461✔
362

363
// removeEntity removes an entity from tne archetype.
364
// Components need to be removed separately.
365
func (a *archetype) removeEntity(index uint32) bool {
1,507,171✔
366
        old := a.len - 1
1,507,171✔
367

1,507,171✔
368
        if index == old {
2,006,369✔
369
                return false
499,198✔
370
        }
499,198✔
371

372
        src := unsafe.Add(a.entityPointer, old*entitySize)
1,007,973✔
373
        dst := unsafe.Add(a.entityPointer, index*entitySize)
1,007,973✔
374
        a.copy(src, dst, entitySize)
1,007,973✔
375

1,007,973✔
376
        return true
1,007,973✔
377
}
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