Skip to main content

ComputeCache

Struct ComputeCache 

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

An opt-in bounded cache for ComputedStyle results, keyed by an opaque signature that captures (node identity, ancestor-chain signature, media context, stylesheet generation).

See the module docs for the correctness invariants and the eviction policy. Two computes with the same signature yield the same ComputedStyle, so a hit short-circuits the cascade. The cache is an access-order LRU: both a get hit and an insert of an existing key promote that key to most-recently-used, and the least-recently-used entry is evicted when the cache is full.

get takes &mut self for two reasons: a stylesheet generation mismatch (detected on every access) clears the cache in place — see Self::check_generation — and a hit promotes the key to the back of the eviction order. Both get and insert are O(capacity) because promotion scans the order deque.

Implementations§

Source§

impl ComputeCache

Source

pub fn new(capacity: usize) -> Self

Build a cache with the given hard capacity. capacity == 0 disables storage: get always returns None and insert is a no-op.

Source

pub fn get(&mut self, sig: u64, current_gen: u64) -> Option<ComputedStyle>

Look up a cached result by signature. Returns an owned ComputedStyle clone on a hit (cheap — a post-resolution ComputedStyle holds no heap Color::Var).

A hit promotes sig to the back of the eviction order (most-recently-used), so frequently accessed entries survive longer. This is the access-order LRU promotion. The scan over the order deque makes get O(capacity); acceptable for a small bounded cache. A miss does NOT mutate the order.

&mut self because check_generation may clear, and a hit promotes.

Source

pub fn insert(&mut self, sig: u64, value: ComputedStyle, current_gen: u64)

Insert a computed result under sig. If the key already exists, update its value and move it to the back of the eviction order (most-recently-used) — an access-order LRU promotion. If at capacity and the key is new, evict the least-recently-used entry (front of the order). A no-op when capacity is 0. O(capacity) due to the promotion scan.

Source

pub fn clear(&mut self)

Drop every entry. Keeps the capacity for reuse.

Source

pub fn len(&self) -> usize

Number of entries currently cached.

Source

pub fn is_empty(&self) -> bool

Whether the cache holds zero entries.

Trait Implementations§

Source§

impl Default for ComputeCache

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.