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

mlange-42 / arche / 4846735678

30 Apr 2023 10:35PM CUT coverage: 100.0%. Remained the same
4846735678

push

github

GitHub
Remove archetypes with relation on reset (#247)

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

3149 of 3149 relevant lines covered (100.0%)

2542.58 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
        ID         uint32            // Filter ID.
6
        Filter     Filter            // The underlying filter.
7
        Archetypes archetypePointers // Archetypes matching the filter.
8
}
9

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

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

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

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

3✔
73
        last := len(c.filters) - 1
3✔
74
        if idx != last {
4✔
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{}
3✔
79
        c.filters = c.filters[:last]
3✔
80

3✔
81
        return filter
3✔
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 {
12✔
88
        if idx, ok := c.indices[f.id]; ok {
23✔
89
                return &c.filters[idx]
11✔
90
        }
11✔
91
        panic("no filter for id found")
1✔
92
}
93

94
// Adds an archetype.
95
//
96
// Iterates over all filters and adds the archetype to the resp. entry where the filter matches.
97
func (c *Cache) addArchetype(arch *archetype) {
1,219✔
98
        ln := len(c.filters)
1,219✔
99
        for i := 0; i < ln; i++ {
1,228✔
100
                e := &c.filters[i]
9✔
101
                if arch.Matches(e.Filter) {
13✔
102
                        e.Archetypes.Add(arch)
4✔
103
                }
4✔
104
        }
105
}
106

107
// Removes an archetype.
108
//
109
// Iterates over all filters and removes the archetype from the resp. entry where the filter matches.
110
func (c *Cache) removeArchetype(arch *archetype) {
7✔
111
        ln := len(c.filters)
7✔
112
        for i := 0; i < ln; i++ {
10✔
113
                e := &c.filters[i]
3✔
114
                if arch.Matches(e.Filter) {
4✔
115
                        e.Archetypes.Remove(arch)
1✔
116
                }
1✔
117
        }
118
}
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