Code runtime optimization-the task of rewriting a given code to a faster one-remains challenging, as it requires reasoning about performance trade-offs involving algorithmic and structural choices. Recent approaches employ code-LLMs with slow-fast code pairs provided as optimization guidance, but such pair-based methods obscure the causal factors of performance gains and often lead to superficial pattern imitation rather than genuine performance reasoning. We introduce ECO, a performance-aware prompting framework for code optimization. ECO first distills runtime optimization instructions (ROIs) from reference slow-fast code pairs; Each ROI describes root causes of inefficiency and the rationales that drive performance improvements. For a given input code, ECO in parallel employs (i) a symbolic advisor to produce a bottleneck diagnosis tailored to the code, and (ii) an ROI retriever to return related ROIs. These two outputs are then composed into a performance-aware prompt, providing actionable guidance for code-LLMs. ECO's prompts are model-agnostic, require no fine-tuning, and can be easily prepended to any code-LLM prompt. Our empirical studies highlight that ECO prompting significantly improves code-LLMs' ability to generate efficient code, achieving speedups of up to 7.81x while minimizing correctness loss.
翻译:代码运行时优化——即重写给定代码以提升其运行速度的任务——仍然具有挑战性,因为它需要权衡涉及算法与结构选择的性能取舍。现有方法通常利用代码大语言模型,并为其提供快慢代码对作为优化指导,但此类基于代码对的方法掩盖了性能提升的因果机制,往往导致模型仅进行表面模式模仿,而非真正的性能推理。本文提出ECO,一种面向代码优化的性能感知提示框架。ECO首先从参考快慢代码对中提炼出运行时优化指令;每条指令均描述低效性的根本原因及驱动性能改进的原理。对于给定的输入代码,ECO并行执行以下操作:(i) 通过符号化分析器生成针对该代码的瓶颈诊断;(ii) 通过ROI检索器返回相关的运行时优化指令。随后将这两部分输出组合成性能感知提示,为代码大语言模型提供可操作的指导。ECO的提示具有模型无关性,无需微调,并可轻松前置集成到任何代码大语言模型的提示中。我们的实证研究表明,ECO提示能显著提升代码大语言模型生成高效代码的能力,在将正确性损失降至最低的同时,最高可实现7.81倍的加速效果。