Skip to main content

LruCompilationCache

Struct LruCompilationCache 

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

LRU compilation cache with configurable capacity.

Stores compiled EinsumGraph instances keyed by ExprFingerprint. When capacity is exceeded the least-recently-used entry is evicted.

This cache is not thread-safe — wrap it in Arc<Mutex<_>> or use CompilationCache if you need concurrent access.

§Example

use tensorlogic_compiler::cache::{LruCompilationCache, ExprFingerprint};
use tensorlogic_ir::EinsumGraph;

let mut cache = LruCompilationCache::new(4);
let fp = ExprFingerprint::compute("pred(x)");
cache.insert(fp.clone(), EinsumGraph::new());
assert!(cache.get(&fp).is_some());

Implementations§

Source§

impl LruCompilationCache

Source

pub fn new(capacity: usize) -> Self

Create a new LRU cache with the given capacity (minimum 1).

Source

pub fn insert(&mut self, fp: ExprFingerprint, graph: EinsumGraph)

Insert a compiled result for the given fingerprint.

If the fingerprint already exists the stored graph is updated and the entry is promoted to the most-recently-used position.

If the cache is at capacity the least-recently-used entry is evicted before the new entry is inserted.

Source

pub fn get(&mut self, fp: &ExprFingerprint) -> Option<&CachedResult>

Look up a fingerprint.

On a hit the entry is promoted to the most-recently-used position, its hit_count is incremented, and a reference to it is returned. On a miss None is returned.

Source

pub fn contains(&self, fp: &ExprFingerprint) -> bool

Check if a fingerprint is present without updating LRU order or stats.

Source

pub fn invalidate(&mut self, fp: &ExprFingerprint) -> bool

Remove a specific entry by fingerprint.

Returns true if the entry existed and was removed, false otherwise.

Source

pub fn clear(&mut self)

Clear all cached entries, resetting memory accounting.

Statistics counters (hits, misses, evictions) are not reset.

Source

pub fn stats(&self) -> &CacheStats

Reference to the current statistics snapshot.

Source

pub fn len(&self) -> usize

Number of cached entries.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache contains no entries.

Source

pub fn capacity(&self) -> usize

The maximum number of entries this cache can hold before eviction.

Trait Implementations§

Source§

impl Default for LruCompilationCache

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, 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.