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

mlange-42 / ark / 13847606158

14 Mar 2025 12:57AM CUT coverage: 99.786%. Remained the same
13847606158

Pull #196

github

web-flow
Merge 139e7446b into 1aa263326
Pull Request #196: Add method `stats.Table.String`

8374 of 8392 relevant lines covered (99.79%)

21281.8 hits per line

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

99.52
/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
        itemSizes      []uint32                 // item size per component ID
22
        componentsMap  []int16                  // mapping from component IDs to column indices; -1 indicates none
23
        isRelation     []bool                   // whether columns are relations components, indexed by column index
24
        relationTables []map[entityID]*tableIDs // lookup for relation targets of tables, indexed by column index
25
        tables         []tableID                // all active tables
26
        freeTables     []tableID                // all inactive/free tables
27
        numRelations   uint8                    // number of relation components
28
}
29

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

34
func newArchetype(id archetypeID, node nodeID, mask *bitMask, components []ID, tables []tableID, reg *componentRegistry) archetype {
736✔
35
        componentsMap := make([]int16, maskTotalBits)
736✔
36
        for i := range maskTotalBits {
189,152✔
37
                componentsMap[i] = -1
188,416✔
38
        }
188,416✔
39
        sizes := make([]uint32, len(components))
736✔
40
        for i, id := range components {
2,681✔
41
                componentsMap[id.id] = int16(i)
1,945✔
42
                tp := reg.Types[id.id]
1,945✔
43
                sizes[i] = uint32(sizeOf(tp))
1,945✔
44
        }
1,945✔
45

46
        numRelations := uint8(0)
736✔
47
        isRelation := make([]bool, len(components))
736✔
48
        relationTables := make([]map[entityID]*tableIDs, len(components))
736✔
49
        for i, id := range components {
2,681✔
50
                if reg.IsRelation[id.id] {
2,008✔
51
                        isRelation[i] = true
63✔
52
                        relationTables[i] = map[entityID]*tableIDs{}
63✔
53
                        numRelations++
63✔
54
                }
63✔
55
        }
56
        return archetype{
736✔
57
                id:             id,
736✔
58
                node:           node,
736✔
59
                mask:           *mask,
736✔
60
                components:     components,
736✔
61
                itemSizes:      sizes,
736✔
62
                componentsMap:  componentsMap,
736✔
63
                isRelation:     isRelation,
736✔
64
                tables:         tables,
736✔
65
                numRelations:   numRelations,
736✔
66
                relationTables: relationTables,
736✔
67
        }
736✔
68
}
69

70
func (a *archetype) HasRelations() bool {
500,283✔
71
        return a.numRelations > 0
500,283✔
72
}
500,283✔
73

74
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
497,277✔
75
        if len(a.tables) == 0 {
497,759✔
76
                return nil, false
482✔
77
        }
482✔
78
        if !a.HasRelations() {
992,900✔
79
                return &storage.tables[a.tables[0]], true
496,105✔
80
        }
496,105✔
81
        if len(relations) != int(a.numRelations) {
691✔
82
                panic("relations must be fully specified")
1✔
83
        }
84
        index := a.componentsMap[relations[0].component.id]
689✔
85
        tables, ok := a.relationTables[index][relations[0].target.id]
689✔
86
        if !ok {
780✔
87
                return nil, false
91✔
88
        }
91✔
89
        for _, t := range tables.tables {
1,234✔
90
                table := &storage.tables[t]
636✔
91
                if table.MatchesExact(relations) {
1,231✔
92
                        return table, true
595✔
93
                }
595✔
94
        }
95
        return nil, false
3✔
96
}
97

98
func (a *archetype) GetTables(relations []RelationID) []tableID {
133✔
99
        if !a.HasRelations() || len(relations) == 0 {
179✔
100
                return a.tables
46✔
101
        }
46✔
102
        index := a.componentsMap[relations[0].component.id]
87✔
103
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
174✔
104
                return tables.tables
87✔
105
        }
87✔
106
        return nil
×
107
}
108

109
func (a *archetype) GetFreeTable() (tableID, bool) {
576✔
110
        if len(a.freeTables) == 0 {
1,148✔
111
                return 0, false
572✔
112
        }
572✔
113
        last := len(a.freeTables) - 1
4✔
114
        table := a.freeTables[last]
4✔
115

4✔
116
        a.freeTables = a.freeTables[:last]
4✔
117

4✔
118
        return table, true
4✔
119
}
120

121
func (a *archetype) FreeTable(table tableID) {
15✔
122
        // TODO: can we speed this up for large numbers of relation targets?
15✔
123
        index := slices.Index(a.tables, table)
15✔
124
        last := len(a.tables) - 1
15✔
125

15✔
126
        if index != last {
21✔
127
                a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
6✔
128
        }
6✔
129
        a.tables = a.tables[:last]
15✔
130

15✔
131
        a.freeTables = append(a.freeTables, table)
15✔
132
}
133

134
func (a *archetype) AddTable(table *table) {
576✔
135
        a.tables = append(a.tables, table.id)
576✔
136
        if !a.HasRelations() {
999✔
137
                return
423✔
138
        }
423✔
139

140
        for i := range table.ids {
853✔
141
                column := &table.columns[i]
700✔
142
                if !column.isRelation {
1,233✔
143
                        continue
533✔
144
                }
145
                target := column.target
167✔
146
                relations := a.relationTables[i]
167✔
147

167✔
148
                if tables, ok := relations[target.id]; ok {
175✔
149
                        tables.tables = append(tables.tables, table.id)
8✔
150
                } else {
167✔
151
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
159✔
152
                }
159✔
153
        }
154
}
155

156
func (a *archetype) RemoveTarget(entity Entity) {
19✔
157
        for i := range a.relationTables {
55✔
158
                if !a.isRelation[i] {
47✔
159
                        continue
11✔
160
                }
161
                delete(a.relationTables[i], entity.id)
25✔
162
        }
163
}
164

165
func (a *archetype) Reset(storage *storage) {
4✔
166
        if !a.HasRelations() {
7✔
167
                storage.tables[a.tables[0]].Reset()
3✔
168
                return
3✔
169
        }
3✔
170

171
        for _, tab := range a.tables {
2✔
172
                table := &storage.tables[tab]
1✔
173
                table.Reset()
1✔
174
        }
1✔
175

176
        for i := len(a.tables) - 1; i >= 0; i-- {
2✔
177
                storage.cache.removeTable(storage, &storage.tables[a.tables[i]])
1✔
178
                a.FreeTable(a.tables[i])
1✔
179
        }
1✔
180

181
        for _, m := range a.relationTables {
3✔
182
                for key := range m {
3✔
183
                        delete(m, key)
1✔
184
                }
1✔
185
        }
186
}
187

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

196
        memPerEntity := int(entitySize)
4✔
197
        intIDs := make([]uint8, len(ids))
4✔
198
        for j, id := range ids {
12✔
199
                intIDs[j] = id.id
8✔
200
                memPerEntity += int(a.itemSizes[j])
8✔
201
        }
8✔
202

203
        cap := 0
4✔
204
        count := 0
4✔
205
        memory := 0
4✔
206
        memoryUsed := 0
4✔
207
        tableStats := make([]stats.Table, len(a.tables))
4✔
208
        for i, id := range a.tables {
7✔
209
                table := &storage.tables[id]
3✔
210
                tableStats[i] = table.Stats(memPerEntity, &storage.registry)
3✔
211
                stats := &tableStats[i]
3✔
212
                cap += stats.Capacity
3✔
213
                count += stats.Size
3✔
214
                memory += stats.Memory
3✔
215
                memoryUsed += stats.MemoryUsed
3✔
216
        }
3✔
217
        for _, id := range a.freeTables {
6✔
218
                table := &storage.tables[id]
2✔
219
                cap += int(table.cap)
2✔
220
                memory += memPerEntity * int(table.cap)
2✔
221
        }
2✔
222

223
        return stats.Archetype{
4✔
224
                FreeTables:      len(a.freeTables),
4✔
225
                NumRelations:    int(a.numRelations),
4✔
226
                ComponentIDs:    intIDs,
4✔
227
                ComponentTypes:  aTypes,
4✔
228
                Memory:          memory,
4✔
229
                MemoryUsed:      memoryUsed,
4✔
230
                MemoryPerEntity: memPerEntity,
4✔
231
                Size:            count,
4✔
232
                Capacity:        cap,
4✔
233
                Tables:          tableStats,
4✔
234
        }
4✔
235
}
236

237
// UpdateStats updates statistics for an archetype.
238
func (a *archetype) UpdateStats(stats *stats.Archetype, storage *storage) {
11✔
239
        tables := a.tables
11✔
240

11✔
241
        cap := 0
11✔
242
        count := 0
11✔
243
        memory := 0
11✔
244
        memoryUsed := 0
11✔
245

11✔
246
        cntOld := int32(len(stats.Tables))
11✔
247
        cntNew := int32(len(tables))
11✔
248
        if cntNew < cntOld {
12✔
249
                stats.Tables = stats.Tables[:cntNew]
1✔
250
                cntOld = cntNew
1✔
251
        }
1✔
252
        var i int32
11✔
253
        for i = 0; i < cntOld; i++ {
22✔
254
                tableStats := &stats.Tables[i]
11✔
255
                table := &storage.tables[tables[i]]
11✔
256
                table.UpdateStats(stats.MemoryPerEntity, tableStats, &storage.registry)
11✔
257
                cap += tableStats.Capacity
11✔
258
                count += tableStats.Size
11✔
259
                memory += tableStats.Memory
11✔
260
                memoryUsed += tableStats.MemoryUsed
11✔
261
        }
11✔
262
        for i = cntOld; i < cntNew; i++ {
14✔
263
                table := &storage.tables[tables[i]]
3✔
264
                tableStats := table.Stats(stats.MemoryPerEntity, &storage.registry)
3✔
265
                stats.Tables = append(stats.Tables, tableStats)
3✔
266
                cap += tableStats.Capacity
3✔
267
                count += tableStats.Size
3✔
268
                memory += tableStats.Memory
3✔
269
                memoryUsed += tableStats.MemoryUsed
3✔
270
        }
3✔
271
        for _, id := range a.freeTables {
14✔
272
                table := &storage.tables[id]
3✔
273
                cap += int(table.cap)
3✔
274
                memory += stats.MemoryPerEntity * int(table.cap)
3✔
275
        }
3✔
276

277
        stats.FreeTables = len(a.freeTables)
11✔
278
        stats.Capacity = cap
11✔
279
        stats.Size = count
11✔
280
        stats.Memory = memory
11✔
281
        stats.MemoryUsed = memoryUsed
11✔
282
}
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