Skip to main content

Module cache

Module cache 

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

CoherenceCache
Stores previous layout allocations for temporal coherence.
CoherenceId
Identity for a layout computation, used as key in the coherence cache.
LayoutCache
Cache for layout computation results.
LayoutCacheKey
Key for layout cache lookups.
LayoutCacheStats
Statistics about layout cache performance.
S3FifoLayoutCache
Layout cache backed by S3-FIFO eviction.