Abstract
FasterPy is a novel framework designed to automate the optimization of Python code execution efficiency. By leveraging Large Language Models (LLMs) through a combination of Retrieval-Augmented Generation (RAG) and Low-Rank Adaptation (LoRA) fine-tuning, FasterPy identifies performance bottlenecks such as redundant computations and inefficient data structures. Experimental results demonstrate that FasterPy significantly enhances both functional correctness and execution speed, with some models achieving performance close to human-optimized references.
Introduction
Performance bugs, including suboptimal algorithmic complexity and inefficient resource usage, are common in software development but difficult to detect and fix manually. Traditional optimization techniques often rely on rigid, rule-based patterns or specialized program representations that lack generalizability. While LLMs have advanced code generation, they frequently fail to prioritize execution efficiency without explicit guidance. FasterPy addresses this by providing a scalable, low-cost framework that "teaches" LLMs to perceive and implement efficiency improvements.
The FasterPy Framework
The FasterPy pipeline consists of four distinct stages designed to transform inefficient code into an optimized version while preserving functional integrity.
- Code Preprocessing: Utilizing the Tree-sitter library, the framework extracts target functions and relevant context (e.g., global variables, class definitions) while removing extraneous code to minimize prompt noise.
- Code Embedding: The preprocessed code is converted into semantic vectors using UniXcoder, which captures both structural information (AST) and natural language semantics.
- Knowledge Base Retrieval (RAG): FasterPy queries a Milvus vector database containing over 60,000 historical optimization pairs. The system retrieves natural language optimization suggestions rather than raw code examples to prevent variable-name "contamination" and hallucinations in smaller models.
- Code Generation (LoRA): The final optimization is performed by an LLM fine-tuned via Low-Rank Adaptation (LoRA). This approach allows the model to specialize in optimization tasks efficiently.
Datasets and Benchmarking
The framework was trained and evaluated using the PIE (Performance Improving Code Edits) dataset, containing ~35,000 code pairs from IBM CodeNet, and the Mercury benchmark, which includes 1,889 Python tasks. To ensure accurate performance measurement, the authors developed an Improved PIE Benchmarking Program that uses a persistent control process and exec() calls, reducing the timing noise associated with standard subprocess-based testing.
Key Findings and Results
Evaluation across various models, including Deepseek-Coder-6.7B and Qwen2.5-Coder-7B, revealed substantial gains:
- Functional Correctness: The Deepseek-Coder-6.7B-FasterPy model showed a 37% improvement in Pass@1 scores over its base version.
- Execution Speed: Models consistently achieved speedup factors exceeding 1.3x on the PIE benchmark. Notably, a FasterPy-enhanced Codex model reached a speedup of 4.60, nearly matching the human reference of 4.76.
- Suggestion Efficacy: For models under 10B parameters, natural language suggestions proved more effective than code-based examples, as they allowed the models to focus on logic rather than specific variable naming conventions.
Conclusion
FasterPy demonstrates that RAG-based context and parameter-efficient fine-tuning can unlock latent optimization capabilities in LLMs. Future work will focus on extending the framework to compiled languages and expanding the scope from function-level to repository-level refactoring, while exploring hardware-level counters for more granular performance data.
Summary updated