CompilationCache

Struct CompilationCache 

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

Compilation cache for storing and retrieving compiled expressions.

The cache is thread-safe and can be shared across multiple compilation operations. It automatically evicts least-recently-used entries when the cache reaches its maximum size.

§Example

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

let cache = CompilationCache::new(100); // Cache up to 100 entries
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)
}).unwrap();

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

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 (default: 1000)
§Example
use tensorlogic_compiler::CompilationCache;

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

pub fn default_size() -> Self

Create a new cache with default settings (1000 entries).

Source

pub fn max_size(&self) -> usize

Get the maximum cache size.

Source

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

Get or compile an expression.

If the expression is in the cache, returns the cached result. Otherwise, compiles the expression using the provided function and caches the result.

§Arguments
  • expr - The expression to compile
  • ctx - The compiler context
  • compile_fn - Function to compile the expression if not cached
§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)
}).unwrap();
Source

pub fn stats(&self) -> CacheStats

Get current cache statistics.

§Example
use tensorlogic_compiler::CompilationCache;

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

pub fn clear(&self)

Clear the cache.

§Example
use tensorlogic_compiler::CompilationCache;

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

pub fn len(&self) -> usize

Get the current number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Check if 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.