Chapter 05Down the Rabbit Hole: E-Graphs, and Why Cranelift Won't Saturate
Take (a * 2) / 2 and two reasonable rules. Strength reduction: x * 2 becomes x << 1. Algebra: (x * y) / z becomes x * (y / z).
Run strength reduction first and you get (a << 1) / 2; the algebra rule can't match anymore. Run them the other way and you get a * (2 / 2), then a * 1, then a.
That's phase ordering: each pass destroys information, so order changes the output. LLVM's answer is roughly a hundred passes at -O2 in hand-tuned order. E-graphs are the other answer: apply every rule, keep every version, pick at the end. I thought that was the whole idea. It's the easy part, and the most visible production user deliberately doesn't do it.
The binder#
Picture a librarian with a "same meaning" binder. When two phrases are proven equal, they staple the new one onto the same page. Pages refer to other pages, so stapling two pages together can make other pages equal too.
- E-class: a page, a set of expressions known to be equal.
- E-node: one expression on it, like
*over two children. Its children are e-classes, not specific expressions. - Equality saturation: apply rules until nothing new turns up.
- Extraction: pick the cheapest expression from the page you care about.
Children pointing at classes is the trick. The number of expressions represented is the product of class sizes; the storage is roughly their sum. Underneath: a union-find over class ids, a hash-cons table from canonical e-node to class id, and per class, a list of parent e-nodes.
Watching it run#
The dive has a ~200-line Python e-graph built like egg. I ran it on (/ (* a 2) 2) with the four rules from the egg paper (mul-two, div-assoc, div-same, mul-one). 2 appears twice in the term but once in the graph: sharing.
e0: {a} e1: {2}
e2: {(* e0 e1)}
e3: {(/ e2 e1)} root = e3Four classes. Nothing rewritten yet.
The expensive part is keeping it honest#
When two classes merge, their parents may become identical: if x and y merge, f(x) and f(y) must too, which can cascade. That's congruence, and maintaining it is the real cost.
f(a1, ..., ak) == f(b1, ..., bk) whenever find(ai) == find(bi) for all iThe hash-cons table is keyed by nodes with canonical children, so lookups are one hit, but only if keys are refreshed after every merge. In the trace, when e6 merges with e4, (<< e0 e4) has a stale key and must be re-canonicalized to (<< e0 e6). That's rebuilding.
Before egg this upkeep kept e-graphs in research: in Herbie, the old simplifier took 98.1% of runtime. egg's contribution (POPL 2021) was deciding when to rebuild. Collect all matches, apply all unions, then rebuild once per iteration.
while not saturated and under_limits:
matches = [(rw, subst, c) for rw in rules for (subst, c) in ematch(rw.lhs)] # read
for rw, subst, c in matches:
union(c, add(instantiate(rw.rhs, subst))) # write
rebuild() # onceMerge x into n leaves where x has n parents: repairing per merge touches n parents n times, O(n²). Deferring collapses it to one dirty class, O(n). On egg's test suite: 88x geometric-mean speedup on congruence closure and 21x on whole saturation, versus immediate rebuilding inside egg. The common misquote is 88x versus Herbie's old engine; that number is "over 3000x".
merge #1 ──▶ repair n parents merge #2 ──▶ repair n parents ... merge #n ──▶ repair n parents n repairs x n parents = O(n²)
merge #1, merge #2, ... merge #n (just mark the class dirty) rebuild once ──▶ repair the one dirty class O(n)
A bonus: every match in an iteration reads the same snapshot, so results don't depend on rule order.
The graph that never stops growing#
Saturation usually never comes. Add commutativity, (* a b) -> (* b a), to the same rules and input. Now (* 2 a) plus div-assoc gives (* 2 (/ a 2)), then a/4, a/8, forever:
iter 0: 3 matches -> e-classes, e-nodes = (6, 9)
iter 3: 16 matches -> e-classes, e-nodes = (7, 20)
iter 6: 56 matches -> e-classes, e-nodes = (13, 52)
iter 9: 134 matches -> e-classes, e-nodes = (19, 103)
iter limit hitIt still finds a, because that appeared early. But it never stops on its own. Commutativity plus associativity is the classic blowup: saturate x1 + ... + xn and every non-empty subset becomes a class, 2^n - 1 classes and 3^n - 2^(n+1) + n + 1 nodes. The demo matches this exactly for n from 2 to 6.
| Summed variables | Classes | Nodes |
|---|---|---|
| 10 | 1,023 | 57,012 |
| 20 | about a million | over 3.4 billion |
So real systems stop on limits. egg's defaults: 30 iterations, 10,000 nodes, 5 seconds. Once a limit fires, output depends on rule order again, so phase ordering comes back through the limits. Flatt (UW PLSE): limits make runtime predictable but results not, and new optimizations can trigger limits earlier and block old ones.
The newest head-to-head: Hong, Soi, Zhang and Aiken ran equality saturation and stochastic search with identical rules on matrix-chain multiplication, where parenthesizations are exponential.

Figure 1 from Hong, Soi, Zhang and Aiken, "Rewrite System Showdown: Stochastic Search vs. EqSat" (EGRAPHS 2026), arXiv:2605.19005, licensed CC BY 4.0. Cropped and recompressed.
Stochastic search stays optimal. EqSat is optimal on short chains, then limits bite. Across all five benchmarks, neither dominated.
The scarier failure: one unsound rule
Merges are global and congruence spreads them, so one unsound rule poisons everything. x / x -> 1 puts 0/0 in the class of 1. Any rule that also derives 0/0 = 0 merges 0 and 1, and congruence can collapse the whole graph. Herbie relied on rules like that and had to validate its results afterward.
The compiler that decided not to saturate#
Cranelift, the code generator behind Wasmtime, has used an e-graph mid-end in production since January 2023. It's an acyclic e-graph and does almost none of the above.
Fallin's conclusion: the sea-of-nodes structure works great, but the aegraph itself "may not (yet?) be pulling its weight."
The reason is the rule set. Cranelift's rules are directional: the right side is never worse than the left, so the first good rewrite is almost always final. He counted 2 missed optimizations in about 4 million value nodes. Saturation pays off when you must go uphill before downhill, and directional rules forbid that by construction.
Eager rewriting also brings phase ordering back. Philip Zucker showed it misses exactly the (a * 2) / 2 case from the top.
The open question#
Is the e-graph worth it in a fast compiler? Fallin's data says the multi-version part is noise for Cranelift. The counter from the UW eggcc folks: you only see the benefit with richer, non-directional rules, which need full saturation and its costs. They measure different rule sets, so neither refutes the other yet.
Underneath is a harder one: can you predict whether a rule set will saturate? In general, no. Suciu, Wang and Zhang showed in 2025 that termination on all terms is Π₂-complete, so limits and schedulers are necessary, not just engineering. What's missing is predictable results under limits. Learned schedulers, LLM-written strategies and guided saturation all report wins on their own benchmarks, with no shared suite to compare.
And extraction is its own rabbit hole. Greedy cheapest-expression is optimal if every use pays for its own copy. Once shared subexpressions count once, it's NP-hard and can't be approximated to a constant factor. Probably my next stop.
Sources#
- Willsey et al., egg: Fast and Extensible Equality Saturation, POPL 2021
- Tate, Stepp, Tatlock, Lerner, Equality Saturation, POPL 2009
- Zhang et al., Relational E-Matching, POPL 2022
- Zhang et al., egglog / Better Together, PLDI 2023
- Hong, Soi, Zhang, Aiken, Rewrite System Showdown, EGRAPHS 2026
- Rosin, Ullrich, Hack, Associativity and Commutativity in Equality Saturation, EGRAPHS 2026
- Suciu, Wang, Zhang, Semantic Foundations of Equality Saturation, ICDT 2025
- Chris Fallin, The acyclic e-graph: Cranelift's mid-end optimizer, 2026
- Cranelift e-graph RFC and egraph/mod.rs
- Flatt, e-graph complexity, UW PLSE 2025
- Philip Zucker, smart-constructor aegraph
- egg run.rs, for the default limits
- Herbie