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
impl CompilationCache
Sourcepub fn default_size() -> Self
pub fn default_size() -> Self
Create a new cache with default settings (1000 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.
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 compilectx- The compiler contextcompile_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();Sourcepub fn stats(&self) -> CacheStats
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);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 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