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

mlange-42 / ark / 13752994473

09 Mar 2025 10:10PM CUT coverage: 99.43%. Remained the same
13752994473

Pull #170

github

web-flow
Merge a957ad4a8 into 2ef7535d0
Pull Request #170: User guide section "Error handling"

6458 of 6495 relevant lines covered (99.43%)

28189.13 hits per line

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

99.48
/ecs/archetype.go
1
package ecs
2

3
import (
4
        "math"
5
        "reflect"
6
        "slices"
7

8
        "github.com/mlange-42/ark/ecs/stats"
9
)
10

11
type archetypeID uint32
12

13
// maxArchetypeID is used as unassigned archetype ID.
14
const maxArchetypeID = math.MaxUint32
15

16
type archetype struct {
17
        id             archetypeID
18
        node           nodeID
19
        mask           bitMask
20
        components     []ID                     // components IDs of the archetype in arbitrary order
21
        componentsMap  []int16                  // mapping from component IDs to column indices; -1 indicates none
22
        isRelation     []bool                   // whether columns are relations components, indexed by column index
23
        relationTables []map[entityID]*tableIDs // lookup for relation targets of tables, indexed by column index
24
        tables         []tableID                // all active tables
25
        freeTables     []tableID                // all inactive/free tables
26
        numRelations   uint8                    // number of relation components
27
}
28

29
type tableIDs struct {
30
        tables []tableID
31
}
32

33
func newArchetype(id archetypeID, node nodeID, mask *bitMask, components []ID, tables []tableID, reg *componentRegistry) archetype {
622✔
34
        componentsMap := make([]int16, maskTotalBits)
622✔
35
        for i := range maskTotalBits {
159,854✔
36
                componentsMap[i] = -1
159,232✔
37
        }
159,232✔
38
        for i, id := range components {
2,085✔
39
                componentsMap[id.id] = int16(i)
1,463✔
40
        }
1,463✔
41

42
        numRelations := uint8(0)
622✔
43
        isRelation := make([]bool, len(components))
622✔
44
        relationTables := make([]map[entityID]*tableIDs, len(components))
622✔
45
        for i, id := range components {
2,085✔
46
                if reg.IsRelation[id.id] {
1,518✔
47
                        isRelation[i] = true
55✔
48
                        relationTables[i] = map[entityID]*tableIDs{}
55✔
49
                        numRelations++
55✔
50
                }
55✔
51
        }
52
        return archetype{
622✔
53
                id:             id,
622✔
54
                node:           node,
622✔
55
                mask:           *mask,
622✔
56
                components:     components,
622✔
57
                componentsMap:  componentsMap,
622✔
58
                isRelation:     isRelation,
622✔
59
                tables:         tables,
622✔
60
                numRelations:   numRelations,
622✔
61
                relationTables: relationTables,
622✔
62
        }
622✔
63
}
64

65
func (a *archetype) HasRelations() bool {
507,283✔
66
        return a.numRelations > 0
507,283✔
67
}
507,283✔
68

69
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
504,670✔
70
        if len(a.tables) == 0 {
505,081✔
71
                return nil, false
411✔
72
        }
411✔
73
        if !a.HasRelations() {
1,007,851✔
74
                return &storage.tables[a.tables[0]], true
503,592✔
75
        }
503,592✔
76
        if len(relations) != int(a.numRelations) {
668✔
77
                panic("relations must be fully specified")
1✔
78
        }
79
        index := a.componentsMap[relations[0].component.id]
666✔
80
        tables, ok := a.relationTables[index][relations[0].target.id]
666✔
81
        if !ok {
744✔
82
                return nil, false
78✔
83
        }
78✔
84
        for _, t := range tables.tables {
1,195✔
85
                table := &storage.tables[t]
607✔
86
                if table.MatchesExact(relations) {
1,193✔
87
                        return table, true
586✔
88
                }
586✔
89
        }
90
        return nil, false
2✔
91
}
92

93
func (a *archetype) GetTables(relations []RelationID) []tableID {
92✔
94
        if !a.HasRelations() || len(relations) == 0 {
127✔
95
                return a.tables
35✔
96
        }
35✔
97
        index := a.componentsMap[relations[0].component.id]
57✔
98
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
114✔
99
                return tables.tables
57✔
100
        }
57✔
101
        return nil
×
102
}
103

104
func (a *archetype) GetFreeTable() (tableID, bool) {
491✔
105
        if len(a.freeTables) == 0 {
980✔
106
                return 0, false
489✔
107
        }
489✔
108
        last := len(a.freeTables) - 1
2✔
109
        table := a.freeTables[last]
2✔
110

2✔
111
        a.freeTables = a.freeTables[:last]
2✔
112

2✔
113
        return table, true
2✔
114
}
115

116
func (a *archetype) FreeTable(table tableID) {
13✔
117
        // TODO: can we speed this up for large numbers of relation targets?
13✔
118
        index := slices.Index(a.tables, table)
13✔
119
        last := len(a.tables) - 1
13✔
120

13✔
121
        if index != last {
18✔
122
                a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
5✔
123
        }
5✔
124
        a.tables = a.tables[:last]
13✔
125

13✔
126
        a.freeTables = append(a.freeTables, table)
13✔
127
}
128

129
func (a *archetype) AddTable(table *table) {
491✔
130
        a.tables = append(a.tables, table.id)
491✔
131
        if !a.HasRelations() {
852✔
132
                return
361✔
133
        }
361✔
134

135
        for i := range table.ids {
611✔
136
                column := &table.columns[i]
481✔
137
                if !column.isRelation {
821✔
138
                        continue
340✔
139
                }
140
                target := column.target
141✔
141
                relations := a.relationTables[i]
141✔
142

141✔
143
                if tables, ok := relations[target.id]; ok {
147✔
144
                        tables.tables = append(tables.tables, table.id)
6✔
145
                } else {
141✔
146
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
135✔
147
                }
135✔
148
        }
149
}
150

151
func (a *archetype) RemoveTarget(entity Entity) {
16✔
152
        for i := range a.relationTables {
43✔
153
                if !a.isRelation[i] {
35✔
154
                        continue
8✔
155
                }
156
                delete(a.relationTables[i], entity.id)
19✔
157
        }
158
}
159

160
func (a *archetype) Reset(storage *storage) {
4✔
161
        if !a.HasRelations() {
7✔
162
                storage.tables[a.tables[0]].Reset()
3✔
163
                return
3✔
164
        }
3✔
165

166
        for _, tab := range a.tables {
2✔
167
                table := &storage.tables[tab]
1✔
168
                table.Reset()
1✔
169
        }
1✔
170

171
        for i := len(a.tables) - 1; i >= 0; i-- {
2✔
172
                storage.cache.removeTable(storage, &storage.tables[a.tables[i]])
1✔
173
                a.FreeTable(a.tables[i])
1✔
174
        }
1✔
175

176
        for _, m := range a.relationTables {
3✔
177
                for key := range m {
3✔
178
                        delete(m, key)
1✔
179
                }
1✔
180
        }
181
}
182

183
// Stats generates statistics for an archetype.
184
func (a *archetype) Stats(storage *storage) stats.Archetype {
4✔
185
        ids := a.components
4✔
186
        aCompCount := len(ids)
4✔
187
        aTypes := make([]reflect.Type, aCompCount)
4✔
188
        for j, id := range ids {
12✔
189
                aTypes[j], _ = storage.registry.ComponentType(id.id)
8✔
190
        }
8✔
191

192
        cap := 0
4✔
193
        count := 0
4✔
194
        memory := 0
4✔
195
        var tableStats []stats.Table
4✔
196
        if a.tables != nil {
8✔
197
                tableStats = make([]stats.Table, len(a.tables))
4✔
198
                for i, id := range a.tables {
9✔
199
                        table := &storage.tables[id]
5✔
200
                        tableStats[i] = table.Stats(&storage.registry)
5✔
201
                        stats := &tableStats[i]
5✔
202
                        cap += stats.Capacity
5✔
203
                        count += stats.Size
5✔
204
                        memory += stats.Memory
5✔
205
                }
5✔
206
        }
207

208
        memPerEntity := 0
4✔
209
        intIDs := make([]uint8, len(ids))
4✔
210
        for j, id := range ids {
12✔
211
                intIDs[j] = id.id
8✔
212
                memPerEntity += int(aTypes[j].Size())
8✔
213
        }
8✔
214

215
        return stats.Archetype{
4✔
216
                FreeTables:      len(a.freeTables),
4✔
217
                NumRelations:    int(a.numRelations),
4✔
218
                Components:      aCompCount,
4✔
219
                ComponentIDs:    intIDs,
4✔
220
                ComponentTypes:  aTypes,
4✔
221
                Memory:          memory,
4✔
222
                MemoryPerEntity: memPerEntity,
4✔
223
                Size:            count,
4✔
224
                Capacity:        cap,
4✔
225
                Tables:          tableStats,
4✔
226
        }
4✔
227
}
228

229
// UpdateStats updates statistics for an archetype.
230
func (a *archetype) UpdateStats(stats *stats.Archetype, storage *storage) {
7✔
231
        arches := a.tables
7✔
232

7✔
233
        cap := 0
7✔
234
        count := 0
7✔
235
        memory := 0
7✔
236

7✔
237
        cntOld := int32(len(stats.Tables))
7✔
238
        cntNew := int32(len(arches))
7✔
239
        if cntNew < cntOld {
8✔
240
                stats.Tables = stats.Tables[:cntNew]
1✔
241
                cntOld = cntNew
1✔
242
        }
1✔
243
        var i int32
7✔
244
        for i = 0; i < cntOld; i++ {
14✔
245
                tableStats := &stats.Tables[i]
7✔
246
                table := &storage.tables[arches[i]]
7✔
247
                table.UpdateStats(stats, tableStats, &storage.registry)
7✔
248
                cap += tableStats.Capacity
7✔
249
                count += tableStats.Size
7✔
250
                memory += tableStats.Memory
7✔
251
        }
7✔
252
        for i = cntOld; i < cntNew; i++ {
8✔
253
                table := &storage.tables[arches[i]]
1✔
254
                tableStats := table.Stats(&storage.registry)
1✔
255
                stats.Tables = append(stats.Tables, tableStats)
1✔
256
                cap += tableStats.Capacity
1✔
257
                count += tableStats.Size
1✔
258
                memory += tableStats.Memory
1✔
259
        }
1✔
260

261
        stats.FreeTables = len(a.freeTables)
7✔
262
        stats.Capacity = cap
7✔
263
        stats.Size = count
7✔
264
        stats.Memory = memory
7✔
265
}
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