Skip to main content

MeasureCache

Struct MeasureCache 

Source
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

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 100-1000 depending on widget complexity.
§Example
use ftui_widgets::MeasureCache;
let cache = MeasureCache::new(256);
Source

pub fn get_or_compute<F>( &mut self, widget_id: WidgetId, available: Size, compute: F, ) -> SizeConstraints
where 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 instance
  • available - Available space for the measurement
  • compute - Closure to compute the constraints if not cached
§Example
let constraints = cache.get_or_compute(
    WidgetId::from_ptr(&paragraph),
    Size::new(80, 24),
    || paragraph.measure(Size::new(80, 24)),
);
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 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.

Source

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
Source

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);
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. Use when transitioning to a completely different view.

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 MeasureCache

Source§

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

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

impl Default for MeasureCache

Source§

fn default() -> Self

Creates a cache with default capacity of 256 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more