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

mlange-42 / ark / 13620752776

03 Mar 2025 12:17AM CUT coverage: 97.759%. Remained the same
13620752776

push

github

web-flow
Set up user guide (#106)

5192 of 5311 relevant lines covered (97.76%)

38008.39 hits per line

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

94.72
/ecs/storage.go
1
package ecs
2

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

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

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

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

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

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

56
func (s *storage) findOrCreateTable(oldTable *table, add []ID, remove []ID, relations []RelationID) *table {
504,666✔
57
        startNode := s.archetypes[oldTable.archetype].node
504,666✔
58

504,666✔
59
        node := s.graph.Find(startNode, add, remove)
504,666✔
60
        var arch *archetype
504,666✔
61
        if archID, ok := node.GetArchetype(); ok {
1,008,954✔
62
                arch = &s.archetypes[archID]
504,288✔
63
        } else {
504,666✔
64
                arch = s.createArchetype(node)
378✔
65
                node.archetype = arch.id
378✔
66
        }
378✔
67

68
        allRelation := appendNew(oldTable.relationIDs, relations...)
504,666✔
69
        table, ok := arch.GetTable(s, allRelation)
504,666✔
70
        if !ok {
505,078✔
71
                table = s.createTable(arch, allRelation)
412✔
72
        }
412✔
73
        return table
504,666✔
74
}
75

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

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

502,840✔
91
        swapped := table.Remove(index.row)
502,840✔
92

502,840✔
93
        s.entityPool.Recycle(entity)
502,840✔
94

502,840✔
95
        if swapped {
1,004,568✔
96
                swapEntity := table.GetEntity(uintptr(index.row))
501,728✔
97
                s.entities[swapEntity.id].row = index.row
501,728✔
98
        }
501,728✔
99
        index.table = maxTableID
502,840✔
100

502,840✔
101
        if s.isTarget[entity.id] {
502,842✔
102
                s.cleanupArchetypes(entity)
2✔
103
                s.isTarget[entity.id] = false
2✔
104
        }
2✔
105
}
106

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

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

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

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

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

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

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

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

155
func (s *storage) registerTargets(relations []RelationID) {
504,718✔
156
        for _, rel := range relations {
505,221✔
157
                s.isTarget[rel.target.id] = true
503✔
158
        }
503✔
159
}
160

161
func (s *storage) createEntity(table tableID) (Entity, uint32) {
504,086✔
162
        entity := s.entityPool.Get()
504,086✔
163

504,086✔
164
        idx := s.tables[table].Add(entity)
504,086✔
165
        len := len(s.entities)
504,086✔
166
        if int(entity.id) == len {
508,241✔
167
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,155✔
168
                s.isTarget = append(s.isTarget, false)
4,155✔
169
        } else {
504,086✔
170
                s.entities[entity.id] = entityIndex{table: table, row: idx}
499,931✔
171
        }
499,931✔
172
        return entity, idx
504,086✔
173
}
174

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

328
        return result
2✔
329
}
330

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

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

364
        return result, true
52✔
365
}
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