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

mlange-42 / ark / 13528862241

25 Feb 2025 07:00PM CUT coverage: 94.795% (+0.008%) from 94.787%
13528862241

push

github

web-flow
Add Map.GetRelationUnchecked (#75)

6 of 6 new or added lines in 2 files covered. (100.0%)

3406 of 3593 relevant lines covered (94.8%)

51323.81 hits per line

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

93.81
/ecs/storage.go
1
package ecs
2

3
import "fmt"
4

5
type storage struct {
6
        registry   componentRegistry
7
        entities   entities
8
        isTarget   []bool
9
        entityPool entityPool
10

11
        archetypes         []archetype
12
        relationArchetypes []archetypeID
13
        tables             []table
14
        components         []componentStorage
15
        initialCapacity    uint32
16
}
17

18
type componentStorage struct {
19
        columns []*column
20
}
21

22
func newStorage(capacity uint32) storage {
93✔
23
        reg := newComponentRegistry()
93✔
24
        entities := make([]entityIndex, reservedEntities, capacity+reservedEntities)
93✔
25
        isTarget := make([]bool, reservedEntities, capacity+reservedEntities)
93✔
26
        // Reserved zero and wildcard entities
93✔
27
        for i := range reservedEntities {
279✔
28
                entities[i] = entityIndex{table: maxTableID, row: 0}
186✔
29
        }
186✔
30

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

47
func (s *storage) findOrCreateTable(oldTable *table, mask *Mask, relations []relationID) *table {
495,473✔
48
        // TODO: use archetype graph
495,473✔
49
        var arch *archetype
495,473✔
50
        for i := range s.archetypes {
1,486,564✔
51
                if s.archetypes[i].mask.Equals(mask) {
1,486,434✔
52
                        arch = &s.archetypes[i]
495,343✔
53
                        break
495,343✔
54
                }
55
        }
56
        if arch == nil {
495,603✔
57
                arch = s.createArchetype(mask)
130✔
58
        }
130✔
59
        allRelation := appendNew(oldTable.relationIDs, relations...)
495,473✔
60
        table, ok := arch.GetTable(s, allRelation)
495,473✔
61
        if !ok {
495,625✔
62
                table = s.createTable(arch, allRelation)
152✔
63
        }
152✔
64
        return table
495,473✔
65
}
66

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

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

493,292✔
82
        swapped := table.Remove(index.row)
493,292✔
83

493,292✔
84
        s.entityPool.Recycle(entity)
493,292✔
85

493,292✔
86
        if swapped {
985,513✔
87
                swapEntity := table.GetEntity(uintptr(index.row))
492,221✔
88
                s.entities[swapEntity.id].row = index.row
492,221✔
89
        }
492,221✔
90
        index.table = maxTableID
493,292✔
91

493,292✔
92
        if s.isTarget[entity.id] {
493,293✔
93
                s.cleanupArchetypes(entity)
1✔
94
                s.isTarget[entity.id] = false
1✔
95
        }
1✔
96
}
97

98
func (s *storage) getRelation(entity Entity, comp ID) Entity {
101✔
99
        if !s.entityPool.Alive(entity) {
101✔
100
                panic("can't get relation for a dead entity")
×
101
        }
102
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
101✔
103
}
104

105
func (s *storage) getRelationUnchecked(entity Entity, comp ID) Entity {
2✔
106
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
2✔
107
}
2✔
108

109
func (s *storage) registerTargets(relations []relationID) {
495,507✔
110
        for _, rel := range relations {
495,929✔
111
                s.isTarget[rel.target.id] = true
422✔
112
        }
422✔
113
}
114

115
func (s *storage) createEntity(table tableID) (Entity, uint32) {
494,893✔
116
        entity := s.entityPool.Get()
494,893✔
117

494,893✔
118
        idx := s.tables[table].Add(entity)
494,893✔
119
        len := len(s.entities)
494,893✔
120
        if int(entity.id) == len {
499,317✔
121
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
4,424✔
122
                s.isTarget = append(s.isTarget, false)
4,424✔
123
        } else {
494,893✔
124
                s.entities[entity.id] = entityIndex{table: table, row: idx}
490,469✔
125
        }
490,469✔
126
        return entity, idx
494,893✔
127
}
128

129
func (s *storage) createEntities(table *table, count int) {
16✔
130
        startIdx := table.Len()
16✔
131
        table.Alloc(uint32(count))
16✔
132

16✔
133
        len := len(s.entities)
16✔
134
        for i := range count {
400✔
135
                index := uint32(startIdx + i)
384✔
136
                entity := s.entityPool.Get()
384✔
137
                table.SetEntity(index, entity)
384✔
138

384✔
139
                if int(entity.id) == len {
768✔
140
                        s.entities = append(s.entities, entityIndex{table: table.id, row: index})
384✔
141
                        s.isTarget = append(s.isTarget, false)
384✔
142
                        len++
384✔
143
                } else {
384✔
144
                        s.entities[entity.id] = entityIndex{table: table.id, row: index}
×
145
                }
×
146
        }
147
}
148

149
func (s *storage) createArchetype(mask *Mask) *archetype {
130✔
150
        comps := mask.toTypes(&s.registry.registry)
130✔
151
        index := len(s.archetypes)
130✔
152
        s.archetypes = append(s.archetypes, newArchetype(archetypeID(index), mask, comps, nil, &s.registry))
130✔
153
        archetype := &s.archetypes[index]
130✔
154
        if archetype.HasRelations() {
145✔
155
                s.relationArchetypes = append(s.relationArchetypes, archetype.id)
15✔
156
        }
15✔
157
        return archetype
130✔
158
}
159

160
func (s *storage) createTable(archetype *archetype, relations []relationID) *table {
156✔
161
        targets := make([]Entity, len(archetype.components))
156✔
162
        numRelations := uint8(0)
156✔
163
        for _, rel := range relations {
203✔
164
                idx := archetype.componentsMap[rel.component.id]
47✔
165
                targets[idx] = rel.target
47✔
166
                numRelations++
47✔
167
        }
47✔
168
        if numRelations != archetype.numRelations {
156✔
169
                panic("relations must be fully specified")
×
170
        }
171

172
        var newTableID tableID
156✔
173
        if id, ok := archetype.GetFreeTable(); ok {
157✔
174
                newTableID = id
1✔
175
                s.tables[newTableID].recycle(targets, relations)
1✔
176
        } else {
156✔
177
                newTableID = tableID(len(s.tables))
155✔
178
                s.tables = append(s.tables, newTable(
155✔
179
                        newTableID, archetype.id, s.initialCapacity, &s.registry,
155✔
180
                        archetype.components, archetype.componentsMap,
155✔
181
                        archetype.isRelation, targets, relations))
155✔
182
        }
155✔
183
        archetype.AddTable(&s.tables[newTableID])
156✔
184

156✔
185
        table := &s.tables[newTableID]
156✔
186
        for i := range s.components {
915✔
187
                id := ID{id: uint8(i)}
759✔
188
                comps := &s.components[i]
759✔
189
                if archetype.mask.Get(id) {
1,334✔
190
                        comps.columns = append(comps.columns, table.GetColumn(id))
575✔
191
                } else {
759✔
192
                        comps.columns = append(comps.columns, nil)
184✔
193
                }
184✔
194
        }
195
        return table
156✔
196
}
197

198
func (s *storage) getExchangeMask(mask *Mask, add []ID, rem []ID) {
852✔
199
        for _, comp := range rem {
1,830✔
200
                if !mask.Get(comp) {
978✔
201
                        panic(fmt.Sprintf("entity does not have a component of type %v, can't remove", s.registry.Types[comp.id]))
×
202
                }
203
                mask.Set(comp, false)
978✔
204
        }
205
        for _, comp := range add {
1,816✔
206
                if mask.Get(comp) {
964✔
207
                        panic(fmt.Sprintf("entity already has component of type %v, can't add", s.registry.Types[comp.id]))
×
208
                }
209
                mask.Set(comp, true)
964✔
210
        }
211
}
212

213
// Removes empty archetypes that have a target relation to the given entity.
214
func (s *storage) cleanupArchetypes(target Entity) {
1✔
215
        newRelations := []relationID{}
1✔
216
        for _, arch := range s.relationArchetypes {
2✔
217
                archetype := &s.archetypes[arch]
1✔
218
                for _, t := range archetype.tables {
3✔
219
                        table := &s.tables[t]
2✔
220

2✔
221
                        foundTarget := false
2✔
222
                        for _, rel := range table.relationIDs {
4✔
223
                                if rel.target.id == target.id {
3✔
224
                                        newRelations = append(newRelations, relationID{component: rel.component, target: Entity{}})
1✔
225
                                        foundTarget = true
1✔
226
                                }
1✔
227
                        }
228
                        if !foundTarget {
3✔
229
                                continue
1✔
230
                        }
231

232
                        allRelations := s.getExchangeTargetsUnchecked(table, newRelations)
1✔
233
                        newTable, ok := archetype.GetTable(s, allRelations)
1✔
234
                        if !ok {
2✔
235
                                newTable = s.createTable(archetype, newRelations)
1✔
236
                        }
1✔
237
                        s.moveEntities(table, newTable)
1✔
238
                        archetype.FreeTable(table.id)
1✔
239

1✔
240
                        newRelations = newRelations[:0]
1✔
241
                }
242
                archetype.RemoveTarget(target)
1✔
243
        }
244
}
245

246
// moveEntities moves all entities from src to dst.
247
func (s *storage) moveEntities(src, dst *table) {
1✔
248
        oldLen := dst.Len()
1✔
249
        dst.AddAll(src)
1✔
250

1✔
251
        newLen := dst.Len()
1✔
252
        newTable := dst.id
1✔
253
        for i := oldLen; i < newLen; i++ {
33✔
254
                entity := dst.GetEntity(uintptr(i))
32✔
255
                s.entities[entity.id] = entityIndex{table: newTable, row: uint32(i)}
32✔
256
        }
32✔
257
        src.Reset()
1✔
258
}
259

260
func (s *storage) getExchangeTargetsUnchecked(oldTable *table, relations []relationID) []relationID {
1✔
261
        targets := append([]Entity(nil), oldTable.relations...)
1✔
262
        for _, rel := range relations {
2✔
263
                index := oldTable.components[rel.component.id]
1✔
264
                if rel.target == targets[index] {
1✔
265
                        continue
×
266
                }
267
                targets[index] = rel.target
1✔
268
        }
269

270
        result := make([]relationID, 0, len(oldTable.relationIDs))
1✔
271
        for i, e := range targets {
3✔
272
                if !oldTable.isRelation[i] {
3✔
273
                        continue
1✔
274
                }
275
                id := oldTable.ids[i]
1✔
276
                result = append(result, relationID{component: id, target: e})
1✔
277
        }
278

279
        return result
1✔
280
}
281

282
func (s *storage) getExchangeTargets(oldTable *table, relations []relationID) ([]relationID, bool) {
34✔
283
        changed := false
34✔
284
        targets := append([]Entity(nil), oldTable.relations...)
34✔
285
        for _, rel := range relations {
68✔
286
                if !rel.target.IsZero() && !s.entityPool.Alive(rel.target) {
34✔
287
                        panic("can't make a dead entity a relation target")
×
288
                }
289
                index := oldTable.components[rel.component.id]
34✔
290
                if !oldTable.isRelation[index] {
34✔
291
                        panic(fmt.Sprintf("component %d is not a relation component", rel.component.id))
×
292
                }
293
                if rel.target == targets[index] {
34✔
294
                        continue
×
295
                }
296
                targets[index] = rel.target
34✔
297
                changed = true
34✔
298
        }
299
        if !changed {
34✔
300
                return nil, false
×
301
        }
×
302

303
        result := make([]relationID, 0, len(oldTable.relationIDs))
34✔
304
        for i, e := range targets {
101✔
305
                if !oldTable.isRelation[i] {
99✔
306
                        continue
32✔
307
                }
308
                id := oldTable.ids[i]
35✔
309
                result = append(result, relationID{component: id, target: e})
35✔
310
        }
311

312
        return result, true
34✔
313
}
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