Skip to main content

Module cache

Module cache 

Source
Expand description

Compilation cache for TensorLogic expressions.

Provides two complementary caching mechanisms:

  1. CompilationCache — A thread-safe, key-based cache that stores compiled EinsumGraph instances keyed by a composite hash of expression structure, compilation configuration, and domain information. Designed for concurrent use.

  2. LruCompilationCache — A single-threaded LRU cache keyed by ExprFingerprint (a structural content-address of an expression). Evicts the least-recently-used entry when capacity is exceeded. Designed for use inside a CachingCompiler wrapper.

§Choosing the right cache

ScenarioRecommended type
Single-threaded compilation loopLruCompilationCache / CachingCompiler
Multi-threaded compilation (shared)CompilationCache
Batch compilation of related exprsCachingCompiler::compile_batch

§Example — LRU cache via CachingCompiler

use tensorlogic_compiler::cache::{CachingCompiler, CacheStats};
use tensorlogic_compiler::compile_to_einsum;
use tensorlogic_ir::{TLExpr, Term};

let mut compiler = CachingCompiler::new(64, |expr| {
    compile_to_einsum(expr).map_err(|e| e.to_string())
});

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

let _g1 = compiler.compile(&expr).expect("first compile");
let _g2 = compiler.compile(&expr).expect("second compile (cache hit)");

assert_eq!(compiler.cache_stats().hits, 1);
assert_eq!(compiler.cache_stats().misses, 1);

Structs§

CacheStats
Aggregate statistics for any compilation cache.
CachedResult
A cached compilation result stored in an LruCompilationCache.
CachingCompiler
CompilationCache
Thread-safe compilation cache for storing and retrieving compiled expressions.
ExprFingerprint
A compact fingerprint of a TLExpr structure (not values).
LruCompilationCache
LRU compilation cache with configurable capacity.