Expand description
Layout cache for memoizing layout computation results.
This module provides LayoutCache which caches the Vec<Rect> results from
layout computations to avoid redundant constraint solving during rendering.
§Overview
Layout computation (constraint solving, flex distribution) can be expensive for complex nested layouts. During a single frame, the same layout may be queried multiple times with identical parameters. The cache eliminates this redundancy.
§Usage
ⓘ
use ftui_layout::{Flex, Constraint, LayoutCache, LayoutCacheKey, Direction};
use ftui_core::geometry::Rect;
let mut cache = LayoutCache::new(64);
let flex = Flex::horizontal()
.constraints([Constraint::Percentage(50.0), Constraint::Fill]);
let area = Rect::new(0, 0, 80, 24);
// First call computes and caches
let rects = flex.split_cached(area, &mut cache);
// Second call returns cached result
let cached = flex.split_cached(area, &mut cache);§Invalidation
§Generation-Based (Primary)
Call LayoutCache::invalidate_all() after any state change affecting layouts:
ⓘ
match msg {
Msg::DataChanged(_) => {
self.layout_cache.invalidate_all();
}
Msg::Resize(_) => {
// Area is part of cache key, no invalidation needed!
}
}§Cache Eviction
The cache uses LRU (Least Recently Used) eviction when at capacity.
Structs§
- Coherence
Cache - Stores previous layout allocations for temporal coherence.
- Coherence
Id - Identity for a layout computation, used as key in the coherence cache.
- Layout
Cache - Cache for layout computation results.
- Layout
Cache Key - Key for layout cache lookups.
- Layout
Cache Stats - Statistics about layout cache performance.