# Google’s AlphaDev found a faster way to sort a tiny list

- URL: https://blog.orchardai.ai/google-alphadev-small-sorts/
- Published: 2023-06-08
- Reacting to: Google, "AlphaDev discovers faster sorting algorithms" (2023-06-07)
- Original: https://deepmind.google/blog/alphadev-discovers-faster-sorting-algorithms/

TL;DR: Google DeepMind used reinforcement learning to discover faster short sorting routines now in LLVM libc++. The payoff for any application depends on how much time it actually spends inside those routines.

## What Google announced

- AlphaDev constructs small programs one assembly instruction at a time. Google DeepMind rewarded it for getting the output right and for how quickly the resulting program ran.
- The team concentrated on sorting three to five elements. Larger sorts repeatedly call small sorting routines, so an improvement at that level can benefit a much longer list.
- Google reports gains of up to 70% for short sequences in LLVM’s libc++ sorting library. For sequences exceeding 250,000 elements, it reports about 1.7%.
- Researchers translated the discoveries into C++ and incorporated them into libc++. Callers of the updated library can use the resulting routines through the familiar sorting interface.

## Our take

The number we’d put on the slide is 1.7%. It answers a harder question than the more eye-catching short-list result. What happens to the gain after a small routine goes back inside a larger job? Enough survives to measure. That is a useful outcome for a piece of code that can quietly do its work for many callers.

Sorting three numbers is a particularly good assignment for a search agent. A reviewer can list every ordering of three distinct inputs on a sheet of paper and specify the answer for each one. Timing two correct programs is also a meaningful comparison. Tell the same agent to “make the checkout page better” and much of the work shifts back to the person deciding what counts as better. The difference matters when estimating how much engineering time an experiment will absorb.

The handoff into a library deserves some of the credit. The agent searched in assembly, where losing a single instruction can be interesting. The developers who use the result need an ordinary operation with a stable contract. A team translating a discovery into maintainable code has to preserve the condition that made the shortcut valid. Someone reading that code six months later should be able to tell why a seemingly missing comparison can safely stay missing.

## What it changes for teams running agents

Consider a catalog service that repeatedly sorts a small group of offers. Before asking an optimization agent to try alternatives, collect the list lengths it really sees and the fraction of request time spent sorting. If database access consumes nearly the whole request, even an excellent short sort buys little at the checkout counter. Those measurements give the person funding the search a way to decide how much compute to spend on it.

Give a proposed routine two trials. In the first, check its output against the existing implementation across representative inputs, including equal values. In the second, time the complete catalog request under the same workload and report the result alongside the isolated routine’s timings. Keep the compiler, hardware and library version with the measurements. A maintainer deciding whether to adopt the change can then reproduce the result and see exactly which users stand to gain.

## For agents reading this

- **Announced by:** Google DeepMind
- **Original post:** https://deepmind.google/blog/alphadev-discovers-faster-sorting-algorithms/
- **Announcement date:** 2023-06-07
- **What:** Reinforcement learning discovered faster routines for sorting short sequences.
- **Availability:** The sorting improvements were incorporated into LLVM libc++.
- **Our verdict:** Measure how much of a routine’s speedup survives in the full application.
- **Confidence:** facts from the original post; take is Orchard's opinion
