🤖 AI Summary
Existing rule-based or supervised-learning approaches for optimizing performance anti-patterns in Python—such as redundant loops and repeated computations—suffer from high manual effort, poor generalizability, and dependence on large-scale annotated datasets. This paper proposes a low-cost, scalable LLM-driven optimization framework that uniquely integrates Retrieval-Augmented Generation (RAG) with LoRA fine-tuning, leveraging a knowledge base constructed from empirically measured performance data of code variants. It requires no handcrafted rules or labeled data, enabling automatic detection and repair of inefficiencies. The framework synergistically combines static Python program analysis with the PIE benchmark suite to ensure end-to-end optimization that is efficient, interpretable, and highly generalizable. Evaluated on the PIE benchmark, our method significantly outperforms state-of-the-art approaches across key metrics—including repair accuracy, performance speedup, and generation validity. The open-source implementation is publicly available on GitHub.
📝 Abstract
Code often suffers from performance bugs. These bugs necessitate the research and practice of code optimization. Traditional rule-based methods rely on manually designing and maintaining rules for specific performance bugs (e.g., redundant loops, repeated computations), making them labor-intensive and limited in applicability. In recent years, machine learning and deep learning-based methods have emerged as promising alternatives by learning optimization heuristics from annotated code corpora and performance measurements. However, these approaches usually depend on specific program representations and meticulously crafted training datasets, making them costly to develop and difficult to scale. With the booming of Large Language Models (LLMs), their remarkable capabilities in code generation have opened new avenues for automated code optimization. In this work, we proposed FasterPy, a low-cost and efficient framework that adapts LLMs to optimize the execution efficiency of Python code. FasterPy combines Retrieval-Augmented Generation (RAG), supported by a knowledge base constructed from existing performance-improving code pairs and corresponding performance measurements, with Low-Rank Adaptation (LoRA) to enhance code optimization performance. Our experimental results on the Performance Improving Code Edits (PIE) benchmark demonstrate that our method outperforms existing models on multiple metrics. The FasterPy tool and the experimental results are available at https://github.com/WuYue22/fasterpy.