Abstract
Sorting algorithms are fundamental to modern computing, yet their optimization has largely plateaued after decades of human refinement. We present AlphaDev, a deep reinforcement learning agent based on AlphaZero, designed to discover novel, high-performance assembly-level algorithms. By treating algorithm construction as a single-player game, AlphaDev discovered sorting routines that outperform existing state-of-the-art implementations in the LLVM C++ standard library. These discoveries include the "AlphaDev swap move" and "AlphaDev copy move," which reduce instruction counts through non-obvious logic. The resulting algorithms have been integrated into the standard C++ library, marking a significant milestone in AI-driven software optimization.
Introduction
Sorting is a ubiquitous operation in computer science, executed trillions of times daily. Even marginal efficiency gains in sorting routines can lead to substantial cumulative savings in energy and computational resources. However, human experts have optimized these algorithms for decades, making further improvements increasingly difficult. Traditional program synthesis methods often struggle with the vast combinatorial search space of low-level machine instructions. This research introduces AlphaDev, which leverages deep reinforcement learning (DRL) to explore the "algorithm space" beyond human intuition, seeking "hitherto unknown" routines that surpass human-engineered benchmarks.
Methods
The researchers formulated algorithm discovery as a single-player game termed AssemblyGame. In this environment, the agent, AlphaDev, selects low-level CPU instructions (x86 assembly) to append to a program. AlphaDev extends the AlphaZero architecture, utilizing a Transformer-based representation to capture the structure of the generated code and a CPU state encoder to track register and memory values.
The agent employs Monte Carlo Tree Search (MCTS) to plan its moves. A critical innovation is the dual reward system: one "value head" evaluates Correctness by testing the program against synthetic input sequences (e.g., all permutations of a short list), while the other evaluates Latency, rewarding the agent for shorter programs or lower measured execution time on physical hardware.
Key Insights and Innovations
AlphaDev's search led to the discovery of several novel optimization strategies that had previously eluded human developers:
- The AlphaDev Swap Move: In sorting three elements, AlphaDev identified that if the second and third elements are already sorted, a step can be skipped. It found a way to compute
min(A, B)instead of the standardmin(A, B, C), saving one instruction per call. - The AlphaDev Copy Move: For sort-8 configurations, the agent discovered a non-obvious "copy" logic that bypassed traditional comparison steps, further reducing the instruction count.
- Rethinking Variable Sorts: For lists of varying lengths (e.g., 1–4 items), AlphaDev moved away from traditional "switch" statements. Instead, it sorts a subset and uses simplified logic to "tuck in" remaining items, yielding significant speedups.
Results and Production Impact
AlphaDev discovered fixed-sort algorithms for 3 and 5 elements that were shorter than previous human benchmarks. For small sequences (length 1–5), these algorithms were up to 70% faster than existing library implementations. When used as sub-routines for large arrays (over 250,000 elements), they provided a 1.7% overall speedup.
These discovered routines for sort3, sort4, and sort5 have been officially integrated into the LLVM libc++ library, marking the first time a component of this foundational library has been replaced by an AI-discovered routine. Beyond sorting, AlphaDev also optimized the VarInt deserialization task, finding a branchless solution 3x faster than human benchmarks.
Conclusion and Future Work
AlphaDev demonstrates the potential of DRL to transcend human intuition in low-level code optimization. While currently limited to "small sorts" (up to 8 elements) due to the complexity of the search space, these building blocks have already had a tangible impact on production software. Future work aims to combine DRL with stochastic search to scale these techniques to larger, more complex algorithms such as hashing and cryptography, potentially revolutionizing how foundational software libraries are written.
Summary updated