Researchers at UC Berkeley are reimagining how we optimize stateful dataflow programs. By leveraging e-graphs and a library of simple, "first-principles" rewrite rules, they have developed an optimizer capable of automatically discovering complex optimizations—such as transforming naive cross-products into efficient incremental joins—without the need for brittle, hand-rolled compiler passes.
The Challenge of Stateful Dataflow
Building high-performance streaming dataflow systems for real-time analytics or distributed applications is notoriously difficult. While developers prefer writing clear, declarative code, the underlying execution must be highly optimized to handle "state"—data that persists and evolves over time.
Traditional compilers typically rely on a fixed sequence of specialized optimization passes. This approach suffers from three major flaws:
- Phase Ordering: The order of passes matters; an early pass might inadvertently "hide" an opportunity for a later, more impactful optimization.
- Complexity: Proving the correctness of a massive, global optimization pass is a significant engineering challenge.
- Brittleness: Compilers often look for specific "special-case" patterns. If the input code deviates slightly from these patterns, the optimization fails.
The researchers focus on Hydroflow, a low-level dataflow language designed as an "LLVM IR for distributed programs." Their goal is to build an optimizer for Hydroflow that is both provably correct and powerful enough to find non-obvious performance gains.
Methodology: E-Graphs and Local Rewrites
The core of the new approach is the e-graph (equivalence graph). Unlike a standard abstract syntax tree (AST), an e-graph represents an entire set of equivalent programs simultaneously.
The optimizer utilizes several key components:
- Local Rewrite Rules: Instead of complex logic, the system uses tiny rules based on mathematical properties like associativity and distributivity. For instance, it defines
persist(storing history) anddelta(retrieving changes) as inverses. - Tree Encoding: Dataflow operators are represented as functions in a tree structure, allowing the e-graph to explore various ways to nest and group operations.
- Zippers: To handle "diamonds"—where data splits and then merges—the optimizer uses a "zipper" data structure. This allows it to "move the cursor" along operator paths to find opportunities for inlining or sharing computation.
- Equality Saturation: The optimizer repeatedly applies rules until it finds the most efficient program version based on a cost model.
Key Insights: Incrementalization from First Principles
One of the most significant breakthroughs is how the system handles incremental computation. The optimizer looks for cycles in the e-graph that pass through a prev node (representing the previous time step). If the e-graph discovers that a computation at time $T$ is equivalent to a computation at $T-1$ plus new data, it has effectively "discovered" an incremental algorithm.
By defining core properties like distributivity (e.g., a cross-product distributed over a chain of inputs), the optimizer can automatically derive "streaming joins" without needing a specific, hard-coded rule for them. This "correctness by construction" ensures that because each local rewrite is small and verifiable, the resulting complex optimizations are also sound.
Results and Future Directions
The prototype successfully transforms naive, inefficient stateful dataflows—which might otherwise recompute everything every "tick"—into optimized versions that only process new changes. The authors demonstrated that their rules for delta, persist, and old operators are general enough to apply to any dataflow, not just specific patterns.
However, a primary hurdle remains: the "Diamond Problem." E-graphs are naturally suited for trees, but dataflow is often a Directed Acyclic Graph (DAG) where results are shared via tee operators. Current tree-based e-graphs struggle to account for the fact that a shared subexpression only incurs a cost once. Additionally, while zippers help manage these structures, they are computationally expensive and can lead to an explosion in e-graph size. Future work will focus on better representing general DAG structures within e-graphs to improve scalability for complex distributed systems.
Summary updated