Reinforcement Learning for Code Optimization

Pierre Chambon, Kunhao Zheng, Juliette Decugis, Benoit Sagot, Gabriel SynnaevearXiv 2026

TL;DR

While Large Language Models (LLMs) have become adept at writing code that works, they often fail to write code that is fast. This paper introduces a robust framework for using Reinforcement Learning (RL) to optimize code execution time, achieving a 125% relative improvement in high-efficiency code generation on the CWM 32B model without sacrificing correctness.

What Problem Does This Paper Address?

In the world of competitive programming and software engineering, a solution that is correct but slow is often as useless as one that is wrong. Current Reinforcement Learning from Verifiable Rewards (RLVR) techniques excel at training models for functional correctness—essentially a binary pass/fail signal.

However, extending RL to code optimization is surprisingly difficult. Naively adding execution time to a reward function usually fails because:

  1. Measurement Noise: Small fluctuations in system load (jitter) can overwhelm the timing signal, especially for fast-running programs.
  2. Reward Sparsity: It is much harder for a model to randomly stumble upon a significantly faster algorithm than it is to find a correct one.
  3. Correctness Trade-offs: Models might "cheat" by finding fast but incorrect solutions if the reward isn't carefully balanced.

The authors tackle these challenges by making execution time a stable, learnable signal through better data, calibrated environments, and refined training objectives.

Methods

The authors propose a three-stage approach to make timing learnable:

  1. Calibrated Execution: Instead of timing code on the same workers running the RL training (which are noisy), they use a dedicated Remote Sandbox (CES). They apply affine calibration to map stored human-reference durations to the current environment's scale, ensuring that "fast" remains a consistent target.
  2. Environment Taxonomy: They categorize how optimization pressure can be applied:
  3. Pre-execution: Filtering out tests that are too small to provide a reliable timing signal.
  4. Intra-execution: Setting strict, reference-derived timeouts during the run.
  5. Post-execution: Ranking the model's solution against a leaderboard of human submissions.
  6. Offline Simulation: To avoid wasting massive compute on failing RL configurations, they developed an RL Simulator. This tool replays human solutions through candidate reward functions to predict which environments will provide the best learning signal.
The full pipeline for making execution time learnable, from problem sampling to GRPO training.

The training itself uses Group Relative Policy Optimization (GRPO), stabilized with larger batch sizes and token-weighted advantage centering to handle the noisier, sparser rewards of the optimization setting.

Datasets

  • DMC-Optim: A new, high-quality benchmark derived from the DeepMind Code Contests (DMC) corpus. The authors cleaned 2,723 problems and generated over 700,000 new tests specifically designed to separate solutions by execution speed.
  • LiveCodeBench (LCB): Used as an out-of-distribution transfer check to see if models trained on DMC-Optim can produce faster code on entirely new problems.
The construction pipeline for DMC-Optim, showing how problems are filtered for "duration filterability."

Key Insights and Ideas

  • Noise is the "Signal Killer": The authors found that a mere 50ms of measurement noise can make standard RL fail. By moving to a calibrated remote service, they increased the correlation between training rewards and actual model improvement.
  • Correctness as a Hard Gate: The most successful reward functions were "collapsed" or "gated." This means the model receives a penalty for incorrect code regardless of speed, preventing it from "reward hacking" by failing fast.
  • The Power of Simulation: The offline simulator allowed the team to screen thousands of environment parameters (like timeout thresholds) in minutes on a single CPU, rather than days on a GPU cluster.
The offline simulator helps select the best environment configurations by plotting "Steepness" against "Reward Level."

Key Results

The results show that optimization-aware RL significantly shifts the model's output distribution toward more efficient algorithms:

  • DMC-Optim Performance: On the CWM 32B model, the "pass@1" rate for solutions in the top 30% of human speed (p30) jumped from 13.7% to 30.9%—a 125% relative gain.
  • Zero-Shot Transfer: On LiveCodeBench, the optimization-trained CWM 32B model won 83% of speed comparisons against a standard RLVR baseline.
  • Algorithmic Discovery: Qualitative analysis showed the model didn't just learn "micro-optimizations" (like faster I/O); in many cases, it learned to replace generic algorithms with more advanced ones, such as replacing Gaussian elimination with near-linear Union-Find for specific graph problems.

Limitations and Future Work

  • Scope: The study focuses on single-file Python problems. Real-world software optimization often involves multi-file repositories, memory constraints, and complex build systems.
  • Human Baselines: The ranking rewards currently rely on a fixed pool of human reference solutions. Future work could involve "self-play" or iterative refinement where the model competes against its own previous best versions.
  • Compute Cost: Even with the simulator, the full pipeline requires significant execution resources to provide the millions of timing measurements needed for RL.

Summary updated

BibTeX
@misc{chambon2026reinforcement,
  author = {Pierre Chambon and Kunhao Zheng and Juliette Decugis and Benoit Sagot and Gabriel Synnaeve},
  title = {{Reinforcement Learning for Code Optimization}},
  year = {2026},
  eprint = {2607.25970},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.25970},
}
Figure