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

mlange-42 / arche / 4913601982

08 May 2023 09:26AM CUT coverage: 100.0%. Remained the same
4913601982

push

github

GitHub
Restructure query iteration code to be more compiler-friendly (#279)

16 of 16 new or added lines in 1 file covered. (100.0%)

3759 of 3759 relevant lines covered (100.0%)

84756.8 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
        TransitionAdd    idMap[*archNode] // Mapping from component ID to add to the resulting archetype
15
        TransitionRemove idMap[*archNode] // Mapping from component ID to remove to the resulting archetype
16
        Relation         int8             // The node's relation component ID. Negative value stands for no relation
17
        IsActive         bool
18
        HasRelation      bool
19
}
20

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

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

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

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

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

66
        data.Ids = ids
1,279✔
67
        data.Types = types
1,279✔
68
        data.archetypeMap = arch
1,279✔
69
        data.capacityIncrement = uint32(capacityIncrement)
1,279✔
70
        data.zeroValue = zeroValue
1,279✔
71
        data.zeroPointer = zeroPointer
1,279✔
72

1,279✔
73
        return archNode{
1,279✔
74
                nodeData:         data,
1,279✔
75
                Mask:             mask,
1,279✔
76
                TransitionAdd:    newIDMap[*archNode](),
1,279✔
77
                TransitionRemove: newIDMap[*archNode](),
1,279✔
78
                Relation:         relation,
1,279✔
79
                HasRelation:      relation >= 0,
1,279✔
80
        }
1,279✔
81
}
82

83
// Matches the archetype node against a filter.
84
// Ignores the relation target.
85
func (a *archNode) Matches(f Filter) bool {
76,751✔
86
        return f.Matches(a.Mask, nil)
76,751✔
87
}
76,751✔
88

89
// Archetypes of the node.
90
// Returns a single wrapped archetype if there are no relations.
91
// Returns nil if the node has no archetype(s).
92
func (a *archNode) Archetypes() archetypes {
150,674✔
93
        if a.archetype == nil {
200,763✔
94
                return &a.archetypes
50,089✔
95
        }
50,089✔
96
        return singleArchetype{Archetype: a.archetype}
100,585✔
97
}
98

99
// GetArchetype returns the archetype for the given relation target.
100
//
101
// The target is ignored if the node has no relation component.
102
func (a *archNode) GetArchetype(target Entity) *archetype {
60,769✔
103
        if a.Relation >= 0 {
93,774✔
104
                return a.archetypeMap[target]
33,005✔
105
        }
33,005✔
106
        return a.archetype
27,764✔
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,211✔
113
        a.archetype = arch
1,211✔
114
}
1,211✔
115

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

136
// RemoveArchetype de-activates an archetype.
137
// The archetype will be re-used by CreateArchetype.
138
func (a *archNode) RemoveArchetype(arch *archetype) {
27,414✔
139
        delete(a.archetypeMap, arch.RelationTarget)
27,414✔
140
        idx := arch.index
27,414✔
141
        a.freeIndices = append(a.freeIndices, idx)
27,414✔
142
        a.archetypes.Get(idx).Deactivate()
27,414✔
143
}
27,414✔
144

145
// Stats generates statistics for an archetype node.
146
func (a *archNode) Stats(reg *componentRegistry[ID]) stats.NodeStats {
13✔
147
        ids := a.Ids
13✔
148
        aCompCount := len(ids)
13✔
149
        aTypes := make([]reflect.Type, aCompCount)
13✔
150
        for j, id := range ids {
24✔
151
                aTypes[j], _ = reg.ComponentType(id)
11✔
152
        }
11✔
153

154
        arches := a.Archetypes()
13✔
155
        var numArches int32
13✔
156
        cap := 0
13✔
157
        count := 0
13✔
158
        memory := 0
13✔
159
        var archStats []stats.ArchetypeStats
13✔
160
        if arches != nil {
26✔
161
                numArches = arches.Len()
13✔
162
                archStats = make([]stats.ArchetypeStats, numArches)
13✔
163
                var i int32
13✔
164
                for i = 0; i < numArches; i++ {
125✔
165
                        archStats[i] = arches.Get(i).Stats(reg)
112✔
166
                        stats := &archStats[i]
112✔
167
                        cap += stats.Capacity
112✔
168
                        count += stats.Size
112✔
169
                        memory += stats.Memory
112✔
170
                }
112✔
171
        }
172

173
        memPerEntity := 0
13✔
174
        for j := range ids {
24✔
175
                memPerEntity += int(aTypes[j].Size())
11✔
176
        }
11✔
177

178
        return stats.NodeStats{
13✔
179
                ArchetypeCount:       int(numArches),
13✔
180
                ActiveArchetypeCount: int(numArches) - len(a.freeIndices),
13✔
181
                IsActive:             a.IsActive,
13✔
182
                HasRelation:          a.HasRelation,
13✔
183
                Components:           aCompCount,
13✔
184
                ComponentIDs:         ids,
13✔
185
                ComponentTypes:       aTypes,
13✔
186
                Memory:               memory,
13✔
187
                MemoryPerEntity:      memPerEntity,
13✔
188
                Size:                 count,
13✔
189
                Capacity:             cap,
13✔
190
                Archetypes:           archStats,
13✔
191
        }
13✔
192
}
193

194
// UpdateStats updates statistics for an archetype node.
195
func (a *archNode) UpdateStats(stats *stats.NodeStats, reg *componentRegistry[ID]) {
150,022✔
196
        if !a.IsActive {
150,023✔
197
                return
1✔
198
        }
1✔
199

200
        arches := a.Archetypes()
150,021✔
201

150,021✔
202
        if !stats.IsActive {
150,022✔
203
                temp := a.Stats(reg)
1✔
204
                *stats = temp
1✔
205
                return
1✔
206
        }
1✔
207

208
        cap := 0
150,020✔
209
        count := 0
150,020✔
210
        memory := 0
150,020✔
211

150,020✔
212
        cntOld := int32(len(stats.Archetypes))
150,020✔
213
        cntNew := int32(arches.Len())
150,020✔
214
        var i int32
150,020✔
215
        for i = 0; i < cntOld; i++ {
5,249,944✔
216
                arch := &stats.Archetypes[i]
5,099,924✔
217
                arches.Get(i).UpdateStats(stats, arch, reg)
5,099,924✔
218
                cap += arch.Capacity
5,099,924✔
219
                count += arch.Size
5,099,924✔
220
                memory += arch.Memory
5,099,924✔
221
        }
5,099,924✔
222
        for i = cntOld; i < cntNew; i++ {
150,021✔
223
                arch := arches.Get(i).Stats(reg)
1✔
224
                stats.Archetypes = append(stats.Archetypes, arch)
1✔
225
                cap += arch.Capacity
1✔
226
                count += arch.Size
1✔
227
                memory += arch.Memory
1✔
228
        }
1✔
229

230
        stats.IsActive = true
150,020✔
231
        stats.ArchetypeCount = int(cntNew)
150,020✔
232
        stats.ActiveArchetypeCount = int(cntNew) - len(a.freeIndices)
150,020✔
233
        stats.Capacity = cap
150,020✔
234
        stats.Size = count
150,020✔
235
        stats.Memory = memory
150,020✔
236
}
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