The equality saturation kernel. It stores expressions in classes of equal expressions, applies the rewrite rules that its caller supplies, restores congruence after merges, and extracts the cheapest expression of a class. The package is generic over the operator type and knows no grammar or regular expression. The regular and context-free packages use it to simplify expressions.
rebuild re-canonicalizes the parents of changed classes from a queue until equal nodes share a class again. (EGraph.rebuild)EGraph.saturate, EGraph.match, Rewrite)EGraph.extract)EGraph.explain, Justification)firstImprovingRewrite, compareRewriteCosts)dumpEGraph)createEGraph, EGraph, ENode)Int32Array table hashes reusable child scratch and compares stored child slots in place. Set children use insertion sort for up to 32 children and typed-array sort for longer vectors, followed by deduplication. Per-class operator lists narrow matching choices. (EGraph.add, EGraph.rebuild, EGraph.match)OperatorSpec, Shape, Analysis, noAnalysis)Pattern, patternVariables)createEGraph receives hasExplanations: true. The option defaults to false, and EGraph.explain requires it. Effective rule names remain available in either mode. (ProofStep, Justification, EGraph.rules)JustificationKind, PatternKind, RuleSource, SaturationStop, ShapeKind)Saturation batches work and storage charges at about 4,096 units and flushes at each rule
boundary. A completed right-hand-side root is merged before a batch check can stop its
compiled rule application. Public additions, merges and readers keep eager budget checks.
The constant analysis supplied by noAnalysis skips analysis joins. Extraction retains
the stable heap order and minimum tree-cost choices.
Class nodes are partitioned by operator, and public reads gather those lists and sort by node creation order. The class size counts allocated nodes, including removed congruent nodes. Node columns grow together when their shared capacity is exhausted. Patterns with one or two node choices and fresh bindings use direct lookup loops. Other patterns use the flat interpreter and its comparison and backtracking instructions.
Run pnpm perf test/egraph/ac.perf.test.ts from the repository root inside
nix develop. Each case saturates five fresh sums and checks the median elapsed time.
Every sample checks saturation, the exact class count and the minimum extraction cost.
The case with seven terms checks correctness. The maintainer sets the timing limits to 125%
of the original Phase A targets.
| Terms | Original target (ms) | Median limit (ms) |
|---|---|---|
| 8 | 140 | 175 |
| 9 | 810 | 1,012.5 |
| 10 | 4,500 | 5,625 |
The performance run at ca690ae0f gives the following medians. All 20 samples satisfy
the saturation, class-count and extraction assertions.
| Terms | Classes | Five-run median (ms) |
|---|---|---|
| 7 | 127 | 34 |
| 8 | 255 | 122 |
| 9 | 511 | 667 |
| 10 | 1,023 | 3,709 |
The comparison pairs the original engine at f39defe9b with the retained Phase A engine
at 7d0419560 using the same standalone AC workload. Seven through nine terms use the
median of three runs. The case with ten terms uses one run. Both engines reach the same
class counts.
| Terms | Classes | Original (ms) | Phase A (ms) |
|---|---|---|---|
| 7 | 127 | 240.42 | 31.86 |
| 8 | 255 | 1,479.62 | 121.01 |
| 9 | 511 | 11,123.87 | 644.48 |
| 10 | 1,023 | 52,883.22 | 4,714.50 |
These standalone measurements use a different sampling method from the performance test's median of five runs.
Profiles use the same repeated standalone eight-term workload. The tables report self sample shares.
| Original hotspot | Self samples |
|---|---|
nodeTuple |
16.2% |
instantiate |
15.1% |
addNode |
10.4% |
Budget.charge |
9.4% |
chooseNode |
5.7% |
applyMatches |
4.8% |
canonicalChildren |
4.6% |
searchClass |
4.6% |
findRow |
4.5% |
| Garbage collection | 4.2% |
| Phase A hotspot | Self samples |
|---|---|
existingPair |
16.5% |
applyPattern |
6.6% |
instantiateNode |
6.4% |
searchInner |
5.4% |
orderedNodes |
4.1% |
canonicalize |
4.1% |
| Garbage collection | 4.0% |
The grammar measurement times the workbench Simplify adapter over all 75 catalogue drafts. Both engines simplify 73 drafts successfully. The total elapsed time is the median of seven runs. The catalogue regression test compares the adapter's results with the snapshot captured from the original engine and matches all 75 outcomes, including the same two unsupported results.
| Workload | Original (ms) | Phase A (ms) |
|---|---|---|
| All catalogue Simplify | 701.43 | 639.20 |
| Warm orbit batch | 76 | 62 |
| Fresh orbit session batch | 215 | 179 |
Orbit timings use the median of five batches of ten solves in the existing checked
session-cache test at
test/decisions/context-free/orbit/cache.perf.test.ts. Both session modes
retain their checked decided outcomes.
The native comparison keeps the existing baseline and repeats the same corpus and settings at the 200 ms fuel tier. Each archive contains 17,081 records. Both engines return 16,895 identical decided answers and 186 unknown outcomes. The matched repeat has zero flips, lost answers, new answers or missing records.
| Matched repeat | Original (ms) | Phase A (ms) |
|---|---|---|
| Total elapsed | 51,146.90 | 40,702.56 |
| Median record | 1.77 | 1.39 |
| 95th percentile record | 5.98 | 4.39 |
Timing varies across run windows. The first pair uses different machine-load windows and records totals of 42,650.55 ms for the original engine and 46,476.26 ms for Phase A. The table reports the matched repeat. These figures describe the measured run windows.
The reviewed fixes are measured at 267cd523f, whose production sources match the
released Phase A fixes. The five-run AC gate passes with medians of 35 ms, 123 ms,
722 ms and 4,105 ms for seven through ten terms. All 20 samples pass their correctness
assertions. The original snapshot matches all 75 catalogue outcomes again.
The following comparison pairs f39defe9b with 267cd523f in the same measurement
window. Grammar Simplify uses the median of seven runs over all 75 drafts, with 73
successful results in both engines. Checked orbit timings use five batches of ten
solves per mode. The earlier AC, profile, grammar and orbit measurements above describe
the selected engine at 7d0419560.
| Reviewed replay | Original (ms) | Reviewed Phase A (ms) |
|---|---|---|
| All catalogue Simplify | 629.66 | 551.18 |
| Warm orbit batch | 52 | 60 |
| Fresh orbit session batch | 137 | 157 |
The native comparison pairs the recorded matched-before archive with the post-fix archive. Both contain 17,081 records, with 16,895 identical decided answers and 186 unknown outcomes. There are zero flips, lost answers, new answers or missing records.
| Native replay | Matched before (ms) | Reviewed Phase A (ms) |
|---|---|---|
| Total elapsed | 51,146.90 | 50,239.57 |
| Median record | 1.77 | 1.94 |
| 95th percentile record | 5.98 | 6.15 |
Total and median timings vary across runs. This native replay has a lower total and higher median and 95th percentile record times. The orbit replay has higher batch times in both modes. These observations describe their measured workloads and windows.
Positional node tables are grouped by operator, arity and owning class. Each row keeps its node creation position, canonical child vector and last changed revision. A table is reused while its class membership and child representatives stay unchanged. Rebuild stamps and canonical child checks refresh tables before a search uses them.
Each plain pattern rule records the revision of its last accepted search. Direct nested searches join new outer rows with every inner row and old outer rows with new inner rows. Larger patterns with fresh variables keep the flat matcher and emit matches that contain a changed row. Patterns whose branches share variables use relation tries with a common variable order chosen from table sizes. Each variable assignment intersects every participating projection, starting with the smallest projection. Later searches partition their joins by the first changed relation.
Matches are applied in the original root and preorder node creation order. The complete match count is retained separately for exponential backoff. A rejected search keeps the rule's previous revision, so its delta remains available when the limit permits it. Callbacks and conditions keep their complete graph observations and existing order. Immutable public node views are cached by numeric node and class positions, and reads retain their eager fuel charges and canonicalize children after a merge.
The paired comparison uses the landed Phase A engine at ac1ced20c and the E17b
engine at 06bd2dbe1. Samples alternate between the engines in one process for each
workload. A second window reverses the starting engine. Each window uses five fresh
graphs per AC size and seven complete grammar catalogue runs per engine. Both engines
produce the same AC shapes and catalogue output hashes in both windows. Each catalogue
run includes all 75 drafts, with 73 successful results and the same two unsupported
results.
| Workload | First Phase A median (ms) | First E17b median (ms) | Reverse Phase A median (ms) | Reverse E17b median (ms) |
|---|---|---|---|---|
| AC with seven terms | 34.53 | 30.62 | 37.84 | 35.97 |
| AC with eight terms | 121.80 | 103.46 | 147.58 | 114.27 |
| AC with nine terms | 619.77 | 574.97 | 729.24 | 673.08 |
| AC with ten terms | 3,553.83 | 3,421.60 | 3,818.71 | 3,332.89 |
| All catalogue Simplify | 632.03 | 602.04 | 569.30 | 572.09 |
AC medians improve in both windows. The grammar median increases in the reverse window. Pooling the 14 grammar timings per engine gives medians of 623.55 ms for Phase A and 576.46 ms for E17b. Of the 14 paired trials, 12 take less time with E17b. The median paired change is a decrease of 32.92 ms.
A separate replay of the unchanged Phase A performance test passes at 06bd2dbe1.
Five fresh graphs per size give medians of 32 ms, 127 ms, 720 ms and 3,535 ms for
seven through ten terms. All 20 samples satisfy the correctness assertions.
The measured medians stay above the stretch targets of 56 ms, 324 ms and 1,800 ms for eight, nine and ten terms. Earlier exploratory comparisons and their load differences are recorded in the E17b decision.