TL;DR
RAPGen (Retrieval-Augmented Prompt Generation) is a novel approach that fixes software performance bugs in a zero-shot manner using Large Language Models (LLMs). By retrieving specific natural language fix instructions from a pre-constructed knowledge base of past code changes, RAPGen guides LLMs to generate accurate performance patches without requiring expensive model fine-tuning.
What Problem Does This Paper Address?
Unlike functional bugs that cause software to crash, performance bugs are silent resource hogs. They lead to high latency, bloated cloud computing costs, and degraded throughput, often remaining undetected until code hits production under heavy loads.
Fixing these bugs typically requires domain expertise. Traditional automated program repair (APR) tools rely on rigid, rule-based analyzers that are expensive to maintain. Recent deep learning approaches, like DeepDev-PERF, require intensive and costly fine-tuning on massive datasets. The authors of this paper address this dilemma: Can we build a flexible, low-maintenance performance-fixing tool that leverages the power of LLMs without the overhead of fine-tuning?
Methods
RAPGen solves this using a zero-shot, retrieval-augmented prompting pipeline. The process is split into two main phases:
- Knowledge-Base (KB) Construction: The authors crawled GitHub for C# commits explicitly marked as performance fixes (e.g., titles starting with
PERF:). They extracted the before-and-after method pairs and used Abstract Syntax Tree (AST) parsing to strip away project-specific identifiers (like variable and custom function names). This left clean, reusable templates of buggy API patterns. For each pattern, they automatically generated an English instruction explaining the fix (e.g., "PERF: Rewrite the above method without FirstOrDefault."). - Inference Pipeline: When a developer encounters a performance bottleneck (localized using standard profiling tools), RAPGen abstracts the expensive line of code and searches the KB for a matching pattern. It uses a code-search technique (Aroma) to find the most structurally similar historical method and retrieves its corresponding natural language instruction.
Finally, RAPGen constructs a prompt containing the original buggy method wrapped in comments, the retrieved instruction, and the method signature to coax the LLM (gpt-3.5-turbo) into completing the optimized version of the method.
Datasets
The approach was evaluated using two primary datasets:
- DeepDev-PERF Dataset: A benchmark consisting of 132 manually reviewed, expert-verified performance fixes from C# repositories on GitHub. This dataset covers a wide variety of real-world performance bugs, primarily involving inefficient usage of .NET APIs and LINQ queries.
- In-the-Wild Production Dataset: To test real-world viability, the authors evaluated RAPGen on 50 internal production .NET cloud services at a large software company, using live CPU profiler traces to identify bottlenecks.
Key Insights and Ideas
- Instructions Beat Raw Examples: Intuitively, one might think providing a raw before-and-after code example (one-shot learning) would help an LLM. However, RAPGen found that distilling that example into an explicit natural language instruction performs significantly better. It spares the LLM from having to implicitly compute code diffs and infer the underlying transformation rule.
- The Danger of "Self-Reasoning" for API Fixes: Recent trends favor letting LLMs "reason" through problems step-by-step (Chain-of-Thought). However, when applied to performance bugs, reasoning-based prompts often caused the model to over-engineer solutions—suggesting complex algorithmic overhauls or caching mechanisms rather than simple, clean API swaps (like replacing an expensive LINQ query with a standard
foreachloop). - Zero-Shot Outperforms Fine-Tuning: By smartly engineering the prompt context with retrieved instructions, a generic LLM can outperform a specialized model that underwent intensive, task-specific fine-tuning.
Key Results
RAPGen was evaluated against several prompt variants and the state-of-the-art fine-tuned model, DeepDev-PERF:
- Verbatim Accuracy: RAPGen achieved a 42.4% verbatim match with the original developer fixes on the benchmark dataset, outperforming DeepDev-PERF (34.3%) despite DeepDev-PERF using 2,000 generation attempts per bug compared to RAPGen’s 100.
- Human Expert Evaluation: In 60.6% of cases, RAPGen generated a fix that performance experts judged to be equivalent to or better than the human developer's original fix.
- In-the-Wild Success: When deployed against live production bottlenecks, RAPGen generated fixes for 10 different codebases. Engineers accepted and merged 7 out of the 10 fixes directly into production services.
Limitations and Future Work
While highly effective, RAPGen has a few notable limitations:
- Scope of Fixes: It currently targets performance issues stemming from the misuse of common .NET APIs and is restricted to single-method/single-file fixes. It cannot yet resolve complex bugs that span multiple classes or folders.
- Localization Dependency: RAPGen assumes that the exact expensive line of code has already been identified by an external profiler.
- Language Constraints: The evaluation was strictly performed on C# codebases, though the authors note the methodology should theoretically translate well to other languages like Java or Python.
Summary updated