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

mlange-42 / ark / 13766400725

10 Mar 2025 01:54PM CUT coverage: 99.376% (-0.06%) from 99.436%
13766400725

Pull #177

github

web-flow
Merge a2655a9e2 into 3d8797a1a
Pull Request #177: Separate reserved/used memory in world stats

63 of 66 new or added lines in 4 files covered. (95.45%)

1 existing line in 1 file now uncovered.

6532 of 6573 relevant lines covered (99.38%)

27605.42 hits per line

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

97.64
/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 {
622✔
35
        componentsMap := make([]int16, maskTotalBits)
622✔
36
        for i := range maskTotalBits {
159,854✔
37
                componentsMap[i] = -1
159,232✔
38
        }
159,232✔
39
        sizes := make([]uint32, len(components))
622✔
40
        for i, id := range components {
2,085✔
41
                componentsMap[id.id] = int16(i)
1,463✔
42
                tp := reg.Types[id.id]
1,463✔
43
                sizes[i] = uint32(sizeOf(tp))
1,463✔
44
        }
1,463✔
45

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

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

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

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

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

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

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

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

13✔
126
        if index != last {
18✔
127
                a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
5✔
128
        }
5✔
129
        a.tables = a.tables[:last]
13✔
130

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

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

140
        for i := range table.ids {
611✔
141
                column := &table.columns[i]
481✔
142
                if !column.isRelation {
821✔
143
                        continue
340✔
144
                }
145
                target := column.target
141✔
146
                relations := a.relationTables[i]
141✔
147

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

156
func (a *archetype) RemoveTarget(entity Entity) {
16✔
157
        for i := range a.relationTables {
43✔
158
                if !a.isRelation[i] {
35✔
159
                        continue
8✔
160
                }
161
                delete(a.relationTables[i], entity.id)
19✔
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
        aCompCount := len(ids)
4✔
192
        aTypes := make([]reflect.Type, aCompCount)
4✔
193
        for j, id := range ids {
12✔
194
                aTypes[j], _ = storage.registry.ComponentType(id.id)
8✔
195
        }
8✔
196

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

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

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

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

7✔
243
        cap := 0
7✔
244
        count := 0
7✔
245
        memory := 0
7✔
246
        memoryUsed := 0
7✔
247

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

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