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

mlange-42 / ark / 13596114390

28 Feb 2025 08:38PM CUT coverage: 96.747% (-0.1%) from 96.882%
13596114390

Pull #81

github

web-flow
Merge 376331b97 into 9b17c8beb
Pull Request #81: Unsafe API

134 of 143 new or added lines in 6 files covered. (93.71%)

12 existing lines in 1 file now uncovered.

3628 of 3750 relevant lines covered (96.75%)

49235.14 hits per line

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

93.57
/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 {
107✔
26
        reg := newComponentRegistry()
107✔
27
        entities := make([]entityIndex, reservedEntities, capacity+reservedEntities)
107✔
28
        isTarget := make([]bool, reservedEntities, capacity+reservedEntities)
107✔
29
        // Reserved zero and wildcard entities
107✔
30
        for i := range reservedEntities {
321✔
31
                entities[i] = entityIndex{table: maxTableID, row: 0}
214✔
32
        }
214✔
33

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

50
func (s *storage) findOrCreateTable(oldTable *table, mask *Mask, relations []RelationID) *table {
495,382✔
51
        // TODO: use archetype graph
495,382✔
52
        var arch *archetype
495,382✔
53
        for i := range s.archetypes {
1,486,045✔
54
                if s.archetypes[i].mask.Equals(mask) {
1,485,908✔
55
                        arch = &s.archetypes[i]
495,245✔
56
                        break
495,245✔
57
                }
58
        }
59
        if arch == nil {
495,519✔
60
                arch = s.createArchetype(mask)
137✔
61
        }
137✔
62
        allRelation := appendNew(oldTable.relationIDs, relations...)
495,382✔
63
        table, ok := arch.GetTable(s, allRelation)
495,382✔
64
        if !ok {
495,541✔
65
                table = s.createTable(arch, allRelation)
159✔
66
        }
159✔
67
        return table
495,382✔
68
}
69

70
func (s *storage) AddComponent(id uint8) {
466✔
71
        if len(s.components) != int(id) {
466✔
UNCOV
72
                panic("components can only be added to a storage sequentially")
×
73
        }
74
        s.components = append(s.components, componentStorage{columns: make([]*column, len(s.tables))})
466✔
75
}
76

77
// RemoveEntity removes the given entity from the world.
78
func (s *storage) RemoveEntity(entity Entity) {
492,249✔
79
        if !s.entityPool.Alive(entity) {
492,249✔
UNCOV
80
                panic("can't remove a dead entity")
×
81
        }
82
        index := &s.entities[entity.id]
492,249✔
83
        table := &s.tables[index.table]
492,249✔
84

492,249✔
85
        swapped := table.Remove(index.row)
492,249✔
86

492,249✔
87
        s.entityPool.Recycle(entity)
492,249✔
88

492,249✔
89
        if swapped {
983,378✔
90
                swapEntity := table.GetEntity(uintptr(index.row))
491,129✔
91
                s.entities[swapEntity.id].row = index.row
491,129✔
92
        }
491,129✔
93
        index.table = maxTableID
492,249✔
94

492,249✔
95
        if s.isTarget[entity.id] {
492,250✔
96
                s.cleanupArchetypes(entity)
1✔
97
                s.isTarget[entity.id] = false
1✔
98
        }
1✔
99
}
100

101
func (s *storage) get(entity Entity, component ID) unsafe.Pointer {
4✔
102
        if !s.entityPool.Alive(entity) {
4✔
NEW
UNCOV
103
                panic("can't get component of a dead entity")
×
104
        }
105
        return s.getUnchecked(entity, component)
4✔
106
}
107

108
func (s *storage) getUnchecked(entity Entity, component ID) unsafe.Pointer {
5✔
109
        index := s.entities[entity.id]
5✔
110
        return s.tables[index.table].Get(component, uintptr(index.row))
5✔
111
}
5✔
112

113
func (s *storage) has(entity Entity, component ID) bool {
15✔
114
        if !s.entityPool.Alive(entity) {
15✔
NEW
115
                panic("can't get component of a dead entity")
×
116
        }
117
        return s.hasUnchecked(entity, component)
15✔
118
}
119

120
func (s *storage) hasUnchecked(entity Entity, component ID) bool {
17✔
121
        index := s.entities[entity.id]
17✔
122
        return s.tables[index.table].Has(component)
17✔
123
}
17✔
124

125
func (s *storage) getRelation(entity Entity, comp ID) Entity {
129✔
126
        if !s.entityPool.Alive(entity) {
129✔
127
                panic("can't get relation for a dead entity")
×
128
        }
129
        index := s.entities[entity.id]
129✔
130
        return s.tables[index.table].GetRelation(comp)
129✔
131
}
132

133
func (s *storage) getRelationUnchecked(entity Entity, comp ID) Entity {
4✔
134
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
4✔
135
}
4✔
136

137
func (s *storage) registerTargets(relations []RelationID) {
495,425✔
138
        for _, rel := range relations {
495,869✔
139
                s.isTarget[rel.target.id] = true
444✔
140
        }
444✔
141
}
142

143
func (s *storage) createEntity(table tableID) (Entity, uint32) {
495,059✔
144
        entity := s.entityPool.Get()
495,059✔
145

495,059✔
146
        idx := s.tables[table].Add(entity)
495,059✔
147
        len := len(s.entities)
495,059✔
148
        if int(entity.id) == len {
499,874✔
149
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,815✔
150
                s.isTarget = append(s.isTarget, false)
4,815✔
151
        } else {
495,059✔
152
                s.entities[entity.id] = entityIndex{table: table, row: idx}
490,244✔
153
        }
490,244✔
154
        return entity, idx
495,059✔
155
}
156

157
func (s *storage) createEntities(table *table, count int) {
16✔
158
        startIdx := table.Len()
16✔
159
        table.Alloc(uint32(count))
16✔
160

16✔
161
        len := len(s.entities)
16✔
162
        for i := range count {
400✔
163
                index := uint32(startIdx + i)
384✔
164
                entity := s.entityPool.Get()
384✔
165
                table.SetEntity(index, entity)
384✔
166

384✔
167
                if int(entity.id) == len {
768✔
168
                        s.entities = append(s.entities, entityIndex{table: table.id, row: index})
384✔
169
                        s.isTarget = append(s.isTarget, false)
384✔
170
                        len++
384✔
171
                } else {
384✔
UNCOV
172
                        s.entities[entity.id] = entityIndex{table: table.id, row: index}
×
UNCOV
173
                }
×
174
        }
175
}
176

177
func (s *storage) createArchetype(mask *Mask) *archetype {
137✔
178
        comps := mask.toTypes(&s.registry.registry)
137✔
179
        index := len(s.archetypes)
137✔
180
        s.archetypes = append(s.archetypes, newArchetype(archetypeID(index), mask, comps, nil, &s.registry))
137✔
181
        archetype := &s.archetypes[index]
137✔
182
        if archetype.HasRelations() {
163✔
183
                s.relationArchetypes = append(s.relationArchetypes, archetype.id)
26✔
184
        }
26✔
185
        return archetype
137✔
186
}
187

188
func (s *storage) createTable(archetype *archetype, relations []RelationID) *table {
172✔
189
        targets := make([]Entity, len(archetype.components))
172✔
190
        numRelations := uint8(0)
172✔
191
        for _, rel := range relations {
241✔
192
                idx := archetype.componentsMap[rel.component.id]
69✔
193
                targets[idx] = rel.target
69✔
194
                numRelations++
69✔
195
        }
69✔
196
        if numRelations != archetype.numRelations {
172✔
UNCOV
197
                panic("relations must be fully specified")
×
198
        }
199

200
        var newTableID tableID
172✔
201
        if id, ok := archetype.GetFreeTable(); ok {
173✔
202
                newTableID = id
1✔
203
                s.tables[newTableID].recycle(targets, relations)
1✔
204
        } else {
172✔
205
                newTableID = tableID(len(s.tables))
171✔
206
                s.tables = append(s.tables, newTable(
171✔
207
                        newTableID, archetype.id, s.initialCapacity, &s.registry,
171✔
208
                        archetype.components, archetype.componentsMap,
171✔
209
                        archetype.isRelation, targets, relations))
171✔
210
        }
171✔
211
        archetype.AddTable(&s.tables[newTableID])
172✔
212

172✔
213
        table := &s.tables[newTableID]
172✔
214
        for i := range s.components {
953✔
215
                id := ID{id: uint8(i)}
781✔
216
                comps := &s.components[i]
781✔
217
                if archetype.mask.Get(id) {
1,382✔
218
                        comps.columns = append(comps.columns, table.GetColumn(id))
601✔
219
                } else {
781✔
220
                        comps.columns = append(comps.columns, nil)
180✔
221
                }
180✔
222
        }
223
        return table
172✔
224
}
225

226
func (s *storage) getExchangeMask(mask *Mask, add []ID, rem []ID) {
615✔
227
        for _, comp := range rem {
1,594✔
228
                if !mask.Get(comp) {
979✔
UNCOV
229
                        panic(fmt.Sprintf("entity does not have a component of type %v, can't remove", s.registry.Types[comp.id]))
×
230
                }
231
                mask.Set(comp, false)
979✔
232
        }
233
        for _, comp := range add {
1,343✔
234
                if mask.Get(comp) {
728✔
235
                        panic(fmt.Sprintf("entity already has component of type %v, can't add", s.registry.Types[comp.id]))
×
236
                }
237
                mask.Set(comp, true)
728✔
238
        }
239
}
240

241
// Removes empty archetypes that have a target relation to the given entity.
242
func (s *storage) cleanupArchetypes(target Entity) {
1✔
243
        newRelations := []RelationID{}
1✔
244
        for _, arch := range s.relationArchetypes {
2✔
245
                archetype := &s.archetypes[arch]
1✔
246
                for _, t := range archetype.tables {
3✔
247
                        table := &s.tables[t]
2✔
248

2✔
249
                        foundTarget := false
2✔
250
                        for _, rel := range table.relationIDs {
4✔
251
                                if rel.target.id == target.id {
3✔
252
                                        newRelations = append(newRelations, RelationID{component: rel.component, target: Entity{}})
1✔
253
                                        foundTarget = true
1✔
254
                                }
1✔
255
                        }
256
                        if !foundTarget {
3✔
257
                                continue
1✔
258
                        }
259

260
                        allRelations := s.getExchangeTargetsUnchecked(table, newRelations)
1✔
261
                        newTable, ok := archetype.GetTable(s, allRelations)
1✔
262
                        if !ok {
2✔
263
                                newTable = s.createTable(archetype, newRelations)
1✔
264
                        }
1✔
265
                        s.moveEntities(table, newTable)
1✔
266
                        archetype.FreeTable(table.id)
1✔
267

1✔
268
                        newRelations = newRelations[:0]
1✔
269
                }
270
                archetype.RemoveTarget(target)
1✔
271
        }
272
}
273

274
// moveEntities moves all entities from src to dst.
275
func (s *storage) moveEntities(src, dst *table) {
1✔
276
        oldLen := dst.Len()
1✔
277
        dst.AddAll(src)
1✔
278

1✔
279
        newLen := dst.Len()
1✔
280
        newTable := dst.id
1✔
281
        for i := oldLen; i < newLen; i++ {
33✔
282
                entity := dst.GetEntity(uintptr(i))
32✔
283
                s.entities[entity.id] = entityIndex{table: newTable, row: uint32(i)}
32✔
284
        }
32✔
285
        src.Reset()
1✔
286
}
287

288
func (s *storage) getExchangeTargetsUnchecked(oldTable *table, relations []RelationID) []RelationID {
1✔
289
        targets := make([]Entity, len(oldTable.columns))
1✔
290
        for i := range oldTable.columns {
3✔
291
                targets[i] = oldTable.columns[i].target
2✔
292
        }
2✔
293
        for _, rel := range relations {
2✔
294
                index := oldTable.components[rel.component.id]
1✔
295
                if rel.target == targets[index] {
1✔
UNCOV
296
                        continue
×
297
                }
298
                targets[index] = rel.target
1✔
299
        }
300

301
        result := make([]RelationID, 0, len(oldTable.relationIDs))
1✔
302
        for i, e := range targets {
3✔
303
                if !oldTable.columns[i].isRelation {
3✔
304
                        continue
1✔
305
                }
306
                id := oldTable.ids[i]
1✔
307
                result = append(result, RelationID{component: id, target: e})
1✔
308
        }
309

310
        return result
1✔
311
}
312

313
func (s *storage) getExchangeTargets(oldTable *table, relations []RelationID) ([]RelationID, bool) {
43✔
314
        changed := false
43✔
315
        targets := make([]Entity, len(oldTable.columns))
43✔
316
        for i := range oldTable.columns {
149✔
317
                targets[i] = oldTable.columns[i].target
106✔
318
        }
106✔
319
        for _, rel := range relations {
87✔
320
                if !rel.target.IsZero() && !s.entityPool.Alive(rel.target) {
44✔
UNCOV
321
                        panic("can't make a dead entity a relation target")
×
322
                }
323
                index := oldTable.components[rel.component.id]
44✔
324
                if !oldTable.columns[index].isRelation {
44✔
UNCOV
325
                        panic(fmt.Sprintf("component %d is not a relation component", rel.component.id))
×
326
                }
327
                if rel.target == targets[index] {
44✔
UNCOV
328
                        continue
×
329
                }
330
                targets[index] = rel.target
44✔
331
                changed = true
44✔
332
        }
333
        if !changed {
43✔
334
                return nil, false
×
UNCOV
335
        }
×
336

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

346
        return result, true
43✔
347
}
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