Abstract
Sorting is a fundamental building block for data-intensive applications like databases and search engines. While radix sort offers $O(N)$ theoretical complexity, its practical performance is often limited by memory bandwidth and microarchitectural bottlenecks. This paper presents a highly optimized radix sort algorithm that leverages 64-bit virtual memory to eliminate redundant passes and employs "Software Write-Combining" to saturate memory bandwidth. Our implementation, "VM+WC," achieves a throughput of 621 million items per second, outperforming contemporary Intel implementations by 1.5x and proving that optimized CPUs can remain competitive with GPUs for large-scale sorting tasks.
Introduction
Despite its theoretical efficiency, standard radix sort implementations face several real-world hurdles. First, traditional counting sort requires an initial pass to build a histogram, leading to redundant data movement. Second, writing large volumes of data can "pollute" the CPU cache, triggering expensive "Read-For-Ownership" transactions. Finally, modern CPUs have a limited number of hardware write-combine buffers; when sorting into many buckets, these buffers overflow, causing significant performance degradation. This work addresses these issues by "hacking" low-level hardware features to achieve near-optimal performance.
Methods
The proposed algorithm introduces two primary innovations:
Virtual-Memory Counting Sort
Instead of a preliminary pass to count bucket sizes, the algorithm allocates a massive chunk of virtual address space (terabytes) for all potential buckets. Leveraging the way modern operating systems handle memory, physical RAM is only mapped when a bucket is first accessed. This allows for a single-pass distribution of items, trading plentiful 64-bit address space for reduced computation time.
Software Write-Combining (SWC)
To overcome the bottleneck of limited hardware write buffers, we manually implement buffering in software. Small, cache-aligned buffers in the L1 cache collect data. Once a buffer is full, non-temporal streaming stores (SSE2 instructions) are used to write data directly to RAM, bypassing the cache hierarchy and avoiding cache pollution.
Hybrid MSD/LSD Approach
The algorithm utilizes a "Most Significant Digit" (MSD) pass to partition data across CPU cores, ensuring NUMA-awareness. This is followed by "Least Significant Digit" (LSD) passes for local sorting within each partition.
Results
The algorithm was evaluated using 64 million (64 Mi) 32-bit integer keys generated with the WELL512 algorithm to ensure uniform distribution. Tests included both "keys only" (K=32, V=0) and 32-bit key-value pairs (K=32, V=32).
As shown in Table 1, the optimized "VM+WC" version reached a peak throughput of 621 million items per second for keys-only sorting. This implementation utilized 88% of the system's peak memory bandwidth. Notably, the VM+WC approach is 1.5x faster than Intel's best contemporary radix sort and 1.24x faster than a high-end NVIDIA Fermi GPU (GTX 480) when accounting for PCIe data transfer overhead.
Conclusion
By treating the 64-bit address space as a functional data structure and exercising direct control over the cache via software write-combining, we demonstrate that CPUs can achieve exceptional performance on memory-bound tasks. While limited by current OS address space constraints (e.g., 8 TiB on older Windows versions) and potential sensitivity to skewed data distributions, this approach highlights the importance of hardware-aware algorithm design. Future work will explore recursive MSD passes for even larger datasets.
Summary updated