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

mlange-42 / ark / 13618795509

02 Mar 2025 07:58PM CUT coverage: 84.737% (-12.6%) from 97.38%
13618795509

Pull #101

github

web-flow
Merge 7affb7efc into 2ca3e2911
Pull Request #101: Implement batch operations on `ExchangeX`

0 of 672 new or added lines in 1 file covered. (0.0%)

374 existing lines in 1 file now uncovered.

4386 of 5176 relevant lines covered (84.74%)

36519.03 hits per line

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

89.5
/ecs/world_internal.go
1
package ecs
2

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

8
func (w *World) newEntityWith(ids []ID, comps []unsafe.Pointer, relations []RelationID) Entity {
503,503✔
9
        w.checkLocked()
503,503✔
10

503,503✔
11
        mask := NewMask(ids...)
503,503✔
12
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
503,503✔
13
        entity, idx := w.storage.createEntity(newTable.id)
503,503✔
14

503,503✔
15
        if comps != nil {
1,006,932✔
16
                if len(ids) != len(comps) {
503,429✔
17
                        panic("lengths of IDs and components to add do not match")
×
18
                }
19
                for i, id := range ids {
1,009,812✔
20
                        newTable.Set(id, idx, comps[i])
506,383✔
21
                }
506,383✔
22
        }
23
        w.storage.registerTargets(relations)
503,503✔
24
        return entity
503,503✔
25
}
26

27
func (w *World) newEntitiesWith(count int, ids []ID, comps []unsafe.Pointer, relations []RelationID) {
8✔
28
        w.checkLocked()
8✔
29

8✔
30
        mask := NewMask(ids...)
8✔
31
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
8✔
32

8✔
33
        startIdx := newTable.Len()
8✔
34
        w.storage.createEntities(newTable, count)
8✔
35

8✔
36
        if comps != nil {
16✔
37
                if len(ids) != len(comps) {
8✔
38
                        panic("lengths of IDs and components to add do not match")
×
39
                }
40
                for i := range count {
200✔
41
                        for j, id := range ids {
1,056✔
42
                                newTable.Set(id, uint32(startIdx+i), comps[j])
864✔
43
                        }
864✔
44
                }
45
        }
46
        w.storage.registerTargets(relations)
8✔
47
}
48

49
func (w *World) newEntities(count int, ids []ID, relations []RelationID) (tableID, int) {
43✔
50
        w.checkLocked()
43✔
51

43✔
52
        mask := NewMask(ids...)
43✔
53
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
43✔
54

43✔
55
        startIdx := newTable.Len()
43✔
56
        w.storage.createEntities(newTable, count)
43✔
57
        w.storage.registerTargets(relations)
43✔
58

43✔
59
        return newTable.id, startIdx
43✔
60
}
43✔
61

62
func (w *World) exchange(entity Entity, add []ID, rem []ID, addComps []unsafe.Pointer, relations []RelationID) {
646✔
63
        w.checkLocked()
646✔
64

646✔
65
        if !w.Alive(entity) {
646✔
66
                panic("can't exchange components on a dead entity")
×
67
        }
68
        if len(add) == 0 && len(rem) == 0 {
646✔
69
                return
×
70
        }
×
71

72
        index := &w.storage.entities[entity.id]
646✔
73
        oldTable := &w.storage.tables[index.table]
646✔
74
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
646✔
75

646✔
76
        mask := oldArchetype.mask
646✔
77
        w.storage.getExchangeMask(&mask, add, rem)
646✔
78

646✔
79
        oldIDs := oldArchetype.components
646✔
80

646✔
81
        newTable := w.storage.findOrCreateTable(oldTable, &mask, relations)
646✔
82
        newIndex := newTable.Add(entity)
646✔
83

646✔
84
        for _, id := range oldIDs {
2,348✔
85
                if mask.Get(id) {
2,414✔
86
                        comp := oldTable.Get(id, uintptr(index.row))
712✔
87
                        newTable.Set(id, newIndex, comp)
712✔
88
                }
712✔
89
        }
90
        if addComps != nil {
973✔
91
                if len(add) != len(addComps) {
327✔
92
                        panic("lengths of IDs and components to add do not match")
×
93
                }
94
                for i, id := range add {
1,046✔
95
                        newTable.Set(id, newIndex, addComps[i])
719✔
96
                }
719✔
97
        }
98

99
        swapped := oldTable.Remove(index.row)
646✔
100

646✔
101
        if swapped {
744✔
102
                swapEntity := oldTable.GetEntity(uintptr(index.row))
98✔
103
                w.storage.entities[swapEntity.id].row = index.row
98✔
104
        }
98✔
105
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
646✔
106

646✔
107
        w.storage.registerTargets(relations)
646✔
108
}
109

110
func (w *World) exchangeBatch(batch *Batch, add []ID, rem []ID,
111
        addComps []unsafe.Pointer, relations []RelationID, fn func(table tableID, start, len int)) int {
32✔
112
        w.checkLocked()
32✔
113

32✔
114
        if len(add) == 0 && len(rem) == 0 {
32✔
115
                if len(relations) > 0 {
×
116
                        panic("exchange operation has no effect, but relations were specified. Use SetRelationBatch instead")
×
117
                }
118
                return 0
×
119
        }
120

121
        tables := w.getTables(batch)
32✔
122
        lengths := make([]uint32, len(tables))
32✔
123
        var totalEntities uint32 = 0
32✔
124
        for i, table := range tables {
96✔
125
                lengths[i] = uint32(table.Len())
64✔
126
                totalEntities += uint32(table.Len())
64✔
127
        }
64✔
128

129
        for i, table := range tables {
96✔
130
                tableLen := lengths[i]
64✔
131

64✔
132
                if tableLen == 0 {
64✔
133
                        continue
×
134
                }
135
                t, start, len := w.exchangeTable(table, int(tableLen), add, rem, addComps, relations)
64✔
136
                if fn != nil {
96✔
137
                        fn(t, start, len)
32✔
138
                }
32✔
139
        }
140

141
        return int(totalEntities)
32✔
142
}
143

144
func (w *World) exchangeTable(oldTable *table, oldLen int, add []ID, rem []ID, addComps []unsafe.Pointer, relations []RelationID) (tableID, int, int) {
64✔
145
        w.checkLocked()
64✔
146

64✔
147
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
64✔
148

64✔
149
        mask := oldArchetype.mask
64✔
150
        w.storage.getExchangeMask(&mask, add, rem)
64✔
151

64✔
152
        oldIDs := oldArchetype.components
64✔
153

64✔
154
        newTable := w.storage.findOrCreateTable(oldTable, &mask, relations)
64✔
155
        startIdx := uintptr(newTable.Len())
64✔
156
        count := uintptr(oldLen)
64✔
157

64✔
158
        var i uintptr
64✔
159
        for i = 0; i < count; i++ {
832✔
160
                idx := startIdx + i
768✔
161
                entity := oldTable.GetEntity(i)
768✔
162
                index := &w.storage.entities[entity.id]
768✔
163
                index.table = newTable.id
768✔
164
                index.row = uint32(idx)
768✔
165
        }
768✔
166

167
        newTable.AddAllEntities(oldTable, true)
64✔
168
        for _, id := range oldIDs {
304✔
169
                if mask.Get(id) {
336✔
170
                        oldCol := oldTable.GetColumn(id)
96✔
171
                        newCol := newTable.GetColumn(id)
96✔
172
                        newCol.SetLast(oldCol)
96✔
173
                }
96✔
174
        }
175
        if addComps != nil {
80✔
176
                if len(add) != len(addComps) {
16✔
177
                        panic("lengths of IDs and components to add do not match")
×
178
                }
179
                for i := range count {
208✔
180
                        for j, id := range add {
1,056✔
181
                                newTable.Set(id, uint32(startIdx+i), addComps[j])
864✔
182
                        }
864✔
183
                }
184
        }
185

186
        oldTable.Reset()
64✔
187
        w.storage.registerTargets(relations)
64✔
188

64✔
189
        return newTable.id, int(startIdx), int(count)
64✔
190
}
191

192
// setRelations sets the target entities for an entity relations.
193
func (w *World) setRelations(entity Entity, relations []RelationID) {
43✔
194
        w.checkLocked()
43✔
195

43✔
196
        if !w.storage.entityPool.Alive(entity) {
43✔
197
                panic("can't set relation for a dead entity")
×
198
        }
199

200
        index := &w.storage.entities[entity.id]
43✔
201
        oldTable := &w.storage.tables[index.table]
43✔
202

43✔
203
        newRelations, changed := w.storage.getExchangeTargets(oldTable, relations)
43✔
204
        if !changed {
43✔
205
                return
×
206
        }
×
207

208
        oldArch := &w.storage.archetypes[oldTable.archetype]
43✔
209
        newTable, ok := oldArch.GetTable(&w.storage, newRelations)
43✔
210
        if !ok {
55✔
211
                newTable = w.storage.createTable(oldArch, newRelations)
12✔
212
        }
12✔
213
        newIndex := newTable.Add(entity)
43✔
214

43✔
215
        for _, id := range oldArch.components {
149✔
216
                comp := oldTable.Get(id, uintptr(index.row))
106✔
217
                newTable.Set(id, newIndex, comp)
106✔
218
        }
106✔
219

220
        swapped := oldTable.Remove(index.row)
43✔
221

43✔
222
        if swapped {
59✔
223
                swapEntity := oldTable.GetEntity(uintptr(index.row))
16✔
224
                w.storage.entities[swapEntity.id].row = index.row
16✔
225
        }
16✔
226
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
43✔
227

43✔
228
        w.storage.registerTargets(relations)
43✔
229
}
230

231
func (w *World) getTables(batch *Batch) []*table {
33✔
232
        tables := []*table{}
33✔
233

33✔
234
        for i := range w.storage.archetypes {
165✔
235
                archetype := &w.storage.archetypes[i]
132✔
236
                if !batch.filter.matches(&archetype.mask) {
198✔
237
                        continue
66✔
238
                }
239

240
                if !archetype.HasRelations() {
132✔
241
                        table := &w.storage.tables[archetype.tables[0]]
66✔
242
                        tables = append(tables, table)
66✔
243
                        continue
66✔
244
                }
245

246
                tableIDs := archetype.GetTables(batch.relations)
×
247
                for _, tab := range tableIDs {
×
248
                        table := &w.storage.tables[tab]
×
249
                        if !table.Matches(batch.relations) {
×
250
                                continue
×
251
                        }
252
                        tables = append(tables, table)
×
253
                }
254
        }
255
        return tables
33✔
256
}
257

258
func (w *World) componentID(tp reflect.Type) ID {
2,128✔
259
        id, newID := w.storage.registry.ComponentID(tp)
2,128✔
260
        if newID {
2,720✔
261
                if w.IsLocked() {
592✔
262
                        w.storage.registry.unregisterLastComponent()
×
263
                        panic("attempt to register a new component in a locked world")
×
264
                }
265
                w.storage.AddComponent(id)
592✔
266
        }
267
        return ID{id: id}
2,128✔
268
}
269

270
func (w *World) resourceID(tp reflect.Type) ResID {
9✔
271
        id, _ := w.resources.registry.ComponentID(tp)
9✔
272
        return ResID{id: id}
9✔
273
}
9✔
274

275
// lock the world and get the lock bit for later unlocking.
276
func (w *World) lock() uint8 {
1,207✔
277
        return w.locks.Lock()
1,207✔
278
}
1,207✔
279

280
// unlock unlocks the given lock bit.
281
func (w *World) unlock(l uint8) {
1,207✔
282
        w.locks.Unlock(l)
1,207✔
283
}
1,207✔
284

285
// checkLocked checks if the world is locked, and panics if so.
286
func (w *World) checkLocked() {
1,007,306✔
287
        if w.IsLocked() {
1,007,306✔
288
                panic("attempt to modify a locked world")
×
289
        }
290
}
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