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
impl CompilationCache
Sourcepub fn default_size() -> Self
pub fn default_size() -> Self
Create a cache with the default size of 1 000 entries.
Sourcepub fn get_or_compile<F>(
&self,
expr: &TLExpr,
ctx: &mut CompilerContext,
compile_fn: F,
) -> Result<EinsumGraph>
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");Sourcepub fn stats(&self) -> CacheStats
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);Trait Implementations§
Auto Trait Implementations§
impl Freeze for CompilationCache
impl RefUnwindSafe for CompilationCache
impl Send for CompilationCache
impl Sync for CompilationCache
impl Unpin for CompilationCache
impl UnsafeUnpin for CompilationCache
impl UnwindSafe for CompilationCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more