Skip to main content

CompilationCache

Struct CompilationCache 

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

Thread-safe compilation cache for storing and retrieving compiled expressions.

Stores compiled EinsumGraph instances keyed by a composite hash that includes the expression structure, compilation configuration, and domain information. This cache is thread-safe and can be shared across compilation threads.

When capacity is exceeded the cache evicts the least-frequently-used entry (lowest hit_count). For strict LRU eviction use LruCompilationCache or CachingCompiler instead.

§Example

use tensorlogic_compiler::{CompilationCache, compile_to_einsum_with_context, CompilerContext};
use tensorlogic_ir::{TLExpr, Term};

let cache = CompilationCache::new(100);
let mut ctx = CompilerContext::new();
ctx.add_domain("Person", 100);

let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);

// First compilation: miss (not in cache)
let graph1 = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
    compile_to_einsum_with_context(expr, ctx)
}).expect("compile");

// Second compilation: hit (cached)
let graph2 = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
    compile_to_einsum_with_context(expr, ctx)
}).expect("compile");

assert_eq!(graph1, graph2);
assert_eq!(cache.stats().hits, 1);

Implementations§

Source§

impl CompilationCache

Source

pub fn new(max_size: usize) -> Self

Create a new compilation cache with the specified maximum size.

§Arguments
  • max_size – Maximum number of entries to cache.
§Example
use tensorlogic_compiler::CompilationCache;

let cache = CompilationCache::new(100);
assert_eq!(cache.max_size(), 100);
Source

pub fn default_size() -> Self

Create a cache with the default size of 1 000 entries.

Source

pub fn max_size(&self) -> usize

Maximum number of entries the cache can hold.

Source

pub fn get_or_compile<F>( &self, expr: &TLExpr, ctx: &mut CompilerContext, compile_fn: F, ) -> Result<EinsumGraph>

Get or compile an expression.

On a cache hit the stored result is returned immediately. On a miss compile_fn is called and the result is stored before returning.

§Example
use tensorlogic_compiler::{CompilationCache, compile_to_einsum_with_context, CompilerContext};
use tensorlogic_ir::{TLExpr, Term};

let cache = CompilationCache::new(100);
let mut ctx = CompilerContext::new();
ctx.add_domain("Person", 100);

let expr = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);

let graph = cache.get_or_compile(&expr, &mut ctx, |expr, ctx| {
    compile_to_einsum_with_context(expr, ctx)
}).expect("compile");
Source

pub fn stats(&self) -> CacheStats

Current cache statistics snapshot.

§Example
use tensorlogic_compiler::CompilationCache;

let cache = CompilationCache::new(100);
let stats = cache.stats();
assert_eq!(stats.hits, 0);
Source

pub fn clear(&self)

Clear all cached entries.

§Example
use tensorlogic_compiler::CompilationCache;

let cache = CompilationCache::new(100);
cache.clear();
assert_eq!(cache.stats().current_entries, 0);
Source

pub fn len(&self) -> usize

Current number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache is empty.

Trait Implementations§

Source§

impl Default for CompilationCache

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.