Skip to main content

SvgrCache

Struct SvgrCache 

Source
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:

  1. 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.
  2. 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

Source

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.

Source

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.

Source

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
Source

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>

Source

pub fn none() -> Self

Creates a no cache value. Neither LRU nor static caching is enabled.

Source

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.

Source

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.

Source

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.

Source

pub fn static_cache_len(&self) -> usize

Returns the number of entries in the static cache.

Source

pub fn static_cache_bytes(&self) -> usize

Returns the total bytes used by the static cache.

Source

pub fn print_stats(&self)

No-op when the cache-stats feature is disabled.

Source

pub fn clear_static_cache(&self)

Clears the static cache, freeing all cached pixmaps.

Source

pub fn has_static_cache(&self) -> bool

Returns true if static caching is enabled.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<RandomState: Debug + BuildHasher> Debug for SvgrCache<RandomState>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: BuildHasher + Send> Send for SvgrCache<T>

Auto Trait Implementations§

§

impl<RandomState = RandomState> !Freeze for SvgrCache<RandomState>

§

impl<RandomState = RandomState> !RefUnwindSafe for SvgrCache<RandomState>

§

impl<RandomState = RandomState> !Sync for SvgrCache<RandomState>

§

impl<RandomState> Unpin for SvgrCache<RandomState>
where RandomState: Unpin,

§

impl<RandomState> UnsafeUnpin for SvgrCache<RandomState>
where RandomState: UnsafeUnpin,

§

impl<RandomState> UnwindSafe for SvgrCache<RandomState>
where RandomState: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.