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

mlange-42 / arche / 10613042955

29 Aug 2024 10:10AM CUT coverage: 100.0% (+0.02%) from 99.984%
10613042955

push

github

web-flow
Add a test for the listener bug fixed in #424 (#425)

6380 of 6380 relevant lines covered (100.0%)

76337.14 hits per line

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

100.0
/ecs/archetype_node.go
1
package ecs
2

3
import (
4
        "reflect"
5
        "unsafe"
6

7
        "github.com/mlange-42/arche/ecs/stats"
8
)
9

10
// archNode is a node in the archetype graph.
11
type archNode struct {
12
        *nodeData
13
        Mask        Mask // Mask of the archetype
14
        Relation    ID
15
        HasRelation bool
16
        IsActive    bool
17
}
18

19
type nodeData struct {
20
        archetype         *archetype            // The single archetype for nodes without entity relation
21
        archetypeMap      map[Entity]*archetype // Mapping from relation targets to archetypes
22
        zeroPointer       unsafe.Pointer        // Points to zeroValue for fast access
23
        Types             []reflect.Type        // Component type per column
24
        Ids               []ID                  // List of component IDs
25
        freeIndices       []int32               // Indices of free/inactive archetypes
26
        zeroValue         []byte                // Used as source for setting storage to zero
27
        archetypes        pagedSlice[archetype] // Storage for archetypes in nodes with entity relation
28
        archetypeData     pagedSlice[archetypeData]
29
        TransitionAdd     idMap[*archNode] // Mapping from component ID to add to the resulting archetype
30
        TransitionRemove  idMap[*archNode] // Mapping from component ID to remove to the resulting archetype
31
        capacityIncrement uint32           // Capacity increment
32
}
33

34
// Creates a new archNode
35
func newArchNode(mask Mask, data *nodeData, relation ID, hasRelation bool, capacityIncrement int, components []componentType) archNode {
1,387✔
36
        var arch map[Entity]*archetype
1,387✔
37
        if hasRelation {
1,441✔
38
                arch = map[Entity]*archetype{}
54✔
39
        }
54✔
40
        ids := make([]ID, len(components))
1,387✔
41
        types := make([]reflect.Type, len(components))
1,387✔
42

1,387✔
43
        var maxSize uintptr = 0
1,387✔
44
        prev := -1
1,387✔
45
        for i, c := range components {
6,854✔
46
                if int(c.ID.id) <= prev {
5,468✔
47
                        panic("component arguments must be sorted by ID")
1✔
48
                }
49
                prev = int(c.ID.id)
5,466✔
50

5,466✔
51
                ids[i] = c.ID
5,466✔
52
                types[i] = c.Type
5,466✔
53
                size, align := c.Type.Size(), uintptr(c.Type.Align())
5,466✔
54
                size = (size + (align - 1)) / align * align
5,466✔
55
                if size > maxSize {
6,702✔
56
                        maxSize = size
1,236✔
57
                }
1,236✔
58
        }
59

60
        var zeroValue []byte
1,386✔
61
        var zeroPointer unsafe.Pointer
1,386✔
62
        if maxSize > 0 {
2,614✔
63
                zeroValue = make([]byte, maxSize)
1,228✔
64
                zeroPointer = unsafe.Pointer(&zeroValue[0])
1,228✔
65
        }
1,228✔
66

67
        data.Ids = ids
1,386✔
68
        data.Types = types
1,386✔
69
        data.archetypeMap = arch
1,386✔
70
        data.capacityIncrement = uint32(capacityIncrement)
1,386✔
71
        data.zeroValue = zeroValue
1,386✔
72
        data.zeroPointer = zeroPointer
1,386✔
73
        data.TransitionAdd = newIDMap[*archNode]()
1,386✔
74
        data.TransitionRemove = newIDMap[*archNode]()
1,386✔
75

1,386✔
76
        return archNode{
1,386✔
77
                nodeData:    data,
1,386✔
78
                Mask:        mask,
1,386✔
79
                Relation:    relation,
1,386✔
80
                HasRelation: hasRelation,
1,386✔
81
        }
1,386✔
82
}
83

84
// Matches the archetype node against a filter.
85
// Ignores the relation target.
86
func (a *archNode) Matches(f Filter) bool {
79,551✔
87
        return f.Matches(&a.Mask)
79,551✔
88
}
79,551✔
89

90
// Archetypes of the node.
91
// Returns a single wrapped archetype if there are no relations.
92
// Returns nil if the node has no archetype(s).
93
func (a *archNode) Archetypes() archetypes {
153,079✔
94
        if a.archetype == nil {
203,377✔
95
                return &a.archetypes
50,298✔
96
        }
50,298✔
97
        return singleArchetype{Archetype: a.archetype}
102,781✔
98
}
99

100
// GetArchetype returns the archetype for the given relation target.
101
//
102
// The target is ignored if the node has no relation component.
103
func (a *archNode) GetArchetype(target Entity) *archetype {
552,464✔
104
        if a.HasRelation {
585,521✔
105
                return a.archetypeMap[target]
33,057✔
106
        }
33,057✔
107
        return a.archetype
519,407✔
108
}
109

110
// SetArchetype sets the archetype for a node without a relation.
111
//
112
// Do not use on nodes without a relation component!
113
func (a *archNode) SetArchetype(arch *archetype) {
1,288✔
114
        a.archetype = arch
1,288✔
115
}
1,288✔
116

117
// CreateArchetype creates a new archetype in nodes with relation component.
118
func (a *archNode) CreateArchetype(layouts uint8, target Entity) *archetype {
27,616✔
119
        var arch *archetype
27,616✔
120
        var archIndex int32
27,616✔
121
        lenFree := len(a.freeIndices)
27,616✔
122
        if lenFree > 0 {
55,020✔
123
                archIndex = a.freeIndices[lenFree-1]
27,404✔
124
                arch = a.archetypes.Get(archIndex)
27,404✔
125
                a.freeIndices = a.freeIndices[:lenFree-1]
27,404✔
126
                arch.Activate(target, archIndex)
27,404✔
127
        } else {
27,616✔
128
                a.archetypes.Add(archetype{})
212✔
129
                a.archetypeData.Add(archetypeData{})
212✔
130
                archIndex := a.archetypes.Len() - 1
212✔
131
                arch = a.archetypes.Get(archIndex)
212✔
132
                arch.Init(a, a.archetypeData.Get(archIndex), archIndex, true, layouts, target)
212✔
133
        }
212✔
134
        a.archetypeMap[target] = arch
27,616✔
135
        return arch
27,616✔
136
}
137

138
func (a *archNode) ExtendArchetypeLayouts(count uint8) {
9✔
139
        if !a.IsActive {
10✔
140
                return
1✔
141
        }
1✔
142

143
        if !a.HasRelation {
14✔
144
                a.archetype.ExtendLayouts(count)
6✔
145
                return
6✔
146
        }
6✔
147

148
        lenArches := a.archetypes.Len()
2✔
149
        var j int32
2✔
150
        for j = 0; j < lenArches; j++ {
4✔
151
                arch := a.archetypes.Get(j)
2✔
152
                arch.ExtendLayouts(count)
2✔
153
        }
2✔
154
}
155

156
// RemoveArchetype de-activates an archetype.
157
// The archetype will be re-used by CreateArchetype.
158
func (a *archNode) RemoveArchetype(arch *archetype) {
27,427✔
159
        delete(a.archetypeMap, arch.RelationTarget)
27,427✔
160
        idx := arch.index
27,427✔
161
        a.freeIndices = append(a.freeIndices, idx)
27,427✔
162
        a.archetypes.Get(idx).Deactivate()
27,427✔
163
}
27,427✔
164

165
// Reset resets the archetypes in this node.
166
// Relation archetypes with non-zero target are de-activated for re-use.
167
func (a *archNode) Reset(cache *Cache) {
97✔
168
        if !a.IsActive {
102✔
169
                return
5✔
170
        }
5✔
171
        if !a.HasRelation {
156✔
172
                a.archetype.Reset()
64✔
173
                return
64✔
174
        }
64✔
175

176
        lenArches := a.archetypes.Len()
28✔
177
        var j int32
28✔
178
        for j = 0; j < lenArches; j++ {
2,439✔
179
                arch := a.archetypes.Get(j)
2,411✔
180
                if !arch.IsActive() {
2,413✔
181
                        continue
2✔
182
                }
183
                if !arch.RelationTarget.IsZero() {
4,816✔
184
                        a.RemoveArchetype(arch)
2,407✔
185
                        cache.removeArchetype(arch)
2,407✔
186
                } else {
2,409✔
187
                        arch.Reset()
2✔
188
                }
2✔
189
        }
190
}
191

192
// Stats generates statistics for an archetype node.
193
func (a *archNode) Stats(reg *componentRegistry) stats.Node {
13✔
194
        ids := a.Ids
13✔
195
        aCompCount := len(ids)
13✔
196
        aTypes := make([]reflect.Type, aCompCount)
13✔
197
        for j, id := range ids {
24✔
198
                aTypes[j], _ = reg.ComponentType(id.id)
11✔
199
        }
11✔
200

201
        arches := a.Archetypes()
13✔
202
        var numArches int32
13✔
203
        cap := 0
13✔
204
        count := 0
13✔
205
        memory := 0
13✔
206
        var archStats []stats.Archetype
13✔
207
        if arches != nil {
26✔
208
                numArches = arches.Len()
13✔
209
                archStats = make([]stats.Archetype, numArches)
13✔
210
                var i int32
13✔
211
                for i = 0; i < numArches; i++ {
125✔
212
                        archStats[i] = arches.Get(i).Stats(reg)
112✔
213
                        stats := &archStats[i]
112✔
214
                        cap += stats.Capacity
112✔
215
                        count += stats.Size
112✔
216
                        memory += stats.Memory
112✔
217
                }
112✔
218
        }
219

220
        memPerEntity := 0
13✔
221
        intIDs := make([]uint8, len(ids))
13✔
222
        for j, id := range ids {
24✔
223
                intIDs[j] = id.id
11✔
224
                memPerEntity += int(aTypes[j].Size())
11✔
225
        }
11✔
226

227
        return stats.Node{
13✔
228
                ArchetypeCount:       int(numArches),
13✔
229
                ActiveArchetypeCount: int(numArches) - len(a.freeIndices),
13✔
230
                IsActive:             a.IsActive,
13✔
231
                HasRelation:          a.HasRelation,
13✔
232
                Components:           aCompCount,
13✔
233
                ComponentIDs:         intIDs,
13✔
234
                ComponentTypes:       aTypes,
13✔
235
                Memory:               memory,
13✔
236
                MemoryPerEntity:      memPerEntity,
13✔
237
                Size:                 count,
13✔
238
                Capacity:             cap,
13✔
239
                Archetypes:           archStats,
13✔
240
        }
13✔
241
}
242

243
// UpdateStats updates statistics for an archetype node.
244
func (a *archNode) UpdateStats(stats *stats.Node, reg *componentRegistry) {
150,022✔
245
        if !a.IsActive {
150,023✔
246
                return
1✔
247
        }
1✔
248

249
        arches := a.Archetypes()
150,021✔
250

150,021✔
251
        if !stats.IsActive {
150,022✔
252
                temp := a.Stats(reg)
1✔
253
                *stats = temp
1✔
254
                return
1✔
255
        }
1✔
256

257
        cap := 0
150,020✔
258
        count := 0
150,020✔
259
        memory := 0
150,020✔
260

150,020✔
261
        cntOld := int32(len(stats.Archetypes))
150,020✔
262
        cntNew := int32(arches.Len())
150,020✔
263
        var i int32
150,020✔
264
        for i = 0; i < cntOld; i++ {
5,249,944✔
265
                arch := &stats.Archetypes[i]
5,099,924✔
266
                arches.Get(i).UpdateStats(stats, arch, reg)
5,099,924✔
267
                cap += arch.Capacity
5,099,924✔
268
                count += arch.Size
5,099,924✔
269
                memory += arch.Memory
5,099,924✔
270
        }
5,099,924✔
271
        for i = cntOld; i < cntNew; i++ {
150,021✔
272
                arch := arches.Get(i).Stats(reg)
1✔
273
                stats.Archetypes = append(stats.Archetypes, arch)
1✔
274
                cap += arch.Capacity
1✔
275
                count += arch.Size
1✔
276
                memory += arch.Memory
1✔
277
        }
1✔
278

279
        stats.IsActive = true
150,020✔
280
        stats.ArchetypeCount = int(cntNew)
150,020✔
281
        stats.ActiveArchetypeCount = int(cntNew) - len(a.freeIndices)
150,020✔
282
        stats.Capacity = cap
150,020✔
283
        stats.Size = count
150,020✔
284
        stats.Memory = memory
150,020✔
285
}
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