pub struct MeasureCache { /* private fields */ }Expand description
Thread-local cache for widget measure results.
Caches SizeConstraints returned by MeasurableWidget::measure() to
avoid redundant computation during layout passes.
§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. Stale entries are
treated as cache misses and will be recomputed on next access.
Implementations§
Source§impl MeasureCache
impl MeasureCache
Sourcepub fn get_or_compute<F>(
&mut self,
widget_id: WidgetId,
available: Size,
compute: F,
) -> SizeConstraintswhere
F: FnOnce() -> SizeConstraints,
pub fn get_or_compute<F>(
&mut self,
widget_id: WidgetId,
available: Size,
compute: F,
) -> SizeConstraintswhere
F: FnOnce() -> SizeConstraints,
Get cached result or compute and cache a new one.
If a valid (same generation) cache entry exists for the given widget ID
and available size, returns it immediately. Otherwise, calls the compute
closure, caches the result, and returns it.
§Arguments
widget_id- Unique identifier for the widget instanceavailable- Available space for the measurementcompute- Closure to compute the constraints if not cached
§Example
let constraints = cache.get_or_compute(
WidgetId::from_ptr(¶graph),
Size::new(80, 24),
|| paragraph.measure(Size::new(80, 24)),
);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 widget measurements:
- Model data changes
- Font/theme changes (if they affect sizing)
- Locale changes (if they affect text)
§Note
Resize events don’t require invalidation because the available size is part of the cache key.
Sourcepub fn invalidate_widget(&mut self, widget_id: WidgetId)
pub fn invalidate_widget(&mut self, widget_id: WidgetId)
Invalidate entries for a specific widget.
Removes all cache entries associated with the given widget ID. Use this for targeted invalidation when only one widget’s content changes.
§Arguments
widget_id- The widget whose entries should be invalidated
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get current cache statistics.
Returns hit/miss counts and the current hit rate.
§Example
use ftui_widgets::MeasureCache;
let cache = MeasureCache::new(100);
let stats = cache.stats();
println!("Hit rate: {:.1}%", stats.hit_rate * 100.0);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.
Use when transitioning to a completely different view.