Skip to main content

Module optimize

Module optimize 

Source
Expand description

Post-parse optimizer — spec §10.

Required pass upstream of compilation. Takes an AST and produces a canonical, push-down form with these properties (per spec §10.6):

  1. Semantic-preserving. Output produces the same dispense sequence (per §9.2).
  2. Idempotent. optimize(optimize(C)) == optimize(C).
  3. Decidable termination. Each rewrite strictly decreases a metadata-derived measure or leaves the AST unchanged.
  4. Bounds-improving. Peak memory never grows.
  5. No rejections. Validity is decided pre-optimizer.

§R-rule catalog

Priority order: R0a → R0b → R1 → R2 → R3 → R4 → R5 → R6 → R7 (spec §10.10.5).

  • R0a — identity elimination (I1–I5): singleton combinators, trivially-true filter, order(Lex, None).
  • R0b — associativity flattening (A1, A2): nested union / cartesian collapse to n-ary form.
  • R1 — order(Lex)ORDER_STREAMING: metadata- driven. The IR compiler (Phase 7) reads metadata.materialization == Streaming for order(Lex, _) and emits ORDER_STREAMING. Not an AST rewrite; recorded in the reducibility catalog as an IR-compilation eligibility.
  • R2 — order(c, strategy, Some(n))indexed_order: metadata-driven. Working set already shrunk via strategy_working_set in metadata.rs’s propagation rule. IR compiler emits ORDER_MATERIALIZE with the indexed variant.
  • R3 — order(filter, Lex, None)filter(order, Lex, None): AST rewrite. Commute when un-truncated.
  • R4 — filter(union(...), p)union(filter(...)): AST rewrite. Distribute filter into each union child.
  • R5 — per-axis filter pushdown: AST rewrite. Consults the predicate analyzer (§10.9) for factorization; when factorization = PerAxis, splits the filter into per-axis filters wrapping each cartesian child.
  • R6 — chained filter folding (F1): AST rewrite. filter(filter(c, p), q)filter(c, p && q).
  • R7 — order chain folding (O1): AST rewrite. order(order(c, s1, None), s2, t)order(c, s2, t).

§Module layout

Re-exports§

pub use finding::ComplexityDelta;
pub use finding::Ordering as ComplexityOrdering;
pub use finding::ReducibilityFinding;
pub use finding::Reduction;
pub use finding::RuleId;

Modules§

finding
ReducibilityFinding and related types — spec §10.10.2.
r0a_identity
R0a — identity elimination (spec §4.2 I1–I5).
r0b_flatten
R0b — associativity flattening (spec §7.1 A1, A2).
r3_commute
R3 — Lex / filter commute (spec §7.5 N2).
r4_distribute
R4 — filter distributes over union (spec §7.3 D1).
r5_factorize
R5 — per-axis filter pushdown (spec §7.3 D2’s optimizer direction + §10.2 R5).
r6_filter_fold
R6 — chained filter folding (spec §7.2 F1).
r7_order_fold
R7 — order chain folding (spec §7.4 O1).

Functions§

analyze_reducibility
Reducibility analyzer entry — spec §10.10.
coord_set_for
Convenience: build a CoordSet from a comprehension’s coordinate names and its computed metadata. R5 uses this when invoking the predicate analyzer.
optimize
Top-level optimizer entry. Applies the R-rule catalog to a fixed point and returns the optimized AST.