pub struct SvgrCache<RandomState: BuildHasher = RandomState> { /* private fields */ }Expand description
Defines rendering cache with both LRU cache for dynamic elements and persistent cache for static elements.
The cache has two tiers:
- Static cache: For elements with compile-time known content (identified by
static_hash). These are rendered once at their bounding box and cached permanently using the hash directly. Includes a bloom filter for fast negative lookups and memory bounds to prevent unbounded growth. - LRU cache: For dynamic elements that may change between frames. Uses least-recently-used eviction when capacity is reached.
§Optimizations for Large Repeating SVGs
The static cache is optimized for SVGs with many identical elements:
- Bloom filter: Provides O(1) negative lookups, avoiding HashMap access for uncached elements
- Memory bounds: Configurable max memory prevents unbounded cache growth
- Pre-sized HashMap: Reduces rehashing overhead for large element counts
- Statistics tracking: Enables monitoring cache efficiency
Pass &mut SvgrCache::none() if you don’t need any caching.
Implementations§
Source§impl SvgrCache
impl SvgrCache
Sourcepub fn new(lru_size: usize) -> Self
pub fn new(lru_size: usize) -> Self
Creates a new cache with the specified LRU capacity. Static caching is enabled by default with a 64MB memory limit.
If capacity <= 0 then the LRU cache is disabled but static cache remains active.
Uses ahash as a hasher, if you want to specify custom hasher use new_with_hasher fn.
Sourcepub fn static_only() -> Self
pub fn static_only() -> Self
Creates a new cache with static caching only (no LRU cache).
This is useful when you only want to cache compile-time static elements and don’t need caching for dynamic elements.
Sourcepub fn for_large_repeating_svgs(lru_size: usize) -> Self
pub fn for_large_repeating_svgs(lru_size: usize) -> Self
Creates a new cache optimized for large SVGs with many repeating elements.
This configuration:
- Uses a larger initial HashMap capacity to reduce rehashing
- Has a higher memory limit (256MB) for more cache entries
- Includes bloom filter for fast negative lookups
Sourcepub fn with_static_config(lru_size: usize, config: StaticCacheConfig) -> Self
pub fn with_static_config(lru_size: usize, config: StaticCacheConfig) -> Self
Creates a new cache with custom static cache configuration.
Source§impl<THashBuilder: BuildHasher + Default> SvgrCache<THashBuilder>
impl<THashBuilder: BuildHasher + Default> SvgrCache<THashBuilder>
Sourcepub fn new_sized(lru_size: usize) -> Self
pub fn new_sized(lru_size: usize) -> Self
Creates a new cache with the specified LRU capacity and static caching enabled. If capacity <= 0 then only static caching is used.
Sourcepub fn lru_only(lru_size: usize) -> Self
pub fn lru_only(lru_size: usize) -> Self
Creates a new cache with only LRU caching (no static cache). Use this if you don’t want permanent caching of static elements.
Sourcepub fn unlimited_static(lru_size: usize) -> Self
pub fn unlimited_static(lru_size: usize) -> Self
Creates a new cache with unlimited static cache memory. Use with caution - cache can grow unbounded for large SVGs.
Sourcepub fn static_cache_len(&self) -> usize
pub fn static_cache_len(&self) -> usize
Returns the number of entries in the static cache.
Sourcepub fn static_cache_bytes(&self) -> usize
pub fn static_cache_bytes(&self) -> usize
Returns the total bytes used by the static cache.
Sourcepub fn print_stats(&self)
pub fn print_stats(&self)
No-op when the cache-stats feature is disabled.
Sourcepub fn clear_static_cache(&self)
pub fn clear_static_cache(&self)
Clears the static cache, freeing all cached pixmaps.
Sourcepub fn has_static_cache(&self) -> bool
pub fn has_static_cache(&self) -> bool
Returns true if static caching is enabled.
Sourcepub fn get_static(&self, static_hash: u64) -> Option<&Pixmap>
pub fn get_static(&self, static_hash: u64) -> Option<&Pixmap>
Get a cached pixmap by its static hash. Uses bloom filter for fast negative lookups.
Sourcepub fn has_static(&self, static_hash: u64) -> bool
pub fn has_static(&self, static_hash: u64) -> bool
Check if a static hash is in the cache. Uses bloom filter for fast negative lookups.
Sourcepub fn insert_static(&self, static_hash: u64, pixmap: Pixmap) -> bool
pub fn insert_static(&self, static_hash: u64, pixmap: Pixmap) -> bool
Insert a pixmap into the static cache. Returns true if inserted, false if memory limit would be exceeded.