RAPGen: An Approach for Fixing Code Inefficiencies in Zero-Shot

Spandan Garg, Roshanak Zilouchian Moghaddam, Neel Sundaresan

Abstract

Performance bugs are critical yet elusive software defects that degrade system efficiency without causing immediate failures. Traditional automated repair techniques often struggle with the complexity and variety of these bugs, requiring either rigid rule-based systems or expensive fine-tuning of large models. This paper introduces RAPGen, a retrieval-augmented zero-shot learning approach that fixes performance inefficiencies by combining a knowledge base of historical fixes with the reasoning capabilities of Large Language Models (LLMs). By retrieving natural language instructions rather than raw code examples, RAPGen achieves superior performance over fine-tuned state-of-the-art models.

Introduction

Performance bugs, often termed "silent killers," are notoriously difficult to detect and fix. Unlike functional bugs, they manifest as increased latency or memory consumption, often only under specific workloads. Fixing these issues typically requires expert knowledge of API optimizations—for instance, replacing a slow LINQ query with a more efficient collection type.

Existing automated program repair (APR) tools generally fall into two categories: rule-based systems, which are difficult to scale, and deep-learning models, which require massive datasets for task-specific fine-tuning. RAPGen offers a flexible alternative by leveraging Retrieval-Augmented Prompt Generation to guide a general-purpose LLM (GPT-3.5-turbo) using historical fix patterns.

Methodology: The RAPGen Pipeline

The core innovation of RAPGen lies in its ability to "look up" relevant optimization advice rather than being explicitly trained on every possible bug type. The process consists of four primary stages:

  1. Knowledge Base (KB) Construction: The authors curated approximately 1,500 performance-related commits from high-quality C# repositories on GitHub. These fixes were abstracted using Abstract Syntax Tree (AST) transformations to remove project-specific details while preserving the underlying API logic.
  2. Instruction Extraction: Instead of using raw code diffs, the system automatically generates natural language "fix instructions" for each entry in the KB (e.g., "Use a HashSet instead of a List to improve lookup performance").
  3. Prompt Generation: When presented with a buggy method and an identified "expensive line" (typically located via a profiler), RAPGen searches the KB for the most similar historical bug and retrieves the corresponding instruction.
  4. Zero-Shot Execution: The LLM receives a prompt containing the buggy code and the retrieved instruction, generating the fix in a zero-shot manner.
The RAPGen pipeline: from identifying an expensive line to generating a fix using retrieved instructions.

Evaluation and Results

The system was evaluated against the DeepDev-PERF benchmark, which contains 132 expert-verified performance fixes from real-world C# projects.

Performance Metrics

RAPGen demonstrated significant improvements over existing baselines:

  • Verbatim Accuracy: RAPGen matched the original developer's fix exactly in 42.4% of cases, outperforming the fine-tuned DeepDev-PERF model (34.3%).
  • Human Evaluation: In a blind study, performance experts rated RAPGen’s suggestions as equivalent to or better than the original developer's fix in approximately 60% of instances.
  • Real-World Impact: In a trial at Microsoft involving 10 production cloud services, 70% of RAPGen's suggested fixes were accepted and merged by production developers.
Comparison of RAPGen's accuracy against static, retrieval-based, and reasoning-based prompting strategies.

Discussion and Key Insights

The success of RAPGen highlights several critical shifts in automated software engineering:

  • Instructions vs. Examples: The study found that providing explicit natural language instructions is more effective than providing "one-shot" code examples. LLMs appear more capable of following logical directives than inferring complex refactoring patterns from raw code.
  • The Power of Retrieval: Grounding a general-purpose model with retrieved domain knowledge can be more effective and significantly cheaper than custom fine-tuning.
  • Localization Synergy: By assuming the "expensive line" is known, RAPGen focuses the LLM's reasoning on the specific area of inefficiency, allowing it to handle the boilerplate of refactoring (like variable scoping) with high reliability.

Limitations

Despite its success, RAPGen is currently limited to single-method fixes and depends on external profilers for bug localization. Future work aims to expand the system to handle multi-file architectural changes and support a broader range of programming languages beyond C#.

Conclusion

RAPGen demonstrates that retrieval-augmented zero-shot learning is a highly effective paradigm for fixing performance bugs. By bridging the gap between historical developer knowledge and the generative power of LLMs, it provides a scalable, low-cost solution for improving software efficiency in production environments.

Summary updated

BibTeX
@inproceedings{garg2023rapgen,
  author = {Spandan Garg and Roshanak Zilouchian Moghaddam and Neel Sundaresan},
  title = {{RAPGen: An Approach for Fixing Code Inefficiencies in Zero-Shot}},
  booktitle = {SEIP@ICSE},
  year = {2023},
}
Figure