TL;DR
The paper introduces Optimo, a novel framework that automates source-code optimization for dynamic languages like Python using Large Language Models (LLMs). By utilizing a "Mixture-of-Prompts" (MoP) architecture and a multi-level optimization strategy (spanning algorithms down to API calls), Optimo achieves up to a 3.97x speedup on human-written code and a massive 13.51x speedup on LLM-generated code.
What Problem Does This Paper Address?
While static languages (like C or C++) benefit from highly mature compiler-level optimizations that operate on intermediate representations (IRs), dynamic languages like Python often miss out on these automated performance boosts. As a result, Python programs can suffer from severe runtime inefficiencies.
Recently, researchers have turned to LLMs to optimize source code directly. However, existing LLM-based approaches face two major hurdles:
- Poor Target Identification: They often mistake fixed-overhead operations (like I/O or printing) for optimization bottlenecks simply because they take a long time, missing the scalable code structures that actually cause performance issues.
- Single-Level Focus: Most tools only optimize code at a single line or statement level, completely missing coarse-grained algorithmic improvements (e.g., replacing a recursive function with an iterative one).
Methods
Optimo tackles these challenges through a structured, four-level optimization pipeline powered by a Mixture-of-Prompts (MoP) architecture.
The framework operates sequentially across four layers of abstraction derived from the program's Abstract Syntax Tree (AST):
- Algorithm Level: Coarse-grained complexity improvements.
- Statement Level: Optimizing loops and conditional blocks.
- Expression Level: Refining mathematical or logical expressions (e.g., list comprehensions).
- API Level: Replacing slow built-in functions with faster alternatives.
Key Technical Components:
- Differential Profiling: To find true bottlenecks, Optimo runs the program on both small and large test inputs. It identifies "time-critical code structures"—lines where execution time scales poorly with input size—ensuring it targets code that will actually benefit from optimization.
- Mixture-of-Prompts (MoP): Inspired by Mixture-of-Experts (MoE) neural networks, MoP replaces specialized sub-models with specialized prompts. Optimo routes identified bottlenecks to specific optimization prompts mined from real-world developer solutions, falling back to a general "shared strategy" prompt if no specialized match is found.
- Correctness Reflection & Validation: Code optimization is notorious for breaking functionality. Optimo automatically tests candidate programs against small test cases. If they fail, it feeds the error messages back into the LLM for self-repair. Finally, it validates performance gains on large stress-test inputs before committing to the changes.
Datasets
The authors utilized three primary datasets for mining strategies and evaluating performance:
- Python Codeforces Submission Dataset: A dataset of competitive programming solutions used to mine slow-fast code pairs. After rigorous cleaning, 13,319 valid solutions across 2,005 problems were used to extract 191 algorithmic, 851 statement-level, and 805 expression-level special optimization prompts.
- COFFE: A benchmark containing 398 function-level and 294 file-level problems used for evaluation.
- Effibench: A benchmark consisting of 707 file-level LeetCode problems used to test optimization efficiency.
Key Insights and Ideas
- Scaling Over Absolute Time: Evaluating code performance based on how it handles scaling (differential profiling) completely eliminates the "I/O trap" where models waste budget trying to optimize un-optimizable system calls.
- Prompts as Cheap Experts: Training an MoE model for code optimization requires massive amounts of high-quality parallel data. Replacing expert models with structured, mined prompts achieves targeted expertise without the steep training overhead.
- Top-Down Synergy Matters: Optimizing from the top down (Algorithm $\rightarrow$ Statement $\rightarrow$ Expression $\rightarrow$ API) ensures that high-level architectural changes are made first, preventing fine-grained optimizations from being instantly overwritten or invalidated by a later algorithmic shift.
Key Results
Optimo was evaluated against six state-of-the-art LLM optimization baselines (including EffiLearner, PIE, and RAPGEN) using GPT-4o as the base model.
- Human-Written Code: Optimo achieved an
opt%(the percentage of programs optimized correctly and accelerated by $\ge 10\%$) of up to 57.48%, outperforming the best baseline by up to 96.51%. It achieved an overall benchmark speedup of up to 3.97x. - LLM-Generated Code: When optimizing code initially written by GPT-4o, Optimo achieved an overall speedup of up to 13.51x.
- Beating Humans: Remarkably, 26.82% to 54.60% of the LLM-generated programs optimized by Optimo ended up being more efficient than the human-written ground truth solutions in the benchmarks.
Limitations and Future Work
- Inference Cost: Optimo's multi-level approach and iterative validation loops mean it consumes significantly more tokens and API calls than single-turn baseline prompts. It represents a clear trade-off between computational cost and code performance.
- Language Constraints: The current implementation is evaluated exclusively on Python. While the underlying concepts of AST-based multi-level optimization and MoP are language-agnostic, extending it to other dynamic languages (like JavaScript or Ruby) requires mining new language-specific prompt libraries.
- Test Case Dependency: The correctness reflection mechanism relies entirely on the quality of the generated test cases. If the test suite misses an edge case, Optimo could potentially introduce silent functional bugs.
Summary updated