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

mlange-42 / ark / 13595642731

28 Feb 2025 08:05PM CUT coverage: 96.909% (+0.03%) from 96.882%
13595642731

Pull #81

github

web-flow
Merge 770782cba into 9b17c8beb
Pull Request #81: Unsafe API

45 of 47 new or added lines in 3 files covered. (95.74%)

25 existing lines in 2 files now uncovered.

3606 of 3721 relevant lines covered (96.91%)

50547.51 hits per line

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

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

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

50
func (s *storage) findOrCreateTable(oldTable *table, mask *Mask, relations []RelationID) *table {
507,318✔
51
        // TODO: use archetype graph
507,318✔
52
        var arch *archetype
507,318✔
53
        for i := range s.archetypes {
1,521,856✔
54
                if s.archetypes[i].mask.Equals(mask) {
1,521,723✔
55
                        arch = &s.archetypes[i]
507,185✔
56
                        break
507,185✔
57
                }
58
        }
59
        if arch == nil {
507,451✔
60
                arch = s.createArchetype(mask)
133✔
61
        }
133✔
62
        allRelation := appendNew(oldTable.relationIDs, relations...)
507,318✔
63
        table, ok := arch.GetTable(s, allRelation)
507,318✔
64
        if !ok {
507,473✔
65
                table = s.createTable(arch, allRelation)
155✔
66
        }
155✔
67
        return table
507,318✔
68
}
69

70
func (s *storage) AddComponent(id uint8) {
462✔
71
        if len(s.components) != int(id) {
462✔
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))})
462✔
75
}
76

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

505,189✔
85
        swapped := table.Remove(index.row)
505,189✔
86

505,189✔
87
        s.entityPool.Recycle(entity)
505,189✔
88

505,189✔
89
        if swapped {
1,009,154✔
90
                swapEntity := table.GetEntity(uintptr(index.row))
503,965✔
91
                s.entities[swapEntity.id].row = index.row
503,965✔
92
        }
503,965✔
93
        index.table = maxTableID
505,189✔
94

505,189✔
95
        if s.isTarget[entity.id] {
505,190✔
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 {
3✔
102
        if !s.entityPool.Alive(entity) {
3✔
NEW
UNCOV
103
                panic("can't get component of a dead entity")
×
104
        }
105
        return s.getUnchecked(entity, component)
3✔
106
}
107

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

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

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

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

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

136
func (s *storage) registerTargets(relations []RelationID) {
507,361✔
137
        for _, rel := range relations {
507,803✔
138
                s.isTarget[rel.target.id] = true
442✔
139
        }
442✔
140
}
141

142
func (s *storage) createEntity(table tableID) (Entity, uint32) {
506,996✔
143
        entity := s.entityPool.Get()
506,996✔
144

506,996✔
145
        idx := s.tables[table].Add(entity)
506,996✔
146
        len := len(s.entities)
506,996✔
147
        if int(entity.id) == len {
511,151✔
148
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,155✔
149
                s.isTarget = append(s.isTarget, false)
4,155✔
150
        } else {
506,996✔
151
                s.entities[entity.id] = entityIndex{table: table, row: idx}
502,841✔
152
        }
502,841✔
153
        return entity, idx
506,996✔
154
}
155

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

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

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

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

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

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

168✔
212
        table := &s.tables[newTableID]
168✔
213
        for i := range s.components {
941✔
214
                id := ID{id: uint8(i)}
773✔
215
                comps := &s.components[i]
773✔
216
                if archetype.mask.Get(id) {
1,369✔
217
                        comps.columns = append(comps.columns, table.GetColumn(id))
596✔
218
                } else {
773✔
219
                        comps.columns = append(comps.columns, nil)
177✔
220
                }
177✔
221
        }
222
        return table
168✔
223
}
224

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

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

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

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

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

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

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

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

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

309
        return result
1✔
310
}
311

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

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

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