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

mlange-42 / ark / 13619551557

02 Mar 2025 09:36PM CUT coverage: 97.72%. Remained the same
13619551557

Pull #103

github

web-flow
Merge 5b8cfc29e into db91fb54b
Pull Request #103: Update README

5101 of 5220 relevant lines covered (97.72%)

35973.1 hits per line

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

94.68
/ecs/storage.go
1
package ecs
2

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

8
type storage struct {
9
        registry   componentRegistry
10
        entities   entities
11
        isTarget   []bool
12
        entityPool entityPool
13

14
        archetypes         []archetype
15
        relationArchetypes []archetypeID
16
        tables             []table
17
        components         []componentStorage
18
        initialCapacity    uint32
19
}
20

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

25
func newStorage(capacity uint32) storage {
179✔
26
        reg := newComponentRegistry()
179✔
27
        entities := make([]entityIndex, reservedEntities, capacity+reservedEntities)
179✔
28
        isTarget := make([]bool, reservedEntities, capacity+reservedEntities)
179✔
29
        // Reserved zero and wildcard entities
179✔
30
        for i := range reservedEntities {
537✔
31
                entities[i] = entityIndex{table: maxTableID, row: 0}
358✔
32
        }
358✔
33
        componentsMap := make([]int16, MaskTotalBits)
179✔
34
        for i := range MaskTotalBits {
46,003✔
35
                componentsMap[i] = -1
45,824✔
36
        }
45,824✔
37

38
        tables := make([]table, 0, 128)
179✔
39
        tables = append(tables, newTable(0, 0, capacity, &reg, []ID{}, componentsMap, []bool{}, []Entity{}, []RelationID{}))
179✔
40
        archetypes := make([]archetype, 0, 128)
179✔
41
        archetypes = append(archetypes, newArchetype(0, &Mask{}, []ID{}, []tableID{0}, &reg))
179✔
42
        return storage{
179✔
43
                registry:        reg,
179✔
44
                entities:        entities,
179✔
45
                isTarget:        isTarget,
179✔
46
                entityPool:      newEntityPool(capacity, reservedEntities),
179✔
47
                archetypes:      archetypes,
179✔
48
                tables:          tables,
179✔
49
                initialCapacity: capacity,
179✔
50
                components:      make([]componentStorage, 0, MaskTotalBits),
179✔
51
        }
179✔
52
}
53

54
func (s *storage) findOrCreateTable(oldTable *table, mask *Mask, relations []RelationID) *table {
499,930✔
55
        // TODO: use archetype graph
499,930✔
56
        var arch *archetype
499,930✔
57
        for i := range s.archetypes {
1,499,923✔
58
                if s.archetypes[i].mask.Equals(mask) {
1,499,545✔
59
                        arch = &s.archetypes[i]
499,552✔
60
                        break
499,552✔
61
                }
62
        }
63
        if arch == nil {
500,308✔
64
                arch = s.createArchetype(mask)
378✔
65
        }
378✔
66
        allRelation := appendNew(oldTable.relationIDs, relations...)
499,930✔
67
        table, ok := arch.GetTable(s, allRelation)
499,930✔
68
        if !ok {
500,342✔
69
                table = s.createTable(arch, allRelation)
412✔
70
        }
412✔
71
        return table
499,930✔
72
}
73

74
func (s *storage) AddComponent(id uint8) {
839✔
75
        if len(s.components) != int(id) {
839✔
76
                panic("components can only be added to a storage sequentially")
×
77
        }
78
        s.components = append(s.components, componentStorage{columns: make([]*column, len(s.tables))})
839✔
79
}
80

81
// RemoveEntity removes the given entity from the world.
82
func (s *storage) RemoveEntity(entity Entity) {
497,999✔
83
        if !s.entityPool.Alive(entity) {
497,999✔
84
                panic("can't remove a dead entity")
×
85
        }
86
        index := &s.entities[entity.id]
497,999✔
87
        table := &s.tables[index.table]
497,999✔
88

497,999✔
89
        swapped := table.Remove(index.row)
497,999✔
90

497,999✔
91
        s.entityPool.Recycle(entity)
497,999✔
92

497,999✔
93
        if swapped {
994,944✔
94
                swapEntity := table.GetEntity(uintptr(index.row))
496,945✔
95
                s.entities[swapEntity.id].row = index.row
496,945✔
96
        }
496,945✔
97
        index.table = maxTableID
497,999✔
98

497,999✔
99
        if s.isTarget[entity.id] {
498,001✔
100
                s.cleanupArchetypes(entity)
2✔
101
                s.isTarget[entity.id] = false
2✔
102
        }
2✔
103
}
104

105
func (s *storage) Reset() {
1✔
106
        s.entities = s.entities[:reservedEntities]
1✔
107
        s.entityPool.Reset()
1✔
108
        s.isTarget = s.isTarget[:reservedEntities]
1✔
109

1✔
110
        for i := range s.archetypes {
5✔
111
                s.archetypes[i].Reset(s)
4✔
112
        }
4✔
113
}
114

115
func (s *storage) get(entity Entity, component ID) unsafe.Pointer {
5✔
116
        if !s.entityPool.Alive(entity) {
6✔
117
                panic("can't get component of a dead entity")
1✔
118
        }
119
        return s.getUnchecked(entity, component)
4✔
120
}
121

122
func (s *storage) getUnchecked(entity Entity, component ID) unsafe.Pointer {
5✔
123
        s.checkHasComponent(entity, component)
5✔
124
        index := s.entities[entity.id]
5✔
125
        return s.tables[index.table].Get(component, uintptr(index.row))
5✔
126
}
5✔
127

128
func (s *storage) has(entity Entity, component ID) bool {
17✔
129
        if !s.entityPool.Alive(entity) {
18✔
130
                panic("can't get component of a dead entity")
1✔
131
        }
132
        return s.hasUnchecked(entity, component)
16✔
133
}
134

135
func (s *storage) hasUnchecked(entity Entity, component ID) bool {
18✔
136
        s.checkHasComponent(entity, component)
18✔
137
        index := s.entities[entity.id]
18✔
138
        return s.tables[index.table].Has(component)
18✔
139
}
18✔
140

141
func (s *storage) getRelation(entity Entity, comp ID) Entity {
345✔
142
        if !s.entityPool.Alive(entity) {
345✔
143
                panic("can't get relation for a dead entity")
×
144
        }
145
        return s.getRelationUnchecked(entity, comp)
345✔
146
}
147

148
func (s *storage) getRelationUnchecked(entity Entity, comp ID) Entity {
349✔
149
        s.checkHasComponent(entity, comp)
349✔
150
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
349✔
151
}
349✔
152

153
func (s *storage) registerTargets(relations []RelationID) {
499,982✔
154
        for _, rel := range relations {
500,485✔
155
                s.isTarget[rel.target.id] = true
503✔
156
        }
503✔
157
}
158

159
func (s *storage) createEntity(table tableID) (Entity, uint32) {
499,350✔
160
        entity := s.entityPool.Get()
499,350✔
161

499,350✔
162
        idx := s.tables[table].Add(entity)
499,350✔
163
        len := len(s.entities)
499,350✔
164
        if int(entity.id) == len {
503,743✔
165
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,393✔
166
                s.isTarget = append(s.isTarget, false)
4,393✔
167
        } else {
499,350✔
168
                s.entities[entity.id] = entityIndex{table: table, row: idx}
494,957✔
169
        }
494,957✔
170
        return entity, idx
499,350✔
171
}
172

173
func (s *storage) createEntities(table *table, count int) {
133✔
174
        startIdx := table.Len()
133✔
175
        table.Alloc(uint32(count))
133✔
176

133✔
177
        len := len(s.entities)
133✔
178
        for i := range count {
2,137✔
179
                index := uint32(startIdx + i)
2,004✔
180
                entity := s.entityPool.Get()
2,004✔
181
                table.SetEntity(index, entity)
2,004✔
182

2,004✔
183
                if int(entity.id) == len {
4,008✔
184
                        s.entities = append(s.entities, entityIndex{table: table.id, row: index})
2,004✔
185
                        s.isTarget = append(s.isTarget, false)
2,004✔
186
                        len++
2,004✔
187
                } else {
2,004✔
188
                        s.entities[entity.id] = entityIndex{table: table.id, row: index}
×
189
                }
×
190
        }
191
}
192

193
func (s *storage) createArchetype(mask *Mask) *archetype {
378✔
194
        comps := mask.toTypes(&s.registry.registry)
378✔
195
        index := len(s.archetypes)
378✔
196
        s.archetypes = append(s.archetypes, newArchetype(archetypeID(index), mask, comps, nil, &s.registry))
378✔
197
        archetype := &s.archetypes[index]
378✔
198
        if archetype.HasRelations() {
415✔
199
                s.relationArchetypes = append(s.relationArchetypes, archetype.id)
37✔
200
        }
37✔
201
        return archetype
378✔
202
}
203

204
func (s *storage) createTable(archetype *archetype, relations []RelationID) *table {
435✔
205
        targets := make([]Entity, len(archetype.components))
435✔
206
        numRelations := uint8(0)
435✔
207
        for _, rel := range relations {
537✔
208
                idx := archetype.componentsMap[rel.component.id]
102✔
209
                targets[idx] = rel.target
102✔
210
                numRelations++
102✔
211
        }
102✔
212
        if numRelations != archetype.numRelations {
435✔
213
                panic("relations must be fully specified")
×
214
        }
215

216
        var newTableID tableID
435✔
217
        if id, ok := archetype.GetFreeTable(); ok {
436✔
218
                newTableID = id
1✔
219
                s.tables[newTableID].recycle(targets, relations)
1✔
220
        } else {
435✔
221
                newTableID = tableID(len(s.tables))
434✔
222
                s.tables = append(s.tables, newTable(
434✔
223
                        newTableID, archetype.id, s.initialCapacity, &s.registry,
434✔
224
                        archetype.components, archetype.componentsMap,
434✔
225
                        archetype.isRelation, targets, relations))
434✔
226
        }
434✔
227
        archetype.AddTable(&s.tables[newTableID])
435✔
228

435✔
229
        table := &s.tables[newTableID]
435✔
230
        for i := range s.components {
2,826✔
231
                id := ID{id: uint8(i)}
2,391✔
232
                comps := &s.components[i]
2,391✔
233
                if archetype.mask.Get(id) {
3,984✔
234
                        comps.columns = append(comps.columns, table.GetColumn(id))
1,593✔
235
                } else {
2,391✔
236
                        comps.columns = append(comps.columns, nil)
798✔
237
                }
798✔
238
        }
239
        return table
435✔
240
}
241

242
func (s *storage) getExchangeMask(mask *Mask, add []ID, rem []ID) {
806✔
243
        for _, comp := range rem {
2,004✔
244
                if !mask.Get(comp) {
1,198✔
245
                        panic(fmt.Sprintf("entity does not have a component of type %v, can't remove", s.registry.Types[comp.id]))
×
246
                }
247
                mask.Set(comp, false)
1,198✔
248
        }
249
        for _, comp := range add {
1,986✔
250
                if mask.Get(comp) {
1,180✔
251
                        panic(fmt.Sprintf("entity already has component of type %v, can't add", s.registry.Types[comp.id]))
×
252
                }
253
                mask.Set(comp, true)
1,180✔
254
        }
255
}
256

257
// Removes empty archetypes that have a target relation to the given entity.
258
func (s *storage) cleanupArchetypes(target Entity) {
2✔
259
        newRelations := []RelationID{}
2✔
260
        for _, arch := range s.relationArchetypes {
4✔
261
                archetype := &s.archetypes[arch]
2✔
262
                for _, t := range archetype.tables {
6✔
263
                        table := &s.tables[t]
4✔
264

4✔
265
                        foundTarget := false
4✔
266
                        for _, rel := range table.relationIDs {
8✔
267
                                if rel.target.id == target.id {
6✔
268
                                        newRelations = append(newRelations, RelationID{component: rel.component, target: Entity{}})
2✔
269
                                        foundTarget = true
2✔
270
                                }
2✔
271
                        }
272
                        if !foundTarget {
6✔
273
                                continue
2✔
274
                        }
275

276
                        allRelations := s.getExchangeTargetsUnchecked(table, newRelations)
2✔
277
                        newTable, ok := archetype.GetTable(s, allRelations)
2✔
278
                        if !ok {
4✔
279
                                newTable = s.createTable(archetype, newRelations)
2✔
280
                        }
2✔
281
                        s.moveEntities(table, newTable, uint32(table.Len()))
2✔
282
                        archetype.FreeTable(table.id)
2✔
283

2✔
284
                        newRelations = newRelations[:0]
2✔
285
                }
286
                archetype.RemoveTarget(target)
2✔
287
        }
288
}
289

290
// moveEntities moves all entities from src to dst.
291
func (s *storage) moveEntities(src, dst *table, count uint32) {
11✔
292
        oldLen := dst.Len()
11✔
293
        dst.AddAll(src, count)
11✔
294

11✔
295
        newLen := dst.Len()
11✔
296
        newTable := dst.id
11✔
297
        for i := oldLen; i < newLen; i++ {
259✔
298
                entity := dst.GetEntity(uintptr(i))
248✔
299
                s.entities[entity.id] = entityIndex{table: newTable, row: uint32(i)}
248✔
300
        }
248✔
301
        src.Reset()
11✔
302
}
303

304
func (s *storage) getExchangeTargetsUnchecked(oldTable *table, relations []RelationID) []RelationID {
2✔
305
        targets := make([]Entity, len(oldTable.columns))
2✔
306
        for i := range oldTable.columns {
6✔
307
                targets[i] = oldTable.columns[i].target
4✔
308
        }
4✔
309
        for _, rel := range relations {
4✔
310
                index := oldTable.components[rel.component.id]
2✔
311
                if rel.target == targets[index] {
2✔
312
                        continue
×
313
                }
314
                targets[index] = rel.target
2✔
315
        }
316

317
        result := make([]RelationID, 0, len(oldTable.relationIDs))
2✔
318
        for i, e := range targets {
6✔
319
                if !oldTable.columns[i].isRelation {
6✔
320
                        continue
2✔
321
                }
322
                id := oldTable.ids[i]
2✔
323
                result = append(result, RelationID{component: id, target: e})
2✔
324
        }
325

326
        return result
2✔
327
}
328

329
func (s *storage) getExchangeTargets(oldTable *table, relations []RelationID) ([]RelationID, bool) {
52✔
330
        changed := false
52✔
331
        targets := make([]Entity, len(oldTable.columns))
52✔
332
        for i := range oldTable.columns {
197✔
333
                targets[i] = oldTable.columns[i].target
145✔
334
        }
145✔
335
        for _, rel := range relations {
105✔
336
                if !rel.target.IsZero() && !s.entityPool.Alive(rel.target) {
53✔
337
                        panic("can't make a dead entity a relation target")
×
338
                }
339
                index := oldTable.components[rel.component.id]
53✔
340
                if !oldTable.columns[index].isRelation {
53✔
341
                        panic(fmt.Sprintf("component %d is not a relation component", rel.component.id))
×
342
                }
343
                if rel.target == targets[index] {
53✔
344
                        continue
×
345
                }
346
                targets[index] = rel.target
53✔
347
                changed = true
53✔
348
        }
349
        if !changed {
52✔
350
                return nil, false
×
351
        }
×
352

353
        result := make([]RelationID, 0, len(oldTable.relationIDs))
52✔
354
        for i, e := range targets {
197✔
355
                if !oldTable.columns[i].isRelation {
236✔
356
                        continue
91✔
357
                }
358
                id := oldTable.ids[i]
54✔
359
                result = append(result, RelationID{component: id, target: e})
54✔
360
        }
361

362
        return result, true
52✔
363
}
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