util/lookup-cache.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/unordered-map"; | ||
| 5 | import "std/math/hash"; | ||
| 6 | import "std/text/stringstream"; | ||
| 7 | import "std/type/type-conversion"; | ||
| 8 | |||
| 9 | // Generic type defs | ||
| 10 | type T dyn; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Lookup cache of a manager (FunctionManager, StructManager, ...). Spice only supports globals of primitive type, so the cache | ||
| 14 | * and its statistics are kept in the global resource manager, which clears them when it is destroyed. | ||
| 15 | */ | ||
| 16 | 7284 | public type LookupCache<T> struct { | |
| 17 | public UnorderedMap<Hash, T*> lookupCache | ||
| 18 | 4220 | public unsigned long lookupCacheHits = 0l | |
| 19 | 4220 | public unsigned long lookupCacheMisses = 0l | |
| 20 | // Requests that did not match anything, with the registry version at that time (only used by the FunctionManager) | ||
| 21 | public UnorderedMap</*cacheKey=*/Hash, /*registryVersion=*/unsigned long> noMatchCache | ||
| 22 | // Increased whenever a new entry is inserted (only used by the FunctionManager) | ||
| 23 | 4220 | public unsigned long registryVersion = 0l | |
| 24 | } | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Clear the lookup cache | ||
| 28 | */ | ||
| 29 | 3064 | public p LookupCache.cleanup() { | |
| 30 | 3064 | this.lookupCache.clear(); | |
| 31 | 3064 | this.noMatchCache.clear(); | |
| 32 | 3064 | this.registryVersion = 0l; | |
| 33 | 3064 | this.lookupCacheHits = 0l; | |
| 34 | 3064 | this.lookupCacheMisses = 0l; | |
| 35 | 3064 | } | |
| 36 | |||
| 37 | /** | ||
| 38 | * Dump usage statistics for the lookup cache | ||
| 39 | * | ||
| 40 | * @param managerName Name of the manager, the cache belongs to | ||
| 41 | * @return Statistics string | ||
| 42 | */ | ||
| 43 | 3040 | public f<String> LookupCache.dumpLookupCacheStatistics(const string managerName) { | |
| 44 | 3040 | StringStream stats; | |
| 45 | 3040 | stats << managerName << " lookup cache statistics:\n"; | |
| 46 | 3040 | stats << " lookup cache entries: " << toString(this.lookupCache.getSize()) << "\n"; | |
| 47 | 3040 | stats << " lookup cache hits: " << toString(this.lookupCacheHits) << "\n"; | |
| 48 | 3040 | stats << " lookup cache misses: " << toString(this.lookupCacheMisses) << "\n"; | |
| 49 | 6080 | return stats.str(); | |
| 50 | 3040 | } | |
| 51 |