NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

LLM Program Optimization via Retrieval Augmented Search

Sagnik Anupam, Alexander Shypula, Osbert Bastani

TL;DR

While Large Language Models (LLMs) excel at writing functional code, optimizing programs for execution speed remains a major hurdle. This paper introduces Retrieval Augmented Search (RAS), a method that combines iterative beam search with "contextual retrieval" based on natural language descriptions of code rather than raw source code. The authors also introduce AEGIS, a framework that breaks down complex, monolithic code optimizations into small, human-readable "atomic edits" to mimic compiler passes and improve code interpretability.

What Problem Does This Paper Address?

LLMs frequently struggle with program optimization out-of-the-box because performance data is scarce in standard web-scale training sets. Existing black-box optimization approaches typically attempt to rewrite a slow program into a fast one in a single "end-to-end" shot.

This approach has two major flaws:

  1. It is fundamentally different from how modern compilers work, which apply sequential, targeted optimization passes.
  2. It often results in massive, monolithic code changes that are incredibly difficult for human developers to interpret, audit, or trust.

The authors set out to bridge this gap by making LLM-driven code optimization both more effective and more interpretable.

Methods

The paper introduces two core frameworks:

1. Retrieval Augmented Search (RAS)

Instead of a one-shot rewrite, RAS treats optimization as an iterative search problem using beam search.

  • Contextual Retrieval: For a given slow program, RAS first prompts an LLM ($F_{context}$) to generate a high-level natural language description of the underlying algorithm and data structures (e.g., "The algorithm uses dynamic programming with memoization to find the minimum cost...").
  • Example Retrieval: It embeds this description and uses vector search (FAISS) to find similar slow-fast program pairs from a training set.
  • Iterative Optimization: The retrieved pairs serve as in-context examples to prompt an optimization LLM ($F_{opt}$). RAS evaluates the generated candidates using a code execution environment, keeps the fastest correct variants, and repeats the process over multiple steps.
The RAS Framework overview

2. Atomic Edit GuIded Search (AEGIS)

To improve interpretability, AEGIS adds a dataset preprocessing step before running RAS.

  • It prompts an LLM to decompose large, end-to-end training examples of slow-fast code pairs into a sequence of isolated "atomic edits" accompanied by natural language explanations (e.g., replacing C++ cin/cout streams with faster C-style scanf/printf functions).
  • At test time, RAS searches over sequences of these smaller, generalized atomic edits rather than allowing the LLM to make arbitrary, sweeping rewrites.
The AEGIS Framework overview

Datasets

The authors evaluated their methods on two primary benchmarks:

  • PIE (Performance Improving Edits): A benchmark consisting of slow-fast C++ program pairs sourced from human submissions on CodeNet. To avoid stochastic execution noise, performance is measured deterministically using the gem5 hardware simulator.
  • Mercury: A code efficiency benchmark for Python consisting of LeetCode problems, where performance is evaluated using runtime percentiles.

Key Insights and Ideas

  • Contextual > Code Retrieval: Retrieving examples based on what the code does (natural language descriptions) significantly outperforms retrieving based on how the code looks (raw code embeddings). Code embeddings often get distracted by superficial details like variable names or formatting, whereas description embeddings capture the algorithmic core.
  • Search Beats One-Shot: Iterative beam search allows the LLM to build on its own optimizations. By updating the program description at each step, the retriever can find increasingly relevant examples for the next optimization pass.
  • The Interpretability Trade-off: Constraining the LLM to atomic edits (AEGIS) slightly reduces the maximum speedup compared to full RAS, but it dramatically reduces the size of code changes, making the optimization trajectory far easier for a human engineer to verify.

Key Results

  • Massive Speedups: On the C++ PIE benchmark, RAS achieved an 8.70$\times$ average speedup using Qwen-3-Coder (compared to 4.23$\times$ for standard dynamic retrieval) and 9.18$\times$ using DeepSeek 3.2.
  • Smaller, Cleaner Edits: AEGIS achieved a 6.08$\times$ speedup with GPT-4o while reducing the average edit size by 17% overall (and 30% on the first, most substantial edit) compared to RAS.
  • Python Improvements: On the Python Mercury benchmark, RAS boosted the mean runtime percentile of Qwen2.5-7B-Instruct by 10.27 points, narrowing the performance gap with much larger reasoning models.
  • High Accuracy: Formal verification using the C Bounded Model Checker (CBMC) confirmed that the optimized programs successfully maintained exact semantic equivalence to their original versions.

Limitations and Future Work

  • Computational Cost: Running iterative beam search with multiple LLM generation and simulation steps per iteration is computationally expensive and slower than one-shot generation.
  • AEGIS Preprocessing Overhead: Decomposing the training set into atomic edits requires a significant upfront investment of LLM compute.
  • Enterprise Scalability: The current approach was evaluated on competitive programming style problems (single-file scripts). Scaling this to massive, multi-file, object-oriented enterprise codebases remains an open challenge, as it requires identifying exactly which snippet of a large system needs optimization.

Summary updated

BibTeX
@inproceedings{anupam2026llm,
  author = {Sagnik Anupam and Alexander Shypula and Osbert Bastani},
  title = {{LLM Program Optimization via Retrieval Augmented Search}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2026},
  doi = {10.18653/v1/2026.findings-acl.2092},
}
Figure