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.
翻译:代码常常存在性能缺陷。这些缺陷促使了代码优化的研究与实践。传统的基于规则的方法依赖人工设计和维护针对特定性能缺陷(如冗余循环、重复计算)的规则,导致其劳动密集且适用性有限。近年来,基于机器学习和深度学习的方法通过从带注释的代码语料库和性能测量中学习优化启发式,成为有前景的替代方案。然而,这些方法通常依赖于特定的程序表示和精心设计的训练数据集,使得开发成本高昂且难以扩展。随着大型语言模型(LLM)的蓬勃发展,它们在代码生成方面的卓越能力为自动化代码优化开辟了新途径。在本工作中,我们提出了FasterPy,一个低成本且高效的框架,该框架适配LLM以优化Python代码的执行效率。FasterPy结合了检索增强生成(RAG)(由从现有性能改进代码对及相应性能测量构建的知识库支撑)与低秩适配(LoRA),以提升代码优化性能。我们在性能改进代码编辑(PIE)基准上的实验结果表明,我们的方法在多个指标上优于现有模型。FasterPy工具及实验结果可从https://github.com/WuYue22/fasterpy获取。