BlockQuicksort: How Branch Mispredictions don't affect Quicksort

Stefan Edelkamp, Armin Weiß
4/22/2016
12 citations
cs.DS

Official code: weissan/BlockQuicksort (40 stars, C++, GPL-3.0, last commit 2021-11-29).

Abstract

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.

Show full abstract

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.

View on arXivView PDF

Code Implementations(1)

GPL-3.0
406C++, PythonJun 20, 20165 years agoGPL-3.0

Similar Papers

Cite this paper

@article{edelkamp2016blockquicksort,
  title  = {BlockQuicksort: How Branch Mispredictions don't affect Quicksort},
  author = {Stefan Edelkamp and Armin Weiß},
  year   = {2016},
  eprint = {1604.06697},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/1604.06697}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/1604.06697.svg)](https://codeofpaper.com/paper/1604.06697)

Discussion