Skip to main content

Module cost_based

Module cost_based 

Source
Expand description

Cost-based query optimization for TLExpr.

This module implements a cost-based optimizer that:

  • Generates equivalent expression rewrites
  • Estimates execution cost for each alternative
  • Selects the most efficient representation
  • Provides backend optimization hints

§Overview

The cost-based optimizer explores the space of equivalent expressions by applying rewriting rules, then uses cost estimation to select the best alternative. This is particularly useful for complex queries where there are multiple valid execution strategies.

§Example

use tensorlogic_compiler::optimize::optimize_by_cost;
use tensorlogic_compiler::CompilerContext;
use tensorlogic_ir::{TLExpr, Term};

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

// Complex expression with multiple equivalent forms
let expr = TLExpr::and(
    TLExpr::pred("p", vec![Term::var("x")]),
    TLExpr::or(
        TLExpr::pred("q", vec![Term::var("x")]),
        TLExpr::pred("r", vec![Term::var("x")]),
    ),
);

let (optimized, stats) = optimize_by_cost(&expr, &ctx);
println!("Cost reduction: {:.1}%", stats.cost_reduction_percent());

Structs§

CostBasedOptimizer
Cost-based optimizer that explores rewrite space.
CostBasedStats
Statistics from cost-based optimization.

Enums§

RewriteRule
Rewriting rule for generating equivalent expressions.

Functions§

optimize_by_cost
Optimize an expression using cost-based optimization.
optimize_by_cost_with_config
Optimize with custom parameters.