FasterPy: An LLM-based Code Execution Efficiency Optimization Framework
Yue Wu, Minghao Han, Ruiyin Li, Peng Liang, Amjed Tahir, Zengyang Li, Qiong Feng, Mojtaba Shahin
TL;DR
FasterPy presents a low-cost, LLM-based framework to optimize Python code execution by linking Retrieval-Augmented Generation with Low-Rank Adaptation. The approach builds a semantic knowledge base of slow code and performance-enhancing edits, retrieves relevant guidance, and uses a fine-tuned LLM to generate functionally correct, faster code. Through an improved PIE benchmarking workflow and a large, paired optimization dataset derived from PIE and Mercury, FasterPy demonstrates significant gains in code generation accuracy and runtime speed across a range of LLMs, especially for billion-parameter models. The work suggests that retrieval-guided, cost-efficient adaptation can broaden practical automatic code optimization, with implications for expanding to other languages and optimization goals using historical code data.
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.
