RC RANDOM CHAOS

Google's SIMD Quicksort sorts 10x faster than std::sort, portable across CPUs

· via Hacker News

Original source

Vectorized and performance-portable Quicksort

Hacker News →

Google has open-sourced a vectorized Quicksort that sorts arrays of numbers roughly ten times faster than C++‘s std::sort while running on every modern CPU architecture from a single codebase. The speedup comes from SIMD/vector instructions, which process many elements per instruction—16 float32 values at once on AVX-512, four on Arm NEON. The trick to applying SIMD to sorting, which inherently rearranges neighboring elements, is that Quicksort spends most of its time partitioning data around a pivot. Newer instruction sets (Arm SVE, RISC-V V, x86 AVX-512) offer a compress-store instruction that writes only the elements matching a yes/no mask to consecutive memory, which maps cleanly onto partitioning; on older sets like AVX2 that lack it, the operation is emulated with permute instructions.

The implementation is built on Google’s Highway portable SIMD library, so the same ~3,000 lines of C++ run across six instruction sets on three architectures rather than being hand-tuned per platform, with Highway selecting the best available instructions at runtime. It also goes beyond prior work by handling 16- to 128-bit inputs instead of only 32-bit integers. Despite being fully portable, it sets performance records: about 499 MB/s on an Apple M1, roughly 1,120 MB/s on a 3 GHz Skylake with AVX-512, and it beats even AVX2-specific state-of-the-art sorts (798 vs. 699 MB/s). Against the standard library’s 58–128 MB/s, that’s a 9–19x gain depending on data type.

The significance is that sorting has long been treated as an expensive operation that shapes how databases and query engines are designed—particularly the increasingly common columnar layouts where sorting and filtering drive SQL performance. Being able to sort at nearly 1 GB/s on a single core could reopen design choices that previously assumed sorting was too costly. The Apache 2.0-licensed code is on GitHub, with an accompanying paper detailing the method, including the specialized routine for sorting small (256-element) blocks.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.