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

mlange-42 / ark / 13526736655

25 Feb 2025 05:02PM CUT coverage: 94.787%. Remained the same
13526736655

push

github

web-flow
Add an example to the README (#74)

3400 of 3587 relevant lines covered (94.79%)

51722.1 hits per line

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

93.72
/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 {
92✔
23
        reg := newComponentRegistry()
92✔
24
        entities := make([]entityIndex, reservedEntities, capacity+reservedEntities)
92✔
25
        isTarget := make([]bool, reservedEntities, capacity+reservedEntities)
92✔
26
        // Reserved zero and wildcard entities
92✔
27
        for i := range reservedEntities {
276✔
28
                entities[i] = entityIndex{table: maxTableID, row: 0}
184✔
29
        }
184✔
30

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

47
func (s *storage) findOrCreateTable(oldTable *table, mask *Mask, relations []relationID) *table {
498,669✔
48
        // TODO: use archetype graph
498,669✔
49
        var arch *archetype
498,669✔
50
        for i := range s.archetypes {
1,496,153✔
51
                if s.archetypes[i].mask.Equals(mask) {
1,496,024✔
52
                        arch = &s.archetypes[i]
498,540✔
53
                        break
498,540✔
54
                }
55
        }
56
        if arch == nil {
498,798✔
57
                arch = s.createArchetype(mask)
129✔
58
        }
129✔
59
        allRelation := appendNew(oldTable.relationIDs, relations...)
498,669✔
60
        table, ok := arch.GetTable(s, allRelation)
498,669✔
61
        if !ok {
498,820✔
62
                table = s.createTable(arch, allRelation)
151✔
63
        }
151✔
64
        return table
498,669✔
65
}
66

67
func (s *storage) AddComponent(id uint8) {
426✔
68
        if len(s.components) != int(id) {
426✔
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))})
426✔
72
}
73

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

496,582✔
82
        swapped := table.Remove(index.row)
496,582✔
83

496,582✔
84
        s.entityPool.Recycle(entity)
496,582✔
85

496,582✔
86
        if swapped {
992,105✔
87
                swapEntity := table.GetEntity(uintptr(index.row))
495,523✔
88
                s.entities[swapEntity.id].row = index.row
495,523✔
89
        }
495,523✔
90
        index.table = maxTableID
496,582✔
91

496,582✔
92
        if s.isTarget[entity.id] {
496,583✔
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 {
99✔
99
        if !s.entityPool.Alive(entity) {
99✔
100
                panic("can't get relation for a dead entity")
×
101
        }
102
        return s.tables[s.entities[entity.id].table].GetRelation(comp)
99✔
103
}
104

105
func (s *storage) registerTargets(relations []relationID) {
498,702✔
106
        for _, rel := range relations {
499,122✔
107
                s.isTarget[rel.target.id] = true
420✔
108
        }
420✔
109
}
110

111
func (s *storage) createEntity(table tableID) (Entity, uint32) {
498,087✔
112
        entity := s.entityPool.Get()
498,087✔
113

498,087✔
114
        idx := s.tables[table].Add(entity)
498,087✔
115
        len := len(s.entities)
498,087✔
116
        if int(entity.id) == len {
501,979✔
117
                s.entities = append(s.entities, entityIndex{table: table, row: idx})
3,892✔
118
                s.isTarget = append(s.isTarget, false)
3,892✔
119
        } else {
498,087✔
120
                s.entities[entity.id] = entityIndex{table: table, row: idx}
494,195✔
121
        }
494,195✔
122
        return entity, idx
498,087✔
123
}
124

125
func (s *storage) createEntities(table *table, count int) {
16✔
126
        startIdx := table.Len()
16✔
127
        table.Alloc(uint32(count))
16✔
128

16✔
129
        len := len(s.entities)
16✔
130
        for i := range count {
400✔
131
                index := uint32(startIdx + i)
384✔
132
                entity := s.entityPool.Get()
384✔
133
                table.SetEntity(index, entity)
384✔
134

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

145
func (s *storage) createArchetype(mask *Mask) *archetype {
129✔
146
        comps := mask.toTypes(&s.registry.registry)
129✔
147
        index := len(s.archetypes)
129✔
148
        s.archetypes = append(s.archetypes, newArchetype(archetypeID(index), mask, comps, nil, &s.registry))
129✔
149
        archetype := &s.archetypes[index]
129✔
150
        if archetype.HasRelations() {
143✔
151
                s.relationArchetypes = append(s.relationArchetypes, archetype.id)
14✔
152
        }
14✔
153
        return archetype
129✔
154
}
155

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

168
        var newTableID tableID
154✔
169
        if id, ok := archetype.GetFreeTable(); ok {
155✔
170
                newTableID = id
1✔
171
                s.tables[newTableID].recycle(targets, relations)
1✔
172
        } else {
154✔
173
                newTableID = tableID(len(s.tables))
153✔
174
                s.tables = append(s.tables, newTable(
153✔
175
                        newTableID, archetype.id, s.initialCapacity, &s.registry,
153✔
176
                        archetype.components, archetype.componentsMap,
153✔
177
                        archetype.isRelation, targets, relations))
153✔
178
        }
153✔
179
        archetype.AddTable(&s.tables[newTableID])
154✔
180

154✔
181
        table := &s.tables[newTableID]
154✔
182
        for i := range s.components {
911✔
183
                id := ID{id: uint8(i)}
757✔
184
                comps := &s.components[i]
757✔
185
                if archetype.mask.Get(id) {
1,330✔
186
                        comps.columns = append(comps.columns, table.GetColumn(id))
573✔
187
                } else {
757✔
188
                        comps.columns = append(comps.columns, nil)
184✔
189
                }
184✔
190
        }
191
        return table
154✔
192
}
193

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

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

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

228
                        allRelations := s.getExchangeTargetsUnchecked(table, newRelations)
1✔
229
                        newTable, ok := archetype.GetTable(s, allRelations)
1✔
230
                        if !ok {
2✔
231
                                newTable = s.createTable(archetype, newRelations)
1✔
232
                        }
1✔
233
                        s.moveEntities(table, newTable)
1✔
234
                        archetype.FreeTable(table.id)
1✔
235

1✔
236
                        newRelations = newRelations[:0]
1✔
237
                }
238
                archetype.RemoveTarget(target)
1✔
239
        }
240
}
241

242
// moveEntities moves all entities from src to dst.
243
func (s *storage) moveEntities(src, dst *table) {
1✔
244
        oldLen := dst.Len()
1✔
245
        dst.AddAll(src)
1✔
246

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

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

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

275
        return result
1✔
276
}
277

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

299
        result := make([]relationID, 0, len(oldTable.relationIDs))
33✔
300
        for i, e := range targets {
99✔
301
                if !oldTable.isRelation[i] {
98✔
302
                        continue
32✔
303
                }
304
                id := oldTable.ids[i]
34✔
305
                result = append(result, relationID{component: id, target: e})
34✔
306
        }
307

308
        return result, true
33✔
309
}
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