use crate::ast::{GlobalVarId, LitValVarId, MsetVarId, SeqVarId, SetVarId, VarId};
use crate::containers::{DenseId, IndexLike};
use crate::resolve::{MatchShape, PatVar, PredGuard, RAtom, RMult, ResolvedQuery};
use std::cell::Cell;
use std::hash::Hash;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum IndexLookup<O, I> {
ByOp { op: O },
ByChildPos { child: PatVar, pos: I },
ByRepr { repr: VarId },
ByContains { child: PatVar },
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Step<O, I, V> {
Join {
target: VarId,
lookups: Vec<IndexLookup<O, I>>,
atom_id: usize,
},
ExtractChild {
target: VarId,
parent: VarId,
pos: I,
},
CheckChildEq {
parent: VarId,
pos: I,
expected: PatVar,
},
CheckEq {
a: VarId,
b: VarId,
},
CheckEqGlobal {
local: VarId,
global: GlobalVarId,
},
BindGlobal {
target: VarId,
global: GlobalVarId,
},
CopyBinding {
target: VarId,
other: VarId,
},
ExpandA {
node: VarId,
children: Vec<PatVar>,
pre: Option<SeqVarId>,
suf: Option<SeqVarId>,
},
DecomposeAC {
node: VarId,
elems: Vec<(PatVar, RMult)>,
rest: Option<MsetVarId>,
idempotent: bool,
},
DecomposeACI {
node: VarId,
elems: Vec<PatVar>,
rest: Option<SetVarId>,
},
ExtractLitVal {
node: VarId,
val: LitValVarId,
},
CheckLit {
node: VarId,
value: V,
},
CheckPred {
guard: PredGuard<O, V>,
},
}
#[derive(Clone, Debug)]
pub struct QueryPlan<O, I, V> {
pub steps: Vec<Step<O, I, V>>,
pub shape: MatchShape,
}
pub struct IndexStats<O: Eq + Hash> {
pub op_card: std::collections::HashMap<O, usize>,
pub fanouts: crate::index::FanOuts<O>,
pub atom_card: std::collections::HashMap<usize, usize>,
}
impl<O: Eq + Hash> Default for IndexStats<O> {
fn default() -> Self {
Self::new()
}
}
impl<O: Eq + Hash> IndexStats<O> {
pub fn new() -> Self {
Self {
op_card: std::collections::HashMap::new(),
atom_card: std::collections::HashMap::new(),
fanouts: crate::index::FanOuts::default(),
}
}
}
impl<O: Eq + Hash + Copy> IndexStats<O> {
pub fn from_index<Cfg>(index: &crate::index::IndexStore<Cfg>) -> Self
where
Cfg: crate::config::EGraphConfig<O = O>,
crate::canon::MSetCanon: crate::canon::VarCanon<Cfg::G, Cfg::C>,
{
Self {
op_card: index.op_cardinalities().collect(),
atom_card: std::collections::HashMap::new(),
fanouts: index.fanouts.clone(),
}
}
}
fn child_pos<I: IndexLike>(pos: usize) -> I {
I::try_from_usize(pos)
.expect("pattern child position exceeds EGraphConfig::Index; configure a wider index word")
}
fn pv_is_bound(pv: &PatVar, bound: &[bool]) -> bool {
match pv {
PatVar::Local(vid) => bound[vid.idx()],
PatVar::Global(_) => true,
}
}
fn pv_mark_bound(pv: &PatVar, bound: &mut [bool]) {
if let PatVar::Local(vid) = pv {
bound[vid.idx()] = true;
}
}
fn base_card<O: Eq + Hash>(op: &O, atom_id: usize, stats: &IndexStats<O>) -> usize {
stats
.atom_card
.get(&atom_id)
.copied()
.or_else(|| stats.op_card.get(op).copied())
.unwrap_or(usize::MAX)
}
fn path_selectivity(fanout: Option<f64>, denom: usize) -> f64 {
match fanout {
Some(f) if denom > 0 => (f / denom as f64).clamp(0.0, 1.0),
_ => 0.5,
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum KeySite {
Node,
Child(usize),
Element,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ProbePath {
ChildPos(usize),
Contains,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SamplerConfig {
pub k: usize,
pub bootstrap: usize,
pub cv_threshold: f64,
}
impl Default for SamplerConfig {
fn default() -> Self {
Self {
k: 32,
bootstrap: 0,
cv_threshold: 1.0,
}
}
}
thread_local! {
static SAMPLED_SELECTIVITY: Cell<Option<SamplerConfig>> = const { Cell::new(None) };
}
pub fn set_sampled_selectivity(cfg: Option<SamplerConfig>) {
SAMPLED_SELECTIVITY.with(|c| c.set(cfg));
}
pub fn sampled_selectivity() -> Option<SamplerConfig> {
SAMPLED_SELECTIVITY.with(|c| c.get())
}
thread_local! {
static SAMPLE_TALLY: Cell<(u64, u64)> = const { Cell::new((0, 0)) };
}
pub fn sample_tally() -> (u64, u64) {
SAMPLE_TALLY.with(|c| c.get())
}
pub fn reset_sample_tally() {
SAMPLE_TALLY.with(|c| c.set((0, 0)));
}
fn tally(rejected: bool) {
SAMPLE_TALLY.with(|c| {
let (t, r) = c.get();
c.set((t + 1, r + u64::from(rejected)));
});
}
pub trait CrossSampler<O> {
fn driver_sample(&self, atom_id: usize, op: O, k: usize, out: &mut Vec<usize>);
fn key_classes(&self, node: usize, site: KeySite, out: &mut Vec<usize>);
fn probe_len(&self, class: usize, path: ProbePath, op: O) -> usize;
}
type SampleKey = (usize, KeySite, usize, ProbePath);
struct Sampling<'a, O> {
sampler: &'a dyn CrossSampler<O>,
cfg: SamplerConfig,
drivers: Vec<(usize, Vec<usize>)>,
memo: Vec<(SampleKey, Option<f64>)>,
classes: Vec<usize>,
draws: Vec<f64>,
means: Vec<f64>,
}
impl<'a, O: DenseId + Copy> Sampling<'a, O> {
fn new(sampler: &'a dyn CrossSampler<O>, cfg: SamplerConfig) -> Self {
Self {
sampler,
cfg,
drivers: Vec::new(),
memo: Vec::new(),
classes: Vec::new(),
draws: Vec::new(),
means: Vec::new(),
}
}
fn driver(&mut self, atom_id: usize, op: O) -> usize {
if let Some(i) = self.drivers.iter().position(|(a, _)| *a == atom_id) {
return i;
}
let mut nodes = Vec::new();
self.sampler
.driver_sample(atom_id, op, self.cfg.k, &mut nodes);
self.drivers.push((atom_id, nodes));
self.drivers.len() - 1
}
fn estimate(
&mut self,
emitter: usize,
emitter_op: O,
site: KeySite,
path: ProbePath,
probe_op: O,
) -> Option<f64> {
let key: SampleKey = (emitter, site, probe_op.to_usize(), path);
if let Some((_, v)) = self.memo.iter().find(|(k, _)| *k == key) {
return *v;
}
let v = self.measure(emitter, emitter_op, site, path, probe_op);
self.memo.push((key, v));
v
}
fn measure(
&mut self,
emitter: usize,
emitter_op: O,
site: KeySite,
path: ProbePath,
probe_op: O,
) -> Option<f64> {
let di = self.driver(emitter, emitter_op);
let nodes = std::mem::take(&mut self.drivers[di].1);
let out = self.sample_draws(&nodes, site, path, probe_op);
self.drivers[di].1 = nodes;
out
}
fn sample_draws(
&mut self,
nodes: &[usize],
site: KeySite,
path: ProbePath,
probe_op: O,
) -> Option<f64> {
if nodes.is_empty() {
return None;
}
self.draws.clear();
for &n in nodes {
self.classes.clear();
self.sampler.key_classes(n, site, &mut self.classes);
if self.classes.is_empty() {
continue;
}
let sum: usize = self
.classes
.iter()
.map(|&c| self.sampler.probe_len(c, path, probe_op))
.sum();
self.draws.push(sum as f64 / self.classes.len() as f64);
}
if self.draws.is_empty() {
return None;
}
let mean = self.draws.iter().sum::<f64>() / self.draws.len() as f64;
let rejected = self.cfg.bootstrap > 0 && !self.bootstrap_ok(mean);
tally(rejected);
if rejected { None } else { Some(mean) }
}
fn bootstrap_ok(&mut self, mean: f64) -> bool {
if mean <= 0.0 {
return self.draws.iter().all(|&d| d == 0.0);
}
let n = self.draws.len();
let mut state = 0x2545_F491_4F6C_DD1Du64;
self.means.clear();
for _ in 0..self.cfg.bootstrap {
let mut acc = 0.0;
for _ in 0..n {
acc += self.draws[(splitmix64(&mut state) % n as u64) as usize];
}
self.means.push(acc / n as f64);
}
let b = self.means.len() as f64;
let bmean = self.means.iter().sum::<f64>() / b;
let var = self
.means
.iter()
.map(|m| (m - bmean) * (m - bmean))
.sum::<f64>()
/ b;
var.sqrt() / mean <= self.cfg.cv_threshold
}
}
fn splitmix64(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
type Emitters = Vec<Option<(usize, KeySite)>>;
fn record_emitters<O, S, V>(
atoms: &[RAtom<O, S, V>],
before: &[bool],
used: &[bool],
em: &mut Emitters,
) {
fn set(em: &mut Emitters, v: usize, src: (usize, KeySite)) {
if em[v].is_none() {
em[v] = Some(src);
}
}
fn set_pv(em: &mut Emitters, pv: &PatVar, src: (usize, KeySite)) {
if let PatVar::Local(v) = pv {
set(em, v.idx(), src);
}
}
for (ai, atom) in atoms.iter().enumerate() {
if used[ai] == before[ai] {
continue;
}
match atom {
RAtom::Plain { node, children, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node));
for (pos, cv) in children.iter().enumerate() {
set_pv(em, cv, (ai, KeySite::Child(pos)));
}
}
RAtom::AExact { node, children, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node));
for cv in children {
set_pv(em, cv, (ai, KeySite::Element));
}
}
RAtom::APrefix { node, fixed, .. }
| RAtom::ASuffix { node, fixed, .. }
| RAtom::ABoth { node, fixed, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node));
for cv in fixed {
set_pv(em, cv, (ai, KeySite::Element));
}
}
RAtom::ACExact { node, elems, .. } | RAtom::ACSub { node, elems, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node));
for (ev, _) in elems {
set_pv(em, ev, (ai, KeySite::Element));
}
}
RAtom::ACIExact { node, elems, .. } | RAtom::ACISub { node, elems, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node));
for ev in elems {
set_pv(em, ev, (ai, KeySite::Element));
}
}
RAtom::Lit { node, .. } | RAtom::LitBind { node, .. } => {
set(em, (*node).idx(), (ai, KeySite::Node))
}
RAtom::Eq(a, b) => {
if em[(*a).idx()].is_none() {
em[(*a).idx()] = em[(*b).idx()];
} else if em[(*b).idx()].is_none() {
em[(*b).idx()] = em[(*a).idx()];
}
}
RAtom::EqGlobal(..) | RAtom::Pred { .. } => {}
}
}
}
fn emitter_op<O: Copy, S, V>(atom: &RAtom<O, S, V>) -> Option<O> {
crate::saturate::atom_op(atom)
}
struct Cost<'a, O: Eq + Hash> {
stats: &'a IndexStats<O>,
emitters: Emitters,
sampling: Option<Sampling<'a, O>>,
}
impl<'a, O: DenseId + Hash + Copy> Cost<'a, O> {
fn sampled<S, V>(
&mut self,
atoms: &[RAtom<O, S, V>],
key: &PatVar,
path: ProbePath,
probe_op: O,
) -> Option<f64> {
let PatVar::Local(v) = key else { return None };
let (emitter, site) = (*self.emitters.get(v.idx())?)?;
let emitter_op = emitter_op(atoms.get(emitter)?)?;
self.sampling
.as_mut()?
.estimate(emitter, emitter_op, site, path, probe_op)
}
}
fn estimate_cost<O: DenseId + Hash + Copy, S, V>(
atoms: &[RAtom<O, S, V>],
atom_id: usize,
bound: &[bool],
ctx: &mut Cost<'_, O>,
) -> f64 {
let atom = &atoms[atom_id];
let stats = ctx.stats;
match atom {
RAtom::Plain { node, op, children } => {
if bound[(*node).idx()] {
return by_repr_cost(op, atom_id, stats);
}
let full = stats.op_card.get(op).copied().unwrap_or(0);
let mut cost = base_card(op, atom_id, stats) as f64;
for (pos, cv) in children.iter().enumerate() {
if pv_is_bound(cv, bound) {
let f = ctx
.sampled(atoms, cv, ProbePath::ChildPos(pos), *op)
.or_else(|| ctx.stats.fanouts.by_child_pos.get(&(*op, pos)).copied());
cost *= path_selectivity(f, full);
}
}
cost
}
RAtom::AExact { node, op, children } => {
by_contains_cost(atoms, node, op, atom_id, children, bound, ctx)
}
RAtom::APrefix {
node, op, fixed, ..
}
| RAtom::ASuffix {
node, op, fixed, ..
}
| RAtom::ABoth {
node, op, fixed, ..
} => by_contains_cost(atoms, node, op, atom_id, fixed, bound, ctx),
RAtom::ACExact { node, op, elems }
| RAtom::ACSub {
node, op, elems, ..
} => {
let evs: Vec<PatVar> = elems.iter().map(|(ev, _)| *ev).collect();
by_contains_cost(atoms, node, op, atom_id, &evs, bound, ctx)
}
RAtom::ACIExact { node, op, elems }
| RAtom::ACISub {
node, op, elems, ..
} => by_contains_cost(atoms, node, op, atom_id, elems, bound, ctx),
RAtom::Lit { op, .. } | RAtom::LitBind { op, .. } => base_card(op, atom_id, stats) as f64,
RAtom::Eq(..) | RAtom::EqGlobal(..) | RAtom::Pred { .. } => 0.0,
}
}
fn by_repr_cost<O: Eq + Hash>(op: &O, atom_id: usize, stats: &IndexStats<O>) -> f64 {
let sel = path_selectivity(Some(stats.fanouts.by_repr), stats.fanouts.nodes);
base_card(op, atom_id, stats) as f64 * sel
}
#[allow(clippy::too_many_arguments)]
fn by_contains_cost<O: DenseId + Hash + Copy, S, V>(
atoms: &[RAtom<O, S, V>],
node: &VarId,
op: &O,
atom_id: usize,
elems: &[PatVar],
bound: &[bool],
ctx: &mut Cost<'_, O>,
) -> f64 {
if bound[(*node).idx()] {
return by_repr_cost(op, atom_id, ctx.stats);
}
let full = ctx.stats.op_card.get(op).copied().unwrap_or(0);
let mut cost = base_card(op, atom_id, ctx.stats) as f64;
for e in elems {
if pv_is_bound(e, bound) {
let f = ctx
.sampled(atoms, e, ProbePath::Contains, *op)
.or_else(|| ctx.stats.fanouts.by_contains.get(op).copied());
cost *= path_selectivity(f, full);
}
}
cost
}
pub fn schedule<O: DenseId + Hash + Copy, S: DenseId + Copy, V: Clone, I: IndexLike>(
rq: &ResolvedQuery<O, S, V>,
) -> QueryPlan<O, I, V> {
schedule_with_stats(rq, &IndexStats::new())
}
pub fn schedule_with_stats<O: DenseId + Hash + Copy, S: DenseId + Copy, V: Clone, I: IndexLike>(
rq: &ResolvedQuery<O, S, V>,
stats: &IndexStats<O>,
) -> QueryPlan<O, I, V> {
schedule_inner(rq, stats, None)
}
pub fn schedule_with_stats_sampled<
O: DenseId + Hash + Copy,
S: DenseId + Copy,
V: Clone,
I: IndexLike,
>(
rq: &ResolvedQuery<O, S, V>,
stats: &IndexStats<O>,
sampler: &dyn CrossSampler<O>,
) -> QueryPlan<O, I, V> {
schedule_inner(rq, stats, sampled_selectivity().map(|cfg| (sampler, cfg)))
}
fn schedule_inner<O: DenseId + Hash + Copy, S: DenseId + Copy, V: Clone, I: IndexLike>(
rq: &ResolvedQuery<O, S, V>,
stats: &IndexStats<O>,
sampling: Option<(&dyn CrossSampler<O>, SamplerConfig)>,
) -> QueryPlan<O, I, V> {
let mut bound = vec![false; rq.shape.num_vars()];
let mut steps = Vec::new();
let mut used = vec![false; rq.atoms.len()];
let mut ctx = Cost {
stats,
emitters: vec![None; rq.shape.num_vars()],
sampling: sampling.map(|(s, cfg)| Sampling::new(s, cfg)),
};
let mut before = used.clone();
loop {
lower_eager(&rq.atoms, &mut used, &mut bound, &mut steps);
record_emitters(&rq.atoms, &before, &used, &mut ctx.emitters);
before.copy_from_slice(&used);
let mut best: Option<(usize, f64)> = None;
for ai in 0..rq.atoms.len() {
if used[ai]
|| matches!(
&rq.atoms[ai],
RAtom::Eq(..) | RAtom::EqGlobal(..) | RAtom::Pred { .. }
)
{
continue;
}
let c = estimate_cost(&rq.atoms, ai, &bound, &mut ctx);
if best.is_none_or(|(_, b)| c < b) {
best = Some((ai, c));
}
}
let Some((ai, _)) = best else { break };
emit_atom(&rq.atoms[ai], ai, &mut bound, &mut steps);
used[ai] = true;
record_emitters(&rq.atoms, &before, &used, &mut ctx.emitters);
before.copy_from_slice(&used);
}
if dump_plan_enabled() {
dump_plan(&steps, rq.atoms.len());
}
QueryPlan {
steps,
shape: rq.shape.clone(),
}
}
fn dump_plan_enabled() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("EGRAPH_DUMP_PLAN").is_some())
}
fn dump_plan<O: std::fmt::Debug, I: std::fmt::Debug, V>(steps: &[Step<O, I, V>], atoms: usize) {
eprintln!("=== plan: {} atoms, {} steps ===", atoms, steps.len());
for (i, st) in steps.iter().enumerate() {
eprintln!("step[{i}]: {}", fmt_step(st));
}
}
fn fmt_step<O: std::fmt::Debug, I: std::fmt::Debug, V>(st: &Step<O, I, V>) -> String {
match st {
Step::BindGlobal { target, global } => {
format!("BindGlobal target=v{} global={global:?}", target.idx())
}
Step::Join {
target,
lookups,
atom_id,
} => format!(
"Join target=v{} atom={atom_id} lookups={lookups:?}",
target.idx()
),
Step::ExtractChild {
target,
parent,
pos,
} => format!(
"ExtractChild target=v{} parent=v{} pos={pos:?}",
target.idx(),
parent.idx()
),
Step::CheckChildEq {
parent,
pos,
expected,
} => format!(
"CheckChildEq parent=v{} pos={pos:?} expected={expected:?}",
parent.idx()
),
Step::CheckEq { a, b } => format!("CheckEq v{} v{}", a.idx(), b.idx()),
Step::CheckEqGlobal { local, global } => {
format!("CheckEqGlobal v{} global={global:?}", local.idx())
}
Step::CopyBinding { target, other } => {
format!("CopyBinding target=v{} from=v{}", target.idx(), other.idx())
}
Step::ExpandA {
node,
children,
pre,
suf,
} => format!(
"ExpandA node=v{} children={children:?} pre={pre:?} suf={suf:?}",
node.idx()
),
Step::DecomposeAC {
node,
elems,
rest,
idempotent,
} => format!(
"DecomposeAC node=v{} elems={elems:?} rest={rest:?} idempotent={idempotent}",
node.idx()
),
Step::DecomposeACI { node, elems, rest } => format!(
"DecomposeACI node=v{} elems={elems:?} rest={rest:?}",
node.idx()
),
Step::ExtractLitVal { node, val } => {
format!("ExtractLitVal node=v{} val={val:?}", node.idx())
}
Step::CheckLit { node, .. } => format!("CheckLit node=v{}", node.idx()),
Step::CheckPred { .. } => "CheckPred".to_string(),
}
}
fn emit_read_children<O: DenseId + Hash + Copy, S, V, I: IndexLike>(
atom: &RAtom<O, S, V>,
bound: &mut [bool],
steps: &mut Vec<Step<O, I, V>>,
) {
if let RAtom::Plain { node, children, .. } = atom {
for (pos, &cv) in children.iter().enumerate() {
if !pv_is_bound(&cv, bound) {
let PatVar::Local(vid) = cv else {
unreachable!()
};
steps.push(Step::ExtractChild {
target: vid,
parent: *node,
pos: child_pos::<I>(pos),
});
pv_mark_bound(&cv, bound);
} else {
steps.push(Step::CheckChildEq {
parent: *node,
pos: child_pos::<I>(pos),
expected: cv,
});
}
}
}
}
pub(crate) fn lower_eager<O: DenseId + Hash + Copy, S, V: Clone, I: IndexLike>(
atoms: &[RAtom<O, S, V>],
used: &mut [bool],
bound: &mut [bool],
steps: &mut Vec<Step<O, I, V>>,
) {
let mut progress = true;
while progress {
progress = false;
for ai in 0..atoms.len() {
if used[ai] {
continue;
}
if let RAtom::Pred { guard, deps } = &atoms[ai] {
if deps.iter().all(|d| used[*d]) {
steps.push(Step::CheckPred {
guard: guard.clone(),
});
used[ai] = true;
progress = true;
}
continue;
}
if let Some(eager) = try_eager_lower(&atoms[ai], ai, bound) {
steps.extend(eager);
used[ai] = true;
progress = true;
}
}
}
}
pub(crate) fn emit_atom<O: DenseId + Hash + Copy, S, V: Clone, I: IndexLike>(
atom: &RAtom<O, S, V>,
atom_id: usize,
bound: &mut [bool],
steps: &mut Vec<Step<O, I, V>>,
) {
match atom {
RAtom::Plain { node, op, children } => {
let mut lookups = vec![IndexLookup::ByOp { op: *op }];
for (pos, &cv) in children.iter().enumerate() {
if pv_is_bound(&cv, bound) {
lookups.push(IndexLookup::ByChildPos {
child: cv,
pos: child_pos::<I>(pos),
});
}
}
steps.push(Step::Join {
target: *node,
lookups,
atom_id,
});
bound[(*node).idx()] = true;
for (pos, &cv) in children.iter().enumerate() {
if !pv_is_bound(&cv, bound) {
let PatVar::Local(vid) = cv else {
unreachable!()
};
steps.push(Step::ExtractChild {
target: vid,
parent: *node,
pos: child_pos::<I>(pos),
});
pv_mark_bound(&cv, bound);
} else {
steps.push(Step::CheckChildEq {
parent: *node,
pos: child_pos::<I>(pos),
expected: cv,
});
}
}
}
RAtom::Lit {
node, op, value, ..
} => {
if !bound[(*node).idx()] {
steps.push(Step::Join {
target: *node,
lookups: vec![IndexLookup::ByOp { op: *op }],
atom_id,
});
bound[(*node).idx()] = true;
}
steps.push(Step::CheckLit {
node: *node,
value: value.clone(),
});
}
RAtom::LitBind { node, op, val } => {
if !bound[(*node).idx()] {
steps.push(Step::Join {
target: *node,
lookups: vec![IndexLookup::ByOp { op: *op }],
atom_id,
});
bound[(*node).idx()] = true;
}
steps.push(Step::ExtractLitVal {
node: *node,
val: *val,
});
}
RAtom::Eq(..) | RAtom::EqGlobal(..) | RAtom::Pred { .. } => {}
RAtom::AExact { node, op, children } => {
emit_variadic_join(node, *op, atom_id, children, bound, steps);
steps.push(Step::ExpandA {
node: *node,
children: children.clone(),
pre: None,
suf: None,
});
for &cv in children {
pv_mark_bound(&cv, bound);
}
}
RAtom::APrefix {
node,
op,
pre,
fixed,
} => {
emit_variadic_join(node, *op, atom_id, fixed, bound, steps);
steps.push(Step::ExpandA {
node: *node,
children: fixed.clone(),
pre: Some(*pre),
suf: None,
});
for &cv in fixed {
pv_mark_bound(&cv, bound);
}
}
RAtom::ASuffix {
node,
op,
fixed,
suf,
} => {
emit_variadic_join(node, *op, atom_id, fixed, bound, steps);
steps.push(Step::ExpandA {
node: *node,
children: fixed.clone(),
pre: None,
suf: Some(*suf),
});
for &cv in fixed {
pv_mark_bound(&cv, bound);
}
}
RAtom::ABoth {
node,
op,
pre,
fixed,
suf,
} => {
emit_variadic_join(node, *op, atom_id, fixed, bound, steps);
steps.push(Step::ExpandA {
node: *node,
children: fixed.clone(),
pre: Some(*pre),
suf: Some(*suf),
});
for &cv in fixed {
pv_mark_bound(&cv, bound);
}
}
RAtom::ACExact { node, op, elems } => {
let evs: Vec<PatVar> = elems.iter().map(|(ev, _)| *ev).collect();
emit_variadic_join(node, *op, atom_id, &evs, bound, steps);
steps.push(Step::DecomposeAC {
node: *node,
elems: elems.clone(),
rest: None,
idempotent: false,
});
for (ev, _) in elems {
pv_mark_bound(ev, bound);
}
}
RAtom::ACSub {
node,
op,
elems,
rest,
} => {
let evs: Vec<PatVar> = elems.iter().map(|(ev, _)| *ev).collect();
emit_variadic_join(node, *op, atom_id, &evs, bound, steps);
steps.push(Step::DecomposeAC {
node: *node,
elems: elems.clone(),
rest: Some(*rest),
idempotent: false,
});
for (ev, _) in elems {
pv_mark_bound(ev, bound);
}
}
RAtom::ACIExact { node, op, elems } => {
emit_variadic_join(node, *op, atom_id, elems, bound, steps);
steps.push(Step::DecomposeACI {
node: *node,
elems: elems.clone(),
rest: None,
});
for &ev in elems {
pv_mark_bound(&ev, bound);
}
}
RAtom::ACISub {
node,
op,
elems,
rest,
} => {
emit_variadic_join(node, *op, atom_id, elems, bound, steps);
steps.push(Step::DecomposeACI {
node: *node,
elems: elems.clone(),
rest: Some(*rest),
});
for &ev in elems {
pv_mark_bound(&ev, bound);
}
}
}
}
pub(crate) fn try_eager_lower<O: DenseId + Hash + Copy, S, V: Clone, I: IndexLike>(
atom: &RAtom<O, S, V>,
atom_id: usize,
bound: &mut [bool],
) -> Option<Vec<Step<O, I, V>>> {
let mut steps = Vec::new();
match atom {
RAtom::Eq(a, b) => {
if bound[(*a).idx()] && bound[(*b).idx()] {
steps.push(Step::CheckEq { a: *a, b: *b });
} else if bound[(*a).idx()] {
steps.push(Step::CopyBinding {
target: *b,
other: *a,
});
bound[(*b).idx()] = true;
} else if bound[(*b).idx()] {
steps.push(Step::CopyBinding {
target: *a,
other: *b,
});
bound[(*a).idx()] = true;
} else {
return None;
}
}
RAtom::EqGlobal(local, global) if bound[(*local).idx()] => {
steps.push(Step::CheckEqGlobal {
local: *local,
global: *global,
});
}
RAtom::EqGlobal(local, global) => {
steps.push(Step::BindGlobal {
target: *local,
global: *global,
});
bound[(*local).idx()] = true;
}
RAtom::Lit {
node, op, value, ..
} if bound[(*node).idx()] => {
steps.push(Step::Join {
target: *node,
lookups: vec![
IndexLookup::ByRepr { repr: *node },
IndexLookup::ByOp { op: *op },
],
atom_id,
});
steps.push(Step::CheckLit {
node: *node,
value: value.clone(),
});
}
RAtom::LitBind { node, op, val } if bound[(*node).idx()] => {
steps.push(Step::Join {
target: *node,
lookups: vec![
IndexLookup::ByRepr { repr: *node },
IndexLookup::ByOp { op: *op },
],
atom_id,
});
steps.push(Step::ExtractLitVal {
node: *node,
val: *val,
});
}
RAtom::Plain { node, op, .. } if bound[(*node).idx()] => {
steps.push(Step::Join {
target: *node,
lookups: vec![
IndexLookup::ByRepr { repr: *node },
IndexLookup::ByOp { op: *op },
],
atom_id,
});
emit_read_children(atom, bound, &mut steps);
}
_ => return None,
}
Some(steps)
}
fn emit_variadic_join<O: DenseId + Hash + Copy, I: IndexLike, V>(
node: &VarId,
op: O,
atom_id: usize,
elems: &[PatVar],
bound: &mut [bool],
steps: &mut Vec<Step<O, I, V>>,
) {
if !bound[(*node).idx()] {
let mut lookups = vec![IndexLookup::ByOp { op }];
for &pv in elems {
if pv_is_bound(&pv, bound) {
lookups.push(IndexLookup::ByContains { child: pv });
}
}
steps.push(Step::Join {
target: *node,
lookups,
atom_id,
});
bound[(*node).idx()] = true;
} else {
steps.push(Step::Join {
target: *node,
lookups: vec![
IndexLookup::ByRepr { repr: *node },
IndexLookup::ByOp { op },
],
atom_id,
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id::{OpId, SortId};
use crate::lit_model::LitModel;
use crate::literal::{LitValStore, NiraLitVal, NiraModel};
use crate::nodes::LitValId;
use crate::registry::{OpRegistry, SortRegistry};
use crate::resolve::MatchShape;
use crate::resolve::resolve;
use crate::sortcheck::flatten_surface as flatten;
use crate::test_helpers::parse_pattern;
fn setup() -> (
OpRegistry<OpId, SortId, false>,
SortRegistry<SortId, false>,
LitValStore<NiraLitVal, LitValId, false>,
) {
let model = NiraModel;
let mut sorts: SortRegistry<SortId, false> = SortRegistry::new();
let sort_names: Vec<&str> = model.sorts().iter().map(|s| s.name).collect();
sorts.register_builtins(&sort_names);
let e = sorts.intern("IExpr");
let mut ops = OpRegistry::new();
ops.register_builtins(&model, &sorts);
ops.register("f", &[e, e], e);
ops.register("g", &[e], e);
ops.register("h", &[e, e], e);
ops.register_a("concat", e, e, crate::registry::AssocDir::Right);
ops.register_mset("add", e, e);
ops.register_set("union", e, e);
let ibig = sorts.id_by_name("IBig").expect("IBig builtin");
ops.register("ILit", &[ibig], e);
(ops, sorts, LitValStore::new())
}
fn do_plan(src: &str) -> (QueryPlan<OpId, u32, NiraLitVal>, MatchShape) {
let (ops, sorts, _) = setup();
let model = NiraModel;
let pat = parse_pattern(src);
let fq = flatten(&[pat], &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&model,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
(schedule(&rq), rq.shape)
}
fn do_plan_multi(srcs: &[&str]) -> (QueryPlan<OpId, u32, NiraLitVal>, MatchShape) {
let (ops, sorts, _) = setup();
let model = NiraModel;
let pats: Vec<_> = srcs.iter().map(|s| parse_pattern(s)).collect();
let fq = flatten(&pats, &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&model,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
(schedule(&rq), rq.shape)
}
fn do_plan_with_stats(
srcs: &[&str],
card: &[(&str, usize)],
) -> (QueryPlan<OpId, u32, NiraLitVal>, MatchShape) {
let (ops, sorts, _) = setup();
let model = NiraModel;
let pats: Vec<_> = srcs.iter().map(|s| parse_pattern(s)).collect();
let fq = flatten(&pats, &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&model,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
let mut stats = IndexStats::new();
for &(name, c) in card {
let op_id = ops.id_by_name(name).unwrap();
stats.op_card.insert(op_id, c);
}
(schedule_with_stats(&rq, &stats), rq.shape)
}
#[test]
fn guard_runs_as_soon_as_its_values_are_bound() {
let (plan, _) = do_plan_multi(&["(f (ILit a) (ILit b))", "(< a b)", "(g y)"]);
let pred = plan
.steps
.iter()
.position(|s| matches!(s, Step::CheckPred { .. }))
.expect("guard step");
let last_extract = plan
.steps
.iter()
.rposition(|s| matches!(s, Step::ExtractLitVal { .. }))
.expect("lit-val extraction");
assert_eq!(pred, last_extract + 1);
assert!(
plan.steps[pred + 1..]
.iter()
.any(|s| matches!(s, Step::Join { .. }))
);
}
#[test]
fn root_binding_copies_the_root() {
let (plan, shape) = do_plan("(= v (g x))");
let v = shape.find_var("v").unwrap();
assert!(
plan.steps
.iter()
.any(|s| matches!(s, Step::CopyBinding { target, .. } if *target == v))
);
}
#[test]
fn plain_flat() {
let (qp, _) = do_plan("(f x y)");
assert_eq!(qp.steps.len(), 3);
assert!(matches!(&qp.steps[0], Step::Join { lookups, .. }
if matches!(&lookups[0], IndexLookup::ByOp { .. })));
assert!(matches!(&qp.steps[1], Step::ExtractChild { pos: 0, .. }));
assert!(matches!(&qp.steps[2], Step::ExtractChild { pos: 1, .. }));
}
#[test]
fn nested() {
let (qp, _) = do_plan("(f x (g y))");
let join_count = qp
.steps
.iter()
.filter(|s| matches!(s, Step::Join { .. }))
.count();
assert_eq!(join_count, 2);
}
#[test]
fn multi_atom_shared_var() {
let (qp, vars) = do_plan_multi(&["(f x y)", "(g y)"]);
let y = vars.find_var("y").unwrap();
let g_join = qp.steps.iter().find(|s| match s {
Step::Join { lookups, .. } => lookups.len() > 1,
_ => false,
});
assert!(g_join.is_some());
if let Step::Join { lookups, .. } = g_join.unwrap() {
assert!(
lookups
.iter()
.any(|l| matches!(l, IndexLookup::ByChildPos { child, pos: 0 } if *child == PatVar::Local(y)))
);
}
}
#[test]
fn literal_atom_joins_and_checks_payload() {
let (qp, _) = do_plan("(f (ILit 42) x)");
for step in &qp.steps {
if let Step::Join { lookups, .. } = step {
assert!(
!lookups.is_empty(),
"a join with no lookups yields no candidates: {qp:?}"
);
}
}
let checks: Vec<_> = qp
.steps
.iter()
.filter(|s| matches!(s, Step::CheckLit { .. }))
.collect();
assert_eq!(checks.len(), 1, "expected one payload check in {qp:?}");
assert!(
matches!(checks[0], Step::CheckLit { value, .. } if value.to_string() == "42"),
"payload check does not carry the pattern's literal: {qp:?}"
);
}
#[test]
fn nonlinear_check_eq() {
let (qp, _) = do_plan("(f x x)");
assert!(
qp.steps
.iter()
.any(|s| matches!(s, Step::CheckChildEq { .. }))
);
}
#[test]
fn ac_subset() {
let (qp, _) = do_plan("(add x:2 ..rest)");
assert!(qp.steps.iter().any(|s| matches!(
s,
Step::DecomposeAC {
rest: Some(_),
idempotent: false,
..
}
)));
}
#[test]
fn aci_subset() {
let (qp, _) = do_plan("(union x y ..rest)");
assert!(
qp.steps
.iter()
.any(|s| matches!(s, Step::DecomposeACI { rest: Some(_), .. }))
);
}
#[test]
fn a_prefix() {
let (qp, _) = do_plan("(concat ..pre x y)");
assert!(qp.steps.iter().any(|s| matches!(
s,
Step::ExpandA {
pre: Some(_),
suf: None,
..
}
)));
}
#[test]
fn selectivity_picks_rarest() {
let (qp, _) = do_plan_with_stats(&["(f x (g y))"], &[("f", 10_000), ("g", 10)]);
let first_join = qp
.steps
.iter()
.find(|s| matches!(s, Step::Join { .. }))
.unwrap();
if let Step::Join { lookups, .. } = first_join {
let (ops, _, _) = setup();
let g_id = ops.id_by_name("g").unwrap();
assert!(
lookups
.iter()
.any(|l| matches!(l, IndexLookup::ByOp { op } if *op == g_id))
);
}
}
#[test]
fn selectivity_three_atoms() {
let (qp, _) = do_plan_with_stats(
&["(f x y)", "(g y)", "(h y w)"],
&[("f", 10_000), ("g", 500), ("h", 5)],
);
let (ops, _, _) = setup();
let join_ops: Vec<OpId> = qp
.steps
.iter()
.filter_map(|s| match s {
Step::Join { lookups, .. } => lookups.iter().find_map(|l| match l {
IndexLookup::ByOp { op } => Some(*op),
_ => None,
}),
_ => None,
})
.collect();
let h = ops.id_by_name("h").unwrap();
let g = ops.id_by_name("g").unwrap();
let f = ops.id_by_name("f").unwrap();
assert_eq!(join_ops, [h, g, f]);
}
#[test]
fn bound_child_reduces_cost() {
let (qp, _) = do_plan_with_stats(&["(f x y)", "(h y z)"], &[("f", 1000), ("h", 1000)]);
let second_join = qp
.steps
.iter()
.filter(|s| matches!(s, Step::Join { .. }))
.nth(1);
if let Some(Step::Join { lookups, .. }) = second_join {
assert!(
lookups
.iter()
.any(|l| matches!(l, IndexLookup::ByChildPos { .. }))
);
}
}
#[test]
fn measured_fanouts_beat_cardinalities_on_sibling_atoms() {
let (ops, _, _) = setup();
let f = ops.id_by_name("f").unwrap();
let h = ops.id_by_name("h").unwrap();
let mut stats = IndexStats::<OpId>::new();
stats.op_card.insert(f, 54_051);
stats.op_card.insert(h, 86_061);
stats.fanouts.nodes = 216_061;
stats.fanouts.by_repr = 2.51;
stats.fanouts.by_child_pos.insert((f, 0), 1_239.0);
stats.fanouts.by_child_pos.insert((f, 1), 1_239.0);
stats.fanouts.by_child_pos.insert((h, 0), 1.5);
stats.fanouts.by_child_pos.insert((h, 1), 1.5);
let join_ops = |stats: &IndexStats<OpId>| -> Vec<OpId> {
let (ops, sorts, _) = setup();
let pats = [parse_pattern("(h (f a b) (f a c))")];
let fq = flatten(&pats, &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&NiraModel,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
let qp: QueryPlan<OpId, u32, NiraLitVal> = schedule_with_stats(&rq, stats);
qp.steps
.iter()
.filter_map(|s| match s {
Step::Join { lookups, .. } => lookups.iter().find_map(|l| match l {
IndexLookup::ByOp { op } => Some(*op),
_ => None,
}),
_ => None,
})
.collect()
};
let mut halving = IndexStats::<OpId>::new();
halving.op_card.insert(f, 54_051);
halving.op_card.insert(h, 86_061);
assert_eq!(
join_ops(&halving),
[f, f, h],
"the fixed halving should still join the second f before h"
);
assert_eq!(
join_ops(&stats),
[f, h, f],
"measured fan-outs should join h second, leaving the second f a class re-join"
);
}
#[test]
fn bound_element_discounts_variadic_cost() {
let (ops, _, _) = setup();
let add = ops.id_by_name("add").unwrap();
let mut stats = IndexStats::<OpId>::new();
stats.op_card.insert(add, 1000);
let atom = RAtom::<OpId, SortId, NiraLitVal>::ACSub {
node: VarId::new(1),
op: add,
elems: vec![(PatVar::Local(VarId::new(0)), RMult::Exact(1))],
rest: crate::ast::MsetVarId::new(0),
};
let cost_of = |bound: &[bool]| {
let mut ctx = Cost {
stats: &stats,
emitters: vec![None; bound.len()],
sampling: None,
};
estimate_cost(std::slice::from_ref(&atom), 0, bound, &mut ctx)
};
let bound_none = [false, false];
assert_eq!(cost_of(&bound_none), 1000.0);
let bound_x = [true, false];
let cost_bound = cost_of(&bound_x);
assert!(
cost_bound < 1000.0,
"binding an element must discount a variadic atom's cost, got {cost_bound}"
);
assert_eq!(
cost_bound, 500.0,
"with no measured fan-out one bound element halves the estimate"
);
}
struct StubSampler {
lens: std::collections::HashMap<OpId, usize>,
drawn: std::cell::RefCell<Vec<usize>>,
}
impl CrossSampler<OpId> for StubSampler {
fn driver_sample(&self, atom_id: usize, _op: OpId, k: usize, out: &mut Vec<usize>) {
self.drawn.borrow_mut().push(atom_id);
out.clear();
out.extend(0..k);
}
fn key_classes(&self, node: usize, _site: KeySite, out: &mut Vec<usize>) {
out.push(node);
}
fn probe_len(&self, _class: usize, _path: ProbePath, op: OpId) -> usize {
self.lens.get(&op).copied().unwrap_or(0)
}
}
#[test]
fn a_sampled_bucket_displaces_the_mean_fanout() {
let (ops, sorts, _) = setup();
let (f, g, h) = (
ops.id_by_name("f").unwrap(),
ops.id_by_name("g").unwrap(),
ops.id_by_name("h").unwrap(),
);
let mut stats = IndexStats::<OpId>::new();
stats.op_card.insert(f, 10);
stats.op_card.insert(g, 1_000);
stats.op_card.insert(h, 1_000);
stats.fanouts.by_child_pos.insert((g, 0), 1.0);
stats.fanouts.by_child_pos.insert((h, 0), 500.0);
let pats: Vec<_> = ["(f x y)", "(g y)", "(h y w)"]
.iter()
.map(|s| parse_pattern(s))
.collect();
let fq = flatten(&pats, &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&NiraModel,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
let join_ops = |plan: &QueryPlan<OpId, u32, NiraLitVal>| -> Vec<OpId> {
plan.steps
.iter()
.filter_map(|s| match s {
Step::Join { lookups, .. } => lookups.iter().find_map(|l| match l {
IndexLookup::ByOp { op } => Some(*op),
_ => None,
}),
_ => None,
})
.collect()
};
assert_eq!(
join_ops(&schedule_with_stats(&rq, &stats)),
[f, g, h],
"the mean should join g second"
);
let stub = StubSampler {
lens: [(g, 400usize), (h, 2usize)].into_iter().collect(),
drawn: std::cell::RefCell::new(Vec::new()),
};
let cfg = SamplerConfig::default();
let plan: QueryPlan<OpId, u32, NiraLitVal> =
schedule_inner(&rq, &stats, Some((&stub, cfg)));
assert_eq!(
join_ops(&plan),
[f, h, g],
"the sampled lengths should join h second"
);
let drawn = stub.drawn.into_inner();
assert_eq!(
drawn,
vec![0],
"the draw must come once from the atom that bound the key"
);
}
#[test]
fn the_flag_off_path_never_samples() {
let (ops, sorts, _) = setup();
let pats = [parse_pattern("(f x y)"), parse_pattern("(g y)")];
let fq = flatten(&pats, &ops).unwrap();
let rq = resolve(
&fq,
&ops,
&sorts,
&NiraModel,
&crate::resolve::GlobalCtx::<_, ()>::new(),
)
.unwrap();
let stub = StubSampler {
lens: std::collections::HashMap::new(),
drawn: std::cell::RefCell::new(Vec::new()),
};
assert_eq!(sampled_selectivity(), None, "the flag defaults to off");
let plan: QueryPlan<OpId, u32, NiraLitVal> =
schedule_with_stats_sampled(&rq, &IndexStats::new(), &stub);
assert_eq!(
plan.steps,
schedule_with_stats::<_, _, _, u32>(&rq, &IndexStats::new()).steps
);
assert!(stub.drawn.into_inner().is_empty());
}
#[test]
fn scheduler_drives_variadic_from_bound_element() {
let (qp, vars) =
do_plan_with_stats(&["(g x)", "(add x ..rest)"], &[("g", 10), ("add", 1000)]);
let x = vars.find_var("x").unwrap();
let has_by_contains = qp.steps.iter().any(|s| match s {
Step::Join { lookups, .. } => lookups.iter().any(
|l| matches!(l, IndexLookup::ByContains { child } if *child == PatVar::Local(x)),
),
_ => false,
});
assert!(
has_by_contains,
"variadic atom with a bound element should drive via ByContains: {:?}",
qp.steps
);
}
}