An exploration of how custom data structures and higher-order functions can enhance e-graph performance by representing algebraic identities more efficiently without system redesign.
Custom Data Structures in E-Graphs: A Path to Efficient Program Equivalence
E-graphs have emerged as powerful tools for reasoning about program equivalence, forming the backbone of modern optimizers and compilers. However, their utility is tempered by a fundamental challenge: when algebraic identities such as associativity, commutativity, and distributivity (A/C/D) are incorporated, these data structures can experience catastrophic size explosions, rendering them impractical for many real-world applications. Saul Shanabrook's recent exploration presents an elegant solution to this dilemma: enhancing e-graph systems with custom data structures and higher-order functions that preserve the essential mathematical properties while dramatically improving performance.
The E-Graph Dilemma
At its core, an e-graph represents equivalent expressions through e-classes, clusters of expressions that have been proven equivalent through rewrite rules. This approach allows for a more flexible exploration of program equivalence than traditional term rewriting systems, as it defers the selection of the "best" representation until extraction time. However, this flexibility comes at a cost.
When A/C/D rules are applied, even simple expressions can generate an exponential number of equivalent representations. For instance, the expression 2 + a + b + b + 3, when combined with associativity and commutativity rules, explodes into a complex web of equivalent expressions, each represented as distinct nodes in the e-graph. This phenomenon, aptly termed "size blow-up," severely limits the scalability of e-graph-based approaches to optimization problems involving complex algebraic structures.
Custom Containers as Semantic Preservers
Shanabrook's approach addresses this limitation by introducing custom data structures that inherently preserve algebraic properties, thereby reducing the need for explicit A/C/D rules. The key insight is that rather than representing operations as binary trees (which naturally generate multiple equivalent expressions under A/C/D), we can represent them using structures that encode these properties intrinsically.
For example, addition can be represented not as a binary operation a + b, but as a multiset (or bag) of terms being added. This representation inherently captures both associativity (the order of operations doesn't matter) and commutativity (the order of terms doesn't matter), eliminating the need for explicit rules that would otherwise generate equivalent expressions.
The implementation of these containers in Egglog requires careful attention to congruence preservation—the property that if a == b, then f(a) == f(b). This is achieved through a "rebuilding" operation that updates references to e-classes whenever unions occur, ensuring that the container's internal structure remains consistent with the e-graph's equivalence relationships.
Higher-Order Functions: Beyond Matching Limitations
A significant challenge in working with these custom containers is the limited ability to match on their internal structure. Since Egglog treats containers as opaque values, traditional pattern matching cannot access their contents. Shanabrook presents two innovative approaches to overcome this limitation:
Index Functions: Creating auxiliary functions that provide access to container contents, similar to database indexes. For example, a
ms_num_indexfunction can map a multiset and an item to its count within that multiset. This approach enables rules that can inspect and transform container contents, though it still adds overhead to the e-graph.Higher-Order Functions: Leveraging block-wise operations that can process entire containers at once, eliminating the need for intermediate representations. For constant folding in a multiset-based addition, for instance, we can define rules that extract all constants, sum them, and replace them with their sum—all in a single transformation step.
This higher-order approach represents a significant advancement, as it allows for more direct expression of complex transformations while maintaining the efficiency benefits of the container representation. The author demonstrates this with a constant folding rule that processes all constants in a multiset simultaneously, rather than pairwise as would be required with index-based approaches.
Real-World Impact: The Cloth Simulation Case Study
The most compelling evidence for this approach comes from a detailed case study involving polynomial expressions from a cloth simulation workload. Starting with expressions representing bending functions and their gradients, Shanabrook demonstrates how the traditional approach—applying A/C/D rules until saturation—quickly becomes infeasible for larger expressions.
The gradient expression, initially containing 20,570 operations, quickly expands to over 2.5 million nodes in the e-graph after just a few iterations of A/C/D rules, with runtime growing prohibitively. In contrast, the multiset-based approach:
Represents polynomials as nested multisets (multisets of multisets), where each inner multiset represents a monomial and the outer multiset represents the sum of these monomials.
Implements a greedy Horner-like factorization algorithm that operates directly on this representation, finding common factors across monomials without exploring the entire space of equivalent expressions.
The results are striking: the gradient expression is factored to a cost of 79,974 operations (compared to over 2 million in the traditional approach), with an e-graph size of just 112,144 nodes (compared to over 2.5 million). This represents not just a quantitative improvement but a qualitative shift in what becomes computationally feasible.
Implications and Future Directions
This work has profound implications for the field of program optimization and compiler construction. By demonstrating that efficient algebraic representations can be added to existing e-graph systems rather than requiring entirely new implementations, Shanabrook opens the door to a more modular and extensible ecosystem of optimization tools.
The approach also highlights an important philosophical point: sometimes, the most effective optimization is not finding the "best" representation among many, but choosing a representation that inherently encodes the desired properties. This aligns with a broader trend in programming language design where choosing appropriate data structures can dramatically simplify algorithms and improve performance.
However, the work also reveals current limitations in systems like Egglog. The lack of built-in generic type support makes implementing higher-order functional primitives on containers challenging, while the restricted function composition capabilities necessitate the creation of numerous bespoke functions. These limitations suggest promising avenues for future research:
- Enhanced support for generic types in both primitives and user code
- More flexible function composition mechanisms, possibly through DSLs or higher-order composition approaches
- Further exploration of domain-specific container representations tailored to particular optimization challenges
Counter-Perspectives and Trade-offs
While the approach presented is compelling, it's worth considering potential counter-perspectives. One might argue that the added complexity of custom containers and higher-order functions could outweigh the performance benefits for simpler problems. For expressions without significant algebraic structure, traditional e-graph approaches might remain preferable due to their simplicity.
Additionally, the representational power comes at the cost of expressiveness. By encoding algebraic properties into the data structure itself, we lose some of the flexibility that makes e-graphs powerful—the ability to explore different equivalence relationships independently. This could limit the applicability of the approach in domains where these properties are not fixed or are only partially applicable.
Furthermore, the implementation of these advanced features requires significant expertise, potentially raising the barrier to entry for researchers and practitioners who wish to experiment with e-graph-based optimization. The author notes that implementing higher-order functions on containers is currently "fiddly and requires careful thought," suggesting that tooling improvements would be necessary to make these techniques more accessible.
Conclusion
Shanabrook's exploration of custom data structures in e-graphs represents a significant advancement in our ability to reason about program equivalence efficiently. By demonstrating how algebraic properties can be encoded directly into data structures rather than through explicit rewrite rules, this work opens new possibilities for scaling e-graph-based optimization to problems that were previously intractable.
The case study involving polynomial factorization provides compelling evidence of the practical value of this approach, showing orders-of-magnitude improvements in both performance and e-graph size. While challenges remain in terms of implementation complexity and system limitations, the direction is clear: enhancing e-graph systems with richer data structures and higher-order operations represents a promising path toward more powerful and scalable program optimization.
As computational demands continue to grow and the complexity of the programs we optimize increases, techniques that can tame the combinatorial explosion of equivalent expressions will become increasingly valuable. Shanabrook's work not only provides such a technique but also demonstrates how it can be integrated into existing systems, preserving the benefits of the e-graph ecosystem while dramatically expanding its reach.
For researchers and practitioners in program optimization, compiler construction, and programming language design, this exploration offers both practical techniques and inspiration for further innovation in the representation and manipulation of program equivalence.

Comments
Please log in or register to join the discussion