[Paper Review] RAPGen: An Approach for Fixing Code Inefficiencies in Zero-Shot
RAPGen is a zero-shot prompt engineering approach that retrieves performance bug fix instructions from a knowledge base of past fixes and uses them to generate effective prompts for large language models (e.g., Codex) to suggest code optimizations. It achieves developer-level fix quality in ~60% of cases and generates verbatim fixes in ~39% of cases, significantly reducing the need for fine-tuning or rule-based systems.
Performance bugs are non-functional bugs that can even manifest in well-tested commercial products. Fixing these performance bugs is an important yet challenging problem. In this work, we address this challenge and present a new approach called Retrieval-Augmented Prompt Generation (RAPGen). Given a code snippet with a performance issue, RAPGen first retrieves a prompt instruction from a pre-constructed knowledge-base of previous performance bug fixes and then generates a prompt using the retrieved instruction. It then uses this prompt on a Large Language Model (such as Codex) in zero-shot to generate a fix. We compare our approach with the various prompt variations and state of the art methods in the task of performance bug fixing. Our evaluation shows that RAPGen can generate performance improvement suggestions equivalent or better than a developer in ~60% of the cases, getting ~42% of them verbatim, in an expert-verified dataset of past performance changes made by C# developers.
Motivation & Objective
- To address the challenge of fixing performance bugs—non-functional issues that are hard to detect and require expert knowledge—without relying on expensive fine-tuning or rule-based systems.
- To reduce the maintenance cost and increase the generalizability of performance bug fixing tools by leveraging prompt engineering instead of training dedicated models.
- To enable zero-shot, scalable, and extensible performance fix generation using pre-trained LLMs by constructing a knowledge base of past fix patterns.
- To improve fix quality and reduce reliance on manual prompt crafting by retrieving and reusing effective instruction templates from historical performance fixes.
Proposed method
- RAPGen constructs a knowledge base of past performance bug fixes, extracting buggy API usages and corresponding fix instructions in natural language.
- Given a buggy code snippet and the specific line with inefficiency, RAPGen retrieves the most relevant fix instruction from the knowledge base using semantic similarity.
- It constructs a prompt by combining the buggy method, the retrieved instruction as a comment, and the method signature to guide the LLM toward generating a correct fix.
- The prompt is then fed into a pre-trained LLM like Codex in a zero-shot setting to generate a performance-optimized version of the code.
- The approach avoids fine-tuning and instead relies on retrieval-augmented prompt generation to adapt to new performance issues dynamically.
- The method is evaluated on a developer-verified dataset of C# performance fixes, comparing RAPGen’s output to human-authored fixes.
Experimental results
Research questions
- RQ1Can retrieval-augmented prompt generation improve the accuracy and relevance of LLM-generated performance bug fixes in a zero-shot setting?
- RQ2How does RAPGen’s performance compare to state-of-the-art models like DeepDev-PERF in generating correct and efficient code fixes?
- RQ3To what extent can a knowledge base of past fix instructions improve prompt quality and fix effectiveness without fine-tuning?
- RQ4Does incorporating the specific buggy line in the prompt lead to better fix suggestions than methods that only provide the full method?
Key findings
- RAPGen generates fixes that are equivalent or better than developer fixes in approximately 60% of cases on an expert-verified dataset of C# performance changes.
- It produces verbatim matches to developer fixes in roughly 39% of cases, demonstrating strong alignment with human intent.
- The approach outperforms DeepDev-PERF and other baselines in generating correct single-method performance fixes, especially in zero-shot scenarios.
- Retrieving and injecting specific fix instructions into prompts significantly improves fix quality compared to generic or manually crafted prompts.
- RAPGen’s zero-shot design eliminates the need for model fine-tuning and reduces maintenance overhead, making it scalable across languages and bug types.
- The method demonstrates that effective prompt engineering can rival or surpass end-to-end fine-tuned models for performance bug fixing, particularly when guided by a curated knowledge base of past fixes.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.