Skip to main content

Module incremental

Module incremental 

Source
Expand description

Incremental compilation for efficient recompilation when expressions change.

This module provides an incremental compilation system that tracks dependencies and recompiles only the parts of expressions that have changed. This is crucial for interactive environments like REPLs, notebooks, and IDEs where expressions are frequently modified.

§Architecture

The incremental compilation system consists of three main components:

  1. DependencyTracker: Tracks what each expression depends on (predicates, variables, domains, configurations).

  2. ChangeDetector: Detects changes to the compilation context (predicate signatures, domains, configurations) and determines what needs recompilation.

  3. IncrementalCompiler: Manages the compilation state, computes minimal invalidation sets, and recompiles only affected sub-expressions.

§Example

use tensorlogic_compiler::{CompilerContext, incremental::IncrementalCompiler};
use tensorlogic_ir::{TLExpr, Term};

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

let mut compiler = IncrementalCompiler::new(ctx);

// Initial compilation
let expr1 = TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]);
let graph1 = compiler.compile(&expr1).unwrap();

// Compile similar expression - some parts will be reused
let expr2 = TLExpr::and(
    TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
    TLExpr::pred("likes", vec![Term::var("x"), Term::var("z")]),
);
let graph2 = compiler.compile(&expr2).unwrap();

// Check incremental compilation stats
let stats = compiler.stats();
println!("Nodes reused: {}", stats.nodes_reused);
println!("Nodes compiled: {}", stats.nodes_compiled);
println!("Reuse rate: {:.1}%", stats.reuse_rate() * 100.0);

Structs§

ChangeDetector
Detects changes to the compilation context.
ChangeSet
Describes what has changed in the compilation context.
ExpressionDependencies
Tracks dependencies of compiled expressions.
IncrementalCompiler
Incremental compiler that reuses previously compiled expressions.
IncrementalStats
Statistics for incremental compilation.