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

mlange-42 / arche / 4903779843

06 May 2023 09:50PM CUT coverage: 100.0%. Remained the same
4903779843

push

github

GitHub
Optimize archetype iteration (#272)

38 of 38 new or added lines in 3 files covered. (100.0%)

3719 of 3719 relevant lines covered (100.0%)

85808.07 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,274✔
35
        var arch map[Entity]*archetype
1,274✔
36
        if relation >= 0 {
1,301✔
37
                arch = map[Entity]*archetype{}
27✔
38
        }
27✔
39
        ids := make([]ID, len(components))
1,274✔
40
        types := make([]reflect.Type, len(components))
1,274✔
41

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

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

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

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

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

83
// Matches the archetype node against a filter.
84
// Ignores the relation target.
85
func (a *archNode) Matches(f Filter) bool {
76,873✔
86
        return f.Matches(a.Mask, nil)
76,873✔
87
}
76,873✔
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 {
151,807✔
93
        if a.archetype == nil {
202,032✔
94
                return &a.archetypes
50,225✔
95
        }
50,225✔
96
        return singleArchetype{Archetype: a.archetype}
101,582✔
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,764✔
103
        if a.Relation >= 0 {
93,767✔
104
                return a.archetypeMap[target]
33,003✔
105
        }
33,003✔
106
        return a.archetype
27,761✔
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,207✔
113
        a.archetype = arch
1,207✔
114
}
1,207✔
115

116
// CreateArchetype creates a new archetype in nodes with relation component.
117
func (a *archNode) CreateArchetype(target Entity) *archetype {
27,573✔
118
        var arch *archetype
27,573✔
119
        var archIndex int32
27,573✔
120
        lenFree := len(a.freeIndices)
27,573✔
121
        if lenFree > 0 {
54,975✔
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,573✔
127
                a.archetypes.Add(archetype{})
171✔
128
                archIndex := a.archetypes.Len() - 1
171✔
129
                arch = a.archetypes.Get(archIndex)
171✔
130
                arch.Init(a, archIndex, true, target)
171✔
131
        }
171✔
132
        a.archetypeMap[target] = arch
27,573✔
133
        return arch
27,573✔
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,413✔
139
        delete(a.archetypeMap, arch.RelationTarget)
27,413✔
140
        idx := arch.index
27,413✔
141
        a.freeIndices = append(a.freeIndices, idx)
27,413✔
142
        a.archetypes.Get(idx).Deactivate()
27,413✔
143
}
27,413✔
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