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

mlange-42 / arche / 12455637666

22 Dec 2024 03:48PM CUT coverage: 100.0%. Remained the same
12455637666

Pull #442

github

web-flow
Merge dcf4cea1c into 30c1f4bc4
Pull Request #442: Update CHANGELOG for release v0.14.0

6411 of 6411 relevant lines covered (100.0%)

114030.79 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
        neighbors         idMap[*archNode] // Mapping from component ID to add/remove, to the resulting archetype
30
        capacityIncrement uint32           // Capacity increment
31
}
32

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

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

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

59
        var zeroValue []byte
1,392✔
60
        var zeroPointer unsafe.Pointer
1,392✔
61
        if maxSize > 0 {
2,624✔
62
                zeroValue = make([]byte, maxSize)
1,232✔
63
                zeroPointer = unsafe.Pointer(&zeroValue[0])
1,232✔
64
        }
1,232✔
65

66
        data.Ids = ids
1,392✔
67
        data.Types = types
1,392✔
68
        data.archetypeMap = arch
1,392✔
69
        data.capacityIncrement = uint32(capacityIncrement)
1,392✔
70
        data.zeroValue = zeroValue
1,392✔
71
        data.zeroPointer = zeroPointer
1,392✔
72
        data.neighbors = newIDMap[*archNode]()
1,392✔
73

1,392✔
74
        return archNode{
1,392✔
75
                nodeData:    data,
1,392✔
76
                Mask:        mask,
1,392✔
77
                Relation:    relation,
1,392✔
78
                HasRelation: hasRelation,
1,392✔
79
        }
1,392✔
80
}
81

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

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

98
// GetArchetype returns the archetype for the given relation target.
99
//
100
// The target is ignored if the node has no relation component.
101
func (a *archNode) GetArchetype(target Entity) (*archetype, bool) {
1,035,689✔
102
        if a.HasRelation {
1,068,746✔
103
                arch, ok := a.archetypeMap[target]
33,057✔
104
                return arch, ok
33,057✔
105
        }
33,057✔
106
        return a.archetype, a.archetype != nil
1,002,632✔
107
}
108

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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