[Paper Review] Analysis of Branch Misses in Quicksort
This paper provides the first analytical study of branch mispredictions in classic and dual-pivot Quicksort, modeling their impact via a linear cost function combining comparisons (BC) and branch misses (BM). It shows that branch mispredictions alone cannot explain Yaroslavskiy’s dual-pivot Quicksort performance advantage, suggesting memory hierarchy effects are more critical, and derives optimal sampling strategies for finite sample sizes under various branch prediction models.
The analysis of algorithms mostly relies on counting classic elementary operations like additions, multiplications, comparisons, swaps etc. This approach is often sufficient to quantify an algorithm's efficiency. In some cases, however, features of modern processor architectures like pipelined execution and memory hierarchies have significant impact on running time and need to be taken into account to get a reliable picture. One such example is Quicksort: It has been demonstrated experimentally that under certain conditions on the hardware the classically optimal balanced choice of the pivot as median of a sample gets harmful. The reason lies in mispredicted branches whose rollback costs become dominating. In this paper, we give the first precise analytical investigation of the influence of pipelining and the resulting branch mispredictions on the efficiency of (classic) Quicksort and Yaroslavskiy's dual-pivot Quicksort as implemented in Oracle's Java 7 library. For the latter it is still not fully understood why experiments prove it 10% faster than a highly engineered implementation of a classic single-pivot version. For different branch prediction strategies, we give precise asymptotics for the expected number of branch misses caused by the aforementioned Quicksort variants when their pivots are chosen from a sample of the input. We conclude that the difference in branch misses is too small to explain the superiority of the dual-pivot algorithm.
Motivation & Objective
- To precisely analyze the expected number of branch misses in classic and dual-pivot Quicksort under finite sampling and various branch prediction strategies.
- To evaluate whether branch mispredictions can explain the 10% performance gain of Yaroslavskiy’s dual-pivot Quicksort over classic Quicksort in Java 7.
- To derive optimal sampling parameters (t*) for both algorithms under a combined cost model of comparisons and branch misses (BC + ξ·BM).
- To provide a quantitative model that captures real-world performance trends despite simplifying assumptions about processor behavior.
- To exclude branch mispredictions as the primary explanation for Yaroslavskiy’s superiority, supporting the memory hierarchy hypothesis.
Proposed method
- Models branch misprediction costs using a linear combination of comparisons (BC) and branch misses (BM), parameterized by ξ.
- Analyzes both classic Quicksort (CQS) and Yaroslavskiy’s dual-pivot Quicksort (YQS) with pivot selection from finite samples of size k.
- Derives asymptotic expressions for expected branch misses under different branch prediction strategies: 1-bit, 2-bit fixed-complement (fc), and 2-bit saturating counter (sc).
- Computes optimal sampling positions t* for finite k by minimizing the cost function BC + ξ·BM, varying ξ to reflect different hardware trade-offs.
- Compares analytical results with experimental data from Kaligosi and Sanders, validating model accuracy under realistic conditions.
- Uses normalized plots of t* / k to visualize optimal sampling skew as a function of ξ for different k and prediction models.
Experimental results
Research questions
- RQ1Can branch mispredictions alone explain the 10% performance improvement of Yaroslavskiy’s dual-pivot Quicksort over classic Quicksort?
- RQ2What is the optimal sampling strategy (t*) for classic and dual-pivot Quicksort under a combined cost model of comparisons and branch misses?
- RQ3How does the optimal sampling skew (t*) vary with the relative cost of branch mispredictions (ξ) for different branch prediction strategies?
- RQ4Does symmetric sampling remain optimal for Yaroslavskiy’s algorithm when branch misprediction costs are high?
- RQ5To what extent does the model’s prediction of optimal pivot selection (τ*) match experimental observations on real processors?
Key findings
- The analysis shows that branch mispredictions alone cannot explain Yaroslavskiy’s 10% performance gain, as the difference in BM counts between CQS and YQS is too small.
- For classic Quicksort, optimal sampling becomes increasingly skewed (t* decreases) as the cost of branch mispredictions (ξ) increases, with t* dropping from (k-1)/2 to 0 as ξ grows.
- For Yaroslavskiy’s dual-pivot Quicksort, even at ξ=0 (no branch misprediction cost), the optimal sampling parameters are asymmetric: τ* ≈ (0.207, 0.349, 0.445), reducing expected bytecodes by 3.6% compared to symmetric sampling.
- The model predicts optimal pivot selection (τ*) that aligns well with experimental observations on Pentium 4, where τ* ≈ 1/10 was found optimal under 2-bit fixed-complement prediction.
- The optimal sampling position t* for finite k decreases monotonically with increasing ξ, and the transition points from one t* value to the next can be computed exactly for each prediction strategy.
- Despite its simplicity, the model BC + ξ·BM captures essential performance trends in real hardware, especially when ξ is in the range of 30–80, corresponding to typical modern processor branch prediction costs.
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.