[Paper Review] BlockQuicksort: How Branch Mispredictions don't affect Quicksort
BlockQuicksort is a novel in-place sorting algorithm that eliminates branch mispredictions in Quicksort by partitioning data in fixed-size blocks (e.g., 128 elements), storing comparison results in buffers before rearranging elements. This approach reduces average branch mispredictions to at most $\epsilon n\log n + \mathcal{O}(n)$, achieving over 80% faster sorting than GCC's std::sort on random integer data and outperforming even Super Scalar Sample Sort on certain workloads.
Since the work of Kaligosi and Sanders (2006), it is well-known that Quicksort -- which is commonly considered as one of the fastest in-place sorting algorithms -- suffers in an essential way from branch mispredictions. We present a novel approach to address this problem by partially decoupling control from data flow: in order to perform the partitioning, we split the input in blocks of constant size (we propose 128 data elements); then, all elements in one block are compared with the pivot and the outcomes of the comparisons are stored in a buffer. In a second pass, the respective elements are rearranged. By doing so, we avoid conditional branches based on outcomes of comparisons at all (except for the final Insertionsort). Moreover, we prove that for a static branch predictor the average total number of branch mispredictions is at most $εn \log n + O(n)$ for some small $ε$ depending on the block size when sorting $n$ elements. Our experimental results are promising: when sorting random integer data, we achieve an increase in speed of 80% over the GCC implementation of C++ std::sort. Also for many other types of data and non-random inputs, there is still a significant speedup over std::sort. Only in few special cases like sorted or almost sorted inputs, std::sort can beat out implementation. Moreover, even on random input permutations, our implementation is even slightly faster than an implementation of the highly tuned Super Scalar Sample Sort, which uses a linear amount of additional space.
Motivation & Objective
- To address the performance degradation in Quicksort caused by branch mispredictions on modern pipelined processors.
- To design an in-place sorting algorithm that minimizes branch mispredictions without relying on complex pivot selection or external memory.
- To maintain high performance across diverse data types, including random, nearly sorted, and duplicate-heavy inputs.
- To achieve competitive performance against highly optimized algorithms like Super Scalar Sample Sort while remaining in-place.
Proposed method
- Partition the input array into fixed-size blocks (e.g., 128 elements) to decouple control flow from data-dependent comparisons.
- For each block, compare all elements with the pivot and store the comparison outcomes (e.g., 'less than' or 'greater than') in a buffer, avoiding conditional branches during comparison.
- After all comparisons are completed, perform a second pass to rearrange elements based on the stored comparison results, eliminating data-dependent branches during partitioning.
- Use Insertionsort as the recursion base case, but optimize buffer usage and block processing to reduce overall branch mispredictions.
- Apply a static branch predictor model to theoretically bound the number of branch mispredictions to $\epsilon n\log n + \mathcal{O}(n)$, where $\epsilon$ depends on block size.
- Use median-of-$\sqrt{n}$ or median-of-5 for pivot selection to balance performance and predictability.
Experimental results
Research questions
- RQ1Can branch mispredictions in Quicksort be significantly reduced by decoupling comparison outcomes from control flow decisions?
- RQ2Does buffering comparison results in blocks lead to a measurable performance improvement on modern processors with deep pipelines?
- RQ3Can an in-place sorting algorithm achieve performance comparable to or better than highly optimized algorithms like Super Scalar Sample Sort while avoiding dynamic memory allocation?
- RQ4How does the block-based approach perform across diverse input types, including random, nearly sorted, and duplicate-heavy data?
- RQ5Can the theoretical bound on branch mispredictions be achieved in practice, and how does it scale with block size?
Key findings
- BlockQuicksort reduces the average number of branch mispredictions to at most $\epsilon n\log n + \mathcal{O}(n)$, where $\epsilon$ is small and depends on block size, under a static branch predictor model.
- On random integer data, BlockQuicksort achieves over 80% higher throughput (elements sorted per second) than GCC’s std::sort.
- For all tested data types, including random permutations, nearly sorted arrays, and data with many duplicates, BlockQuicksort outperforms std::sort, except in rare cases like already-sorted inputs.
- Even on random permutations, BlockQuicksort is slightly faster than Super Scalar Sample Sort, which uses linear extra space, due to better branch prediction and cache behavior.
- The algorithm achieves low instruction count and cache miss rates, with only 2.25 branch mispredictions per element on average, significantly lower than std::sort’s 10.23.
- The use of duplicate-checking (dc) and median-of-$\sqrt{n}$ pivot selection further improves performance, especially on data with repeated elements.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.