Optimizing Code Runtime Performance through Context-Aware Retrieval-Augmented Generation
Manish Acharya, Yifan Zhang, Kevin Leach, Yu Huang
TL;DR
Automating code runtime optimization with large language models remains challenging due to insufficient deep program analysis. AutoPatch combines human-inspired insights, CFG-diff analysis, and retrieval-augmented prompting to produce context-aware patches, formalizing CFG differences as $\Delta G = G_p - G_o$ with components $\Delta S$, $\Delta F$, and $\Delta C$. In experiments on IBM Project CodeNet (C++), AutoPatch achieves a 7.3% runtime improvement over GPT-4o and demonstrates lexical improvements across multiple metrics, validating a unified pipeline that bridges human expertise and automated optimization. This framework lays the groundwork for scalable, interpretable, and safer automated program optimization across diverse software engineering tasks.
Abstract
Optimizing software performance through automated code refinement offers a promising avenue for enhancing execution speed and efficiency. Despite recent advancements in LLMs, a significant gap remains in their ability to perform in-depth program analysis. This study introduces AUTOPATCH, an in-context learning approach designed to bridge this gap by enabling LLMs to automatically generate optimized code. Inspired by how programmers learn and apply knowledge to optimize software, AUTOPATCH incorporates three key components: (1) an analogy-driven framework to align LLM optimization with human cognitive processes, (2) a unified approach that integrates historical code examples and CFG analysis for context-aware learning, and (3) an automated pipeline for generating optimized code through in-context prompting. Experimental results demonstrate that AUTOPATCH achieves a 7.3% improvement in execution efficiency over GPT-4o across common generated executable code, highlighting its potential to advance automated program runtime optimization.
