pub struct LayoutCache { /* private fields */ }Expand description
Cache for layout computation results.
Stores Vec<Rect> results keyed by LayoutCacheKey to avoid redundant
constraint solving during rendering.
§Capacity
The cache has a fixed maximum capacity. When full, the least recently used entries are evicted to make room for new ones.
§Generation-Based Invalidation
Each entry is tagged with a generation number. Calling invalidate_all()
bumps the generation, making all existing entries stale.
Implementations§
Source§impl LayoutCache
impl LayoutCache
Sourcepub fn get_or_compute<F>(
&mut self,
key: LayoutCacheKey,
compute: F,
) -> Vec<Rect>
pub fn get_or_compute<F>( &mut self, key: LayoutCacheKey, compute: F, ) -> Vec<Rect>
Get cached layout or compute and cache a new one.
If a valid (same generation) cache entry exists for the given key,
returns a clone of it. Otherwise, calls the compute closure,
caches the result, and returns it.
§Arguments
key- The cache key identifying this layout computationcompute- Closure to compute the layout if not cached
§Example
let key = LayoutCacheKey::new(area, &constraints, Direction::Horizontal, None);
let rects = cache.get_or_compute(key, || flex.split(area));Sourcepub fn invalidate_all(&mut self)
pub fn invalidate_all(&mut self)
Invalidate all entries by bumping the generation.
Existing entries become stale and will be recomputed on next access. This is an O(1) operation - entries are not immediately removed.
§When to Call
Call this after any state change that affects layout:
- Model data changes that affect widget content
- Theme/font changes that affect sizing
§Note
Resize events don’t require invalidation because the area is part of the cache key.
Sourcepub fn stats(&self) -> LayoutCacheStats
pub fn stats(&self) -> LayoutCacheStats
Get current cache statistics.
Returns hit/miss counts and the current hit rate.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset statistics counters to zero.
Useful for measuring hit rate over a specific period (e.g., per frame).
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all entries from the cache.
Unlike invalidate_all(), this immediately frees memory.