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

mlange-42 / ark / 13768706324

10 Mar 2025 03:38PM CUT coverage: 99.437%. Remained the same
13768706324

Pull #179

github

web-flow
Merge 8094be2da into 093db9782
Pull Request #179: Fix false-positive debug checks

6538 of 6575 relevant lines covered (99.44%)

27299.41 hits per line

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

97.79
/ecs/storage.go
1
package ecs
2

3
import (
4
        "unsafe"
5
)
6

7
type storage struct {
8
        config     config
9
        registry   componentRegistry
10
        entities   []entityIndex
11
        isTarget   []bool
12
        entityPool entityPool
13
        graph      graph
14
        cache      cache
15

16
        archetypes         []archetype
17
        relationArchetypes []archetypeID
18
        tables             []table
19
        components         []componentStorage
20
}
21

22
type componentStorage struct {
23
        columns []*column
24
}
25

26
func newStorage(capacity ...int) storage {
208✔
27
        config := newConfig(capacity...)
208✔
28

208✔
29
        reg := newComponentRegistry()
208✔
30
        entities := make([]entityIndex, reservedEntities, config.initialCapacity+reservedEntities)
208✔
31
        isTarget := make([]bool, reservedEntities, config.initialCapacity+reservedEntities)
208✔
32
        // Reserved zero and wildcard entities
208✔
33
        for i := range reservedEntities {
624✔
34
                entities[i] = entityIndex{table: maxTableID, row: 0}
416✔
35
        }
416✔
36
        componentsMap := make([]int16, maskTotalBits)
208✔
37
        for i := range maskTotalBits {
53,456✔
38
                componentsMap[i] = -1
53,248✔
39
        }
53,248✔
40

41
        archetypes := make([]archetype, 0, 128)
208✔
42
        archetypes = append(archetypes, newArchetype(0, 0, &bitMask{}, []ID{}, []tableID{0}, &reg))
208✔
43
        tables := make([]table, 0, 128)
208✔
44
        tables = append(tables, newTable(0, &archetypes[0], uint32(config.initialCapacity), &reg, []Entity{}, []RelationID{}))
208✔
45
        return storage{
208✔
46
                config:     config,
208✔
47
                registry:   reg,
208✔
48
                cache:      newCache(),
208✔
49
                entities:   entities,
208✔
50
                isTarget:   isTarget,
208✔
51
                entityPool: newEntityPool(uint32(config.initialCapacity), reservedEntities),
208✔
52
                graph:      newGraph(),
208✔
53
                archetypes: archetypes,
208✔
54
                tables:     tables,
208✔
55
                components: make([]componentStorage, 0, maskTotalBits),
208✔
56
        }
208✔
57
}
58

59
func (s *storage) findOrCreateTable(oldTable *table, add []ID, remove []ID, relations []RelationID, outMask *bitMask) *table {
497,985✔
60
        startNode := s.archetypes[oldTable.archetype].node
497,985✔
61

497,985✔
62
        node := s.graph.Find(startNode, add, remove, outMask)
497,985✔
63
        var arch *archetype
497,985✔
64
        if archID, ok := node.GetArchetype(); ok {
995,559✔
65
                arch = &s.archetypes[archID]
497,574✔
66
        } else {
497,985✔
67
                arch = s.createArchetype(node)
411✔
68
                node.archetype = arch.id
411✔
69
        }
411✔
70

71
        var allRelations []RelationID
497,985✔
72
        if len(relations) > 0 {
498,648✔
73
                allRelations = appendNew(oldTable.relationIDs, relations...)
663✔
74
        } else {
497,985✔
75
                allRelations = oldTable.relationIDs
497,322✔
76
        }
497,322✔
77
        table, ok := arch.GetTable(s, allRelations)
497,985✔
78
        if !ok {
498,455✔
79
                table = s.createTable(arch, allRelations)
470✔
80
        }
470✔
81
        return table
497,984✔
82
}
83

84
func (s *storage) AddComponent(id uint8) {
922✔
85
        if len(s.components) != int(id) {
923✔
86
                panic("components can only be added to a storage sequentially")
1✔
87
        }
88
        s.components = append(s.components, componentStorage{columns: make([]*column, len(s.tables))})
921✔
89
}
90

91
// RemoveEntity removes the given entity from the world.
92
func (s *storage) RemoveEntity(entity Entity) {
495,554✔
93
        if !s.entityPool.Alive(entity) {
495,555✔
94
                panic("can't remove a dead entity")
1✔
95
        }
96
        index := &s.entities[entity.id]
495,553✔
97
        table := &s.tables[index.table]
495,553✔
98

495,553✔
99
        swapped := table.Remove(index.row)
495,553✔
100

495,553✔
101
        s.entityPool.Recycle(entity)
495,553✔
102

495,553✔
103
        if swapped {
989,666✔
104
                swapEntity := table.GetEntity(uintptr(index.row))
494,113✔
105
                s.entities[swapEntity.id].row = index.row
494,113✔
106
        }
494,113✔
107
        index.table = maxTableID
495,553✔
108

495,553✔
109
        if s.isTarget[entity.id] {
495,556✔
110
                s.cleanupArchetypes(entity)
3✔
111
                s.isTarget[entity.id] = false
3✔
112
        }
3✔
113
}
114

115
func (s *storage) Reset() {
1✔
116
        s.entities = s.entities[:reservedEntities]
1✔
117
        s.entityPool.Reset()
1✔
118
        s.isTarget = s.isTarget[:reservedEntities]
1✔
119
        s.cache.Reset()
1✔
120

1✔
121
        for i := range s.archetypes {
5✔
122
                s.archetypes[i].Reset(s)
4✔
123
        }
4✔
124
}
125

126
func (s *storage) get(entity Entity, component ID) unsafe.Pointer {
5✔
127
        if !s.entityPool.Alive(entity) {
6✔
128
                panic("can't get component of a dead entity")
1✔
129
        }
130
        return s.getUnchecked(entity, component)
4✔
131
}
132

133
func (s *storage) getUnchecked(entity Entity, component ID) unsafe.Pointer {
5✔
134
        s.checkHasComponent(entity, component)
5✔
135
        index := s.entities[entity.id]
5✔
136
        return s.tables[index.table].Get(component, uintptr(index.row))
5✔
137
}
5✔
138

139
func (s *storage) has(entity Entity, component ID) bool {
17✔
140
        if !s.entityPool.Alive(entity) {
18✔
141
                panic("can't get component of a dead entity")
1✔
142
        }
143
        return s.hasUnchecked(entity, component)
16✔
144
}
145

146
func (s *storage) hasUnchecked(entity Entity, component ID) bool {
18✔
147
        index := s.entities[entity.id]
18✔
148
        return s.tables[index.table].Has(component)
18✔
149
}
18✔
150

151
func (s *storage) getRelation(entity Entity, comp ID) Entity {
347✔
152
        if !s.entityPool.Alive(entity) {
348✔
153
                panic("can't get relation for a dead entity")
1✔
154
        }
155
        return s.getRelationUnchecked(entity, comp)
346✔
156
}
157

158
func (s *storage) getRelationUnchecked(entity Entity, comp ID) Entity {
351✔
159
        s.checkHasComponent(entity, comp)
351✔
160
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
351✔
161
}
351✔
162

163
func (s *storage) registerTargets(relations []RelationID) {
498,038✔
164
        for _, rel := range relations {
498,802✔
165
                s.isTarget[rel.target.id] = true
764✔
166
        }
764✔
167
}
168

169
func (s *storage) registerFilter(batch *Batch) cacheID {
32✔
170
        return s.cache.register(s, batch)
32✔
171
}
32✔
172

173
func (s *storage) unregisterFilter(entry cacheID) {
28✔
174
        s.cache.unregister(entry)
28✔
175
}
28✔
176

177
func (s *storage) getRegisteredFilter(id cacheID) *cacheEntry {
28✔
178
        return s.cache.getEntry(id)
28✔
179
}
28✔
180

181
func (s *storage) createEntity(table tableID) (Entity, uint32) {
497,401✔
182
        entity := s.entityPool.Get()
497,401✔
183

497,401✔
184
        idx := s.tables[table].Add(entity)
497,401✔
185
        len := len(s.entities)
497,401✔
186
        if int(entity.id) == len {
501,944✔
187
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,543✔
188
                s.isTarget = append(s.isTarget, false)
4,543✔
189
        } else {
497,401✔
190
                s.entities[entity.id] = entityIndex{table: table, row: idx}
492,858✔
191
        }
492,858✔
192
        return entity, idx
497,401✔
193
}
194

195
func (s *storage) createEntities(table *table, count int) {
159✔
196
        startIdx := table.Len()
159✔
197
        table.Alloc(uint32(count))
159✔
198

159✔
199
        len := len(s.entities)
159✔
200
        for i := range count {
3,214✔
201
                index := uint32(startIdx + i)
3,055✔
202
                entity := s.entityPool.Get()
3,055✔
203
                table.SetEntity(index, entity)
3,055✔
204

3,055✔
205
                if int(entity.id) == len {
5,892✔
206
                        s.entities = append(s.entities, entityIndex{table: table.id, row: index})
2,837✔
207
                        s.isTarget = append(s.isTarget, false)
2,837✔
208
                        len++
2,837✔
209
                } else {
3,055✔
210
                        s.entities[entity.id] = entityIndex{table: table.id, row: index}
218✔
211
                }
218✔
212
        }
213
}
214

215
func (s *storage) createArchetype(node *node) *archetype {
411✔
216
        comps := node.mask.toTypes(&s.registry.registry)
411✔
217
        index := len(s.archetypes)
411✔
218
        s.archetypes = append(s.archetypes, newArchetype(archetypeID(index), node.id, &node.mask, comps, nil, &s.registry))
411✔
219
        archetype := &s.archetypes[index]
411✔
220
        if archetype.HasRelations() {
461✔
221
                s.relationArchetypes = append(s.relationArchetypes, archetype.id)
50✔
222
        }
50✔
223
        return archetype
411✔
224
}
225

226
func (s *storage) createTable(archetype *archetype, relations []RelationID) *table {
493✔
227
        targets := make([]Entity, len(archetype.components))
493✔
228
        numRelations := uint8(0)
493✔
229
        for _, rel := range relations {
638✔
230
                idx := archetype.componentsMap[rel.component.id]
145✔
231
                targets[idx] = rel.target
145✔
232
                numRelations++
145✔
233
        }
145✔
234
        if numRelations != archetype.numRelations {
493✔
235
                panic("relations must be fully specified")
×
236
        }
237
        for i := range relations {
638✔
238
                rel := &relations[i]
145✔
239
                s.checkRelationComponent(rel.component)
145✔
240
                s.checkRelationTarget(rel.target)
145✔
241
        }
145✔
242

243
        var newTableID tableID
493✔
244
        if id, ok := archetype.GetFreeTable(); ok {
497✔
245
                newTableID = id
4✔
246
                s.tables[newTableID].recycle(targets, relations)
4✔
247
        } else {
493✔
248
                newTableID = tableID(len(s.tables))
489✔
249
                cap := s.config.initialCapacity
489✔
250
                if archetype.HasRelations() {
617✔
251
                        cap = s.config.initialCapacityRelations
128✔
252
                }
128✔
253
                s.tables = append(s.tables, newTable(
489✔
254
                        newTableID, archetype, uint32(cap), &s.registry,
489✔
255
                        targets, relations))
489✔
256
        }
257
        archetype.AddTable(&s.tables[newTableID])
493✔
258

493✔
259
        table := &s.tables[newTableID]
493✔
260
        for i := range s.components {
3,094✔
261
                id := ID{id: uint8(i)}
2,601✔
262
                comps := &s.components[i]
2,601✔
263
                if archetype.mask.Get(id) {
4,365✔
264
                        comps.columns = append(comps.columns, table.GetColumn(id))
1,764✔
265
                } else {
2,601✔
266
                        comps.columns = append(comps.columns, nil)
837✔
267
                }
837✔
268
        }
269

270
        s.cache.addTable(s, table)
493✔
271
        return table
493✔
272
}
273

274
// Removes empty archetypes that have a target relation to the given entity.
275
func (s *storage) cleanupArchetypes(target Entity) {
12✔
276
        newRelations := []RelationID{}
12✔
277
        for _, arch := range s.relationArchetypes {
31✔
278
                archetype := &s.archetypes[arch]
19✔
279
                len := len(archetype.tables)
19✔
280
                for i := len - 1; i >= 0; i-- {
47✔
281
                        table := &s.tables[archetype.tables[i]]
28✔
282

28✔
283
                        foundTarget := false
28✔
284
                        for _, rel := range table.relationIDs {
64✔
285
                                if rel.target.id == target.id {
50✔
286
                                        newRelations = append(newRelations, RelationID{component: rel.component, target: Entity{}})
14✔
287
                                        foundTarget = true
14✔
288
                                }
14✔
289
                        }
290
                        if !foundTarget {
42✔
291
                                continue
14✔
292
                        }
293

294
                        if table.Len() > 0 {
15✔
295
                                allRelations := s.getExchangeTargetsUnchecked(table, newRelations)
1✔
296
                                newTable, ok := archetype.GetTable(s, allRelations)
1✔
297
                                if !ok {
2✔
298
                                        newTable = s.createTable(archetype, newRelations)
1✔
299
                                }
1✔
300
                                s.moveEntities(table, newTable, uint32(table.Len()))
1✔
301
                        }
302
                        archetype.FreeTable(table.id)
14✔
303
                        s.cache.removeTable(s, table)
14✔
304

14✔
305
                        newRelations = newRelations[:0]
14✔
306
                }
307
                archetype.RemoveTarget(target)
19✔
308
        }
309
}
310

311
// moveEntities moves all entities from src to dst.
312
func (s *storage) moveEntities(src, dst *table, count uint32) {
10✔
313
        oldLen := dst.Len()
10✔
314
        dst.AddAll(src, count)
10✔
315

10✔
316
        newLen := dst.Len()
10✔
317
        newTable := dst.id
10✔
318
        for i := oldLen; i < newLen; i++ {
258✔
319
                entity := dst.GetEntity(uintptr(i))
248✔
320
                s.entities[entity.id] = entityIndex{table: newTable, row: uint32(i)}
248✔
321
        }
248✔
322
        src.Reset()
10✔
323
}
324

325
func (s *storage) getExchangeTargetsUnchecked(oldTable *table, relations []RelationID) []RelationID {
1✔
326
        targets := make([]Entity, len(oldTable.columns))
1✔
327
        for i := range oldTable.columns {
3✔
328
                targets[i] = oldTable.columns[i].target
2✔
329
        }
2✔
330
        for _, rel := range relations {
2✔
331
                column := oldTable.components[rel.component.id]
1✔
332
                if rel.target == targets[column.index] {
1✔
333
                        continue
×
334
                }
335
                targets[column.index] = rel.target
1✔
336
        }
337

338
        result := make([]RelationID, 0, len(oldTable.relationIDs))
1✔
339
        for i, e := range targets {
3✔
340
                if !oldTable.columns[i].isRelation {
3✔
341
                        continue
1✔
342
                }
343
                id := oldTable.ids[i]
1✔
344
                result = append(result, RelationID{component: id, target: e})
1✔
345
        }
346

347
        return result
1✔
348
}
349

350
func (s *storage) getExchangeTargets(oldTable *table, relations []RelationID) ([]RelationID, bool) {
54✔
351
        changed := false
54✔
352
        targets := make([]Entity, len(oldTable.columns))
54✔
353
        for i := range oldTable.columns {
201✔
354
                targets[i] = oldTable.columns[i].target
147✔
355
        }
147✔
356
        for _, rel := range relations {
109✔
357
                // Validity of the target is checked when creating a new table.
55✔
358
                // Whether the component is a relation is checked when creating a new table.
55✔
359
                column := oldTable.components[rel.component.id]
55✔
360
                if rel.target == targets[column.index] {
55✔
361
                        continue
×
362
                }
363
                targets[column.index] = rel.target
55✔
364
                changed = true
55✔
365
        }
366
        if !changed {
54✔
367
                return nil, false
×
368
        }
×
369

370
        result := make([]RelationID, 0, len(oldTable.relationIDs))
54✔
371
        for i, e := range targets {
201✔
372
                if !oldTable.columns[i].isRelation {
238✔
373
                        continue
91✔
374
                }
375
                id := oldTable.ids[i]
56✔
376
                result = append(result, RelationID{component: id, target: e})
56✔
377
        }
378

379
        return result, true
54✔
380
}
381

382
func (s *storage) getTables(batch *Batch) []*table {
97✔
383
        tables := []*table{}
97✔
384

97✔
385
        for i := range s.archetypes {
451✔
386
                archetype := &s.archetypes[i]
354✔
387
                if !batch.filter.matches(&archetype.mask) {
517✔
388
                        continue
163✔
389
                }
390

391
                if !archetype.HasRelations() {
368✔
392
                        table := &s.tables[archetype.tables[0]]
177✔
393
                        tables = append(tables, table)
177✔
394
                        continue
177✔
395
                }
396

397
                tableIDs := archetype.GetTables(batch.relations)
14✔
398
                for _, tab := range tableIDs {
34✔
399
                        table := &s.tables[tab]
20✔
400
                        if !table.Matches(batch.relations) {
20✔
401
                                continue
×
402
                        }
403
                        tables = append(tables, table)
20✔
404
                }
405
        }
406
        return tables
97✔
407
}
408

409
func (s *storage) getTableIDs(batch *Batch) []tableID {
34✔
410
        tables := []tableID{}
34✔
411

34✔
412
        for i := range s.archetypes {
101✔
413
                archetype := &s.archetypes[i]
67✔
414
                if !batch.filter.matches(&archetype.mask) {
102✔
415
                        continue
35✔
416
                }
417

418
                if !archetype.HasRelations() {
39✔
419
                        tables = append(tables, archetype.tables[0])
7✔
420
                        continue
7✔
421
                }
422

423
                tableIDs := archetype.GetTables(batch.relations)
25✔
424
                for _, tab := range tableIDs {
84✔
425
                        table := &s.tables[tab]
59✔
426
                        if !table.Matches(batch.relations) {
59✔
427
                                continue
×
428
                        }
429
                        tables = append(tables, tab)
59✔
430
                }
431
        }
432
        return tables
34✔
433
}
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