Skip to main content

TextShapeCache

Struct TextShapeCache 

Source
pub struct TextShapeCache { /* private fields */ }
Expand description

Tier 2: bounded global LRU shaping cache.

Stores shaped text results keyed by (FontId, font_size_bits, text_hash). Evicts least-recently-used entries when the total memory budget is exceeded.

The cache tracks access order via an internal age counter. Each access updates the entry’s age. When the budget is exceeded, the oldest entries are evicted first.

§Examples

use martensite_text::TextShapeCache;

let cache = TextShapeCache::with_default_budget();
assert!(cache.is_empty());
assert_eq!(cache.budget(), 16 * 1024 * 1024);

Implementations§

Source§

impl TextShapeCache

Source

pub fn new(budget: usize) -> Self

Creates a new cache with the given memory budget in bytes.

Source

pub fn with_default_budget() -> Self

Creates a cache with the default 16 MB budget.

Source

pub fn len(&self) -> usize

Returns the number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

Source

pub fn total_memory(&self) -> usize

Returns the total estimated memory usage in bytes.

Source

pub fn budget(&self) -> usize

Returns the memory budget in bytes.

Source

pub fn hits(&self) -> u64

Returns the number of cache hits.

Source

pub fn misses(&self) -> u64

Returns the number of cache misses.

Source

pub fn hit_rate(&self) -> f64

Returns the cache hit rate as a fraction in [0.0, 1.0].

Source

pub fn get(&mut self, key: &ShapeCacheKey) -> Option<&CachedShape>

Looks up a cached shape by key.

Returns Some(&CachedShape) on hit, None on miss. Updates the entry’s access age on hit.

Source

pub fn insert(&mut self, key: ShapeCacheKey, shape: CachedShape)

Inserts a shaped result into the cache.

If the entry’s memory pushes the total over budget, LRU entries are evicted until the budget is satisfied.

Source

pub fn clear(&mut self)

Clears all entries from the cache.

Source

pub fn trim(&mut self, keep_age: u64)

Trims entries that haven’t been accessed in keep_age ticks.

This is a softer eviction than the memory-based eviction in Self::insert.

Source

pub fn invalidate_font(&mut self, font_id: FontId)

Invalidates all entries for a specific font (e.g., when a font is unloaded or changed).

Source

pub fn invalidate_font_size(&mut self, font_size: f32)

Invalidates all entries for a specific font size.

Source

pub fn resize(&mut self, new_budget: usize)

Resizes the memory budget, evicting entries if the new budget is smaller than current usage.

Trait Implementations§

Source§

impl Debug for TextShapeCache

Source§

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

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

impl Default for TextShapeCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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