Skip to main content

LayoutCache

Struct LayoutCache 

Source
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

Source

pub fn new(max_entries: usize) -> Self

Create a new cache with the specified maximum capacity.

§Arguments
  • max_entries - Maximum number of entries before LRU eviction occurs. A typical value is 64-256 for most UIs.
§Example
use ftui_layout::LayoutCache;
let cache = LayoutCache::new(64);
Source

pub fn get_or_compute<F>( &mut self, key: LayoutCacheKey, compute: F, ) -> Vec<Rect>
where F: FnOnce() -> 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 computation
  • compute - 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));
Source

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.

Source

pub fn stats(&self) -> LayoutCacheStats

Get current cache statistics.

Returns hit/miss counts and the current hit rate.

Source

pub fn reset_stats(&mut self)

Reset statistics counters to zero.

Useful for measuring hit rate over a specific period (e.g., per frame).

Source

pub fn clear(&mut self)

Clear all entries from the cache.

Unlike invalidate_all(), this immediately frees memory.

Source

pub fn len(&self) -> usize

Returns the current number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

Source

pub fn capacity(&self) -> usize

Returns the maximum capacity of the cache.

Trait Implementations§

Source§

impl Debug for LayoutCache

Source§

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

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

impl Default for LayoutCache

Source§

fn default() -> Self

Creates a cache with default capacity of 64 entries.

Auto Trait Implementations§

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.