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):
- Semantic-preserving. Output produces the same dispense sequence (per §9.2).
- Idempotent.
optimize(optimize(C)) == optimize(C). - Decidable termination. Each rewrite strictly decreases a metadata-derived measure or leaves the AST unchanged.
- Bounds-improving. Peak memory never grows.
- 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) readsmetadata.materialization == Streamingfororder(Lex, _)and emitsORDER_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 viastrategy_working_setinmetadata.rs’s propagation rule. IR compiler emitsORDER_MATERIALIZEwith 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
finding—ReducibilityFinding,Reduction,ComplexityDelta.r0a_identity— I1–I5 elimination.r0b_flatten— A1, A2 flattening.r3_commute— Lex/filter commute.r4_distribute— filter over union.r5_factorize— per-axis filter pushdown.r6_filter_fold— chained filter folding.r7_order_fold— order chain folding.
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
ReducibilityFindingand 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
CoordSetfrom 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.