• 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/cache.go
1
package ecs
2

3
// Cache entry for a [Filter].
4
type cacheEntry struct {
5
        Filter     Filter              // The underlying filter.
6
        Indices    map[*archetype]int  // Map of archetype indices for removal.
7
        Archetypes pointers[archetype] // Nodes matching the filter.
8
        ID         uint32              // Filter ID.
9
}
10

11
// Cache provides [Filter] caching to speed up queries.
12
//
13
// Access it using [World.Cache].
14
//
15
// For registered filters, the relevant archetypes are tracked internally,
16
// so that there are no mask checks required during iteration.
17
// This is particularly helpful to avoid query iteration slowdown by a very high number of archetypes.
18
// If the number of archetypes exceeds approx. 50-100, uncached filters experience a slowdown.
19
// The relative slowdown increases with lower numbers of entities queried (noticeable below a few thousand entities).
20
// Cached filters avoid this slowdown.
21
//
22
// The overhead of tracking cached filters internally is very low, as updates are required only when new archetypes are created.
23
type Cache struct {
24
        indices       map[uint32]int              // Mapping from filter IDs to indices in filters
25
        filters       []cacheEntry                // The cached filters, indexed by indices
26
        getArchetypes func(f Filter) []*archetype // Callback for getting archetypes for a new filter from the world
27
        intPool       intPool[uint32]             // Pool for filter IDs
28
}
29

30
// newCache creates a new [Cache].
31
func newCache() Cache {
137✔
32
        return Cache{
137✔
33
                intPool: newIntPool[uint32](128),
137✔
34
                indices: map[uint32]int{},
137✔
35
                filters: []cacheEntry{},
137✔
36
        }
137✔
37
}
137✔
38

39
// Register a [Filter].
40
//
41
// Use the returned [CachedFilter] to construct queries:
42
//
43
//        filter := All(posID, velID)
44
//        cached := world.Cache().Register(&filter)
45
//        query := world.Query(&cached)
46
func (c *Cache) Register(f Filter) CachedFilter {
24✔
47
        if _, ok := f.(*CachedFilter); ok {
25✔
48
                panic("filter is already registered")
1✔
49
        }
50
        id := c.intPool.Get()
23✔
51
        c.filters = append(c.filters,
23✔
52
                cacheEntry{
23✔
53
                        ID:         id,
23✔
54
                        Filter:     f,
23✔
55
                        Archetypes: pointers[archetype]{c.getArchetypes(f)},
23✔
56
                        Indices:    nil,
23✔
57
                })
23✔
58
        c.indices[id] = len(c.filters) - 1
23✔
59
        return CachedFilter{f, id}
23✔
60
}
61

62
// Unregister a filter.
63
//
64
// Returns the original filter.
65
func (c *Cache) Unregister(f *CachedFilter) Filter {
9✔
66
        idx, ok := c.indices[f.id]
9✔
67
        if !ok {
10✔
68
                panic("no filter for id found to unregister")
1✔
69
        }
70
        filter := c.filters[idx].Filter
8✔
71
        delete(c.indices, f.id)
8✔
72

8✔
73
        last := len(c.filters) - 1
8✔
74
        if idx != last {
9✔
75
                c.filters[idx], c.filters[last] = c.filters[last], c.filters[idx]
1✔
76
                c.indices[c.filters[idx].ID] = idx
1✔
77
        }
1✔
78
        c.filters[last] = cacheEntry{}
8✔
79
        c.filters = c.filters[:last]
8✔
80

8✔
81
        return filter
8✔
82
}
83

84
// Returns the [cacheEntry] for the given filter.
85
//
86
// Panics if there is no entry for the filter's ID.
87
func (c *Cache) get(f *CachedFilter) *cacheEntry {
27✔
88
        if idx, ok := c.indices[f.id]; ok {
53✔
89
                return &c.filters[idx]
26✔
90
        }
26✔
91
        panic("no filter for id found")
1✔
92
}
93

94
// Adds a node.
95
//
96
// Iterates over all filters and adds the node to the resp. entry where the filter matches.
97
func (c *Cache) addArchetype(arch *archetype) {
28,908✔
98
        if !arch.HasRelation() {
30,201✔
99
                for i := range c.filters {
1,302✔
100
                        e := &c.filters[i]
9✔
101
                        if !e.Filter.Matches(&arch.Mask) {
15✔
102
                                continue
6✔
103
                        }
104
                        e.Archetypes.Add(arch)
3✔
105
                }
106
                return
1,293✔
107
        }
108

109
        for i := range c.filters {
27,640✔
110
                e := &c.filters[i]
25✔
111
                if !e.Filter.Matches(&arch.Mask) {
28✔
112
                        continue
3✔
113
                }
114
                if rf, ok := e.Filter.(*RelationFilter); ok {
38✔
115
                        if rf.Target == arch.RelationTarget {
21✔
116
                                e.Archetypes.Add(arch)
5✔
117
                                // Not required: can't add after removing,
5✔
118
                                // as the target entity is dead.
5✔
119
                                // if e.Indices != nil { e.Indices[arch] = int(e.Archetypes.Len() - 1) }
5✔
120
                        }
5✔
121
                        continue
16✔
122
                }
123
                e.Archetypes.Add(arch)
6✔
124
                if e.Indices != nil {
10✔
125
                        e.Indices[arch] = int(e.Archetypes.Len() - 1)
4✔
126
                }
4✔
127
        }
128
}
129

130
// Removes an archetype.
131
//
132
// Can only be used for archetypes that have a relation target.
133
// Archetypes without a relation are never removed.
134
func (c *Cache) removeArchetype(arch *archetype) {
27,427✔
135
        for i := range c.filters {
27,451✔
136
                e := &c.filters[i]
24✔
137

24✔
138
                if e.Indices == nil && e.Filter.Matches(&arch.Mask) {
28✔
139
                        c.mapArchetypes(e)
4✔
140
                }
4✔
141

142
                if idx, ok := e.Indices[arch]; ok {
32✔
143
                        swap := e.Archetypes.RemoveAt(idx)
8✔
144
                        if swap {
12✔
145
                                e.Indices[e.Archetypes.Get(int32(idx))] = idx
4✔
146
                        }
4✔
147
                        delete(e.Indices, arch)
8✔
148
                }
149
        }
150
}
151

152
func (c *Cache) mapArchetypes(e *cacheEntry) {
4✔
153
        e.Indices = map[*archetype]int{}
4✔
154
        for i, arch := range e.Archetypes.pointers {
8✔
155
                if arch.HasRelation() {
8✔
156
                        e.Indices[arch] = i
4✔
157
                }
4✔
158
        }
159
}
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