Abstract
Optimizing Pandas programs is a challenging problem. Existing systems and compiler-based approaches offer reliability but are either heavyweight or support only a limited set of optimizations. Conversely, using LLMs in a per-program optimization methodology can synthesize nontrivial optimizations, but is unreliable, expensive, and offers a low yield. In this work, we introduce a hybrid approach that works in a 3-stage manner that decouples discovery from deployment and connects them via a novel bridge. First, it discovers per-program optimizations (discovery). Second, they are converted into generalised rewrite rules (bridge). Finally, these rules are incorporated into a compiler that can automatically apply them wherever applicable, eliminating repeated reliance on LLMs (deployment). We demonstrate that RuleFlow is the new state-of-the-art (SOTA) Pandas optimization framework on PandasBench, a challenging Pandas benchmark consisting of Python notebooks. Across these notebooks, we achieve a speedup of up to 4.3x over Dias, the previous compiler-based SOTA, and 1914.9x over Modin, the previous systems-based SOTA.
Introduction
Pandas is the cornerstone of modern data science in Python, yet it is notoriously inefficient in terms of speed and memory. Traditional optimization efforts have followed two main paths: heavyweight systems like Modin or Dask that parallelize the backend, and compiler-based rewriters like Dias that utilize manually engineered rules. While systems-based approaches often introduce significant overhead for typical exploratory data analysis (EDA) workloads, compiler-based approaches are limited by the human effort required to manually define optimization patterns.
Large Language Models (LLMs) offer a promising alternative due to their ability to generate creative optimizations. However, using LLMs directly at runtime is impractical; they are expensive, slow, and often unreliable—our research indicates LLMs successfully optimize Pandas code only about 5.7% of the time. RuleFlow addresses these challenges by using LLMs to discover optimizations offline and then distilling those successes into permanent, reusable compiler rules.
Methodology: The Three-Stage Pipeline
RuleFlow's architecture is built on the principle of decoupling the expensive discovery of optimizations from their efficient deployment.
1. Discovery (SnippetGen)
In the offline discovery phase, an LLM agent analyzes real-world Pandas snippets to propose faster alternatives. To ensure reliability, these rewrites undergo rigorous testing for semantic equivalence and performance gains. An adversarial "FeedbackGen" agent is employed to find edge cases that might break the proposed optimization, ensuring only robust rewrites proceed.
2. Bridge (RuleGen)
The core innovation of RuleFlow is the "Bridge" that converts specific code optimizations into generalized rewrite rules. A multi-agent system consisting of four specialized agents—Variable & Constant Generalizer, AST Type Resolver, Rule Constructor, and Precondition Synthesizer—works in concert. This modular approach significantly improves the validity of generated rules compared to a single-agent approach, increasing the success rate from 18% to 68%.
3. Deployment (CodeGen)
Once generalized, these rules are integrated into a lightweight compiler. At runtime, the compiler uses static pattern matching to apply optimizations to new, unseen code. This stage requires no LLM interaction, ensuring that the optimization process is fast, deterministic, and free of runtime costs.
Evaluation and Performance
We evaluated RuleFlow using PandasBench, a benchmark of 102 real-world Python notebooks. The discovery phase utilized 199 notebooks from Kaggle to generate a library of 88 valid, reusable rewrite rules.
RuleFlow achieved a mean speedup of 1.54x over the previous compiler-based state-of-the-art (Dias) and 112.79x over the systems-based state-of-the-art (Modin). In extreme cases, speedups reached 4.3x over Dias and over 1900x over Modin. Notably, RuleFlow demonstrated high applicability, with 88 out of 102 test notebooks triggering at least one optimization rule.
Discussion and Future Directions
A key insight of this work is the "Low Yield, High Reuse" phenomenon. While LLMs are inconsistent at optimizing code, the few successful optimizations they discover are often broadly applicable. By converting these rare successes into compiler rules, RuleFlow amortizes the high cost of LLM discovery.
Current limitations include semantic divergence in edge cases (e.g., handling of NaN values) and the overhead of runtime preconditions. Future work will focus on "cost-aware" precondition synthesis to ensure safety checks do not outweigh optimization gains, as well as more sophisticated rule-scheduling algorithms to maximize performance across diverse workloads.
Conclusion
RuleFlow demonstrates a powerful hybrid paradigm for program optimization. By bridging the creative potential of LLMs with the reliability of traditional compilers, it provides a scalable and effective solution for the long-standing performance challenges of the Pandas ecosystem.
Summary updated