BlockQuicksort:分支预测错误如何不影响快速排序
BlockQuicksort: How Branch Mispredictions don't affect Quicksort
摘要:
自Kaligosi和Sanders(2006)的工作以来,众所周知,Quicksort-通常被认为是最快的就地排序算法之一-在本质上受到分支错误预测的影响。我们提出了一种新的方法来解决这个问题,通过部分解耦控制从数据流:为了执行分区,我们分裂的输入块的恒定大小(我们建议128个数据元素),然后,在一个块中的所有元素与枢轴和比较的结果存储在一个缓冲区。在第二遍中,重新布置相应的元件。通过这样做,我们完全避免了基于比较结果的条件分支(除了最后的Insertionsort)。此外,我们证明,对于静态分支预测的分支误预测的平均总数是最多$εn \log n + O(n)$一些小的$ε$取决于块大小排序时,$n$元素。我们的实验结果是有希望的:当对随机整数数据进行排序时,我们实现了比C++ std::sort的GCC实现速度提高80%。对于许多其他类型的数据和非随机输入,仍然比std::sort有显著的加速。只有在少数特殊情况下,如排序或几乎排序的输入,std::sort可以击败实现。此外,即使在随机输入排列上,我们的实现甚至比高度调优的Super Scalar Sample Sort的实现稍快,后者使用线性量的额外空间。
英文摘要:
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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。