mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Auto merge of #107894 - Voultapher:improve-heapsort-fallback, r=scottmcm
Speedup heapsort by 1.5x by making it branchless `slice::sort_unstable` will fall back to heapsort if it repeatedly fails to find a good pivot. By making the core child update code branchless it is much faster. On Zen3 sorting 10k `u64` and forcing the sort to pick heapsort, results in: 455us -> 278us
This commit is contained in:
commit
b7089e0dd3
@ -198,8 +198,11 @@ where
|
||||
}
|
||||
|
||||
// Choose the greater child.
|
||||
if child + 1 < v.len() && is_less(&v[child], &v[child + 1]) {
|
||||
child += 1;
|
||||
if child + 1 < v.len() {
|
||||
// We need a branch to be sure not to out-of-bounds index,
|
||||
// but it's highly predictable. The comparison, however,
|
||||
// is better done branchless, especially for primitives.
|
||||
child += is_less(&v[child], &v[child + 1]) as usize;
|
||||
}
|
||||
|
||||
// Stop if the invariant holds at `node`.
|
||||
|
Loading…
Reference in New Issue
Block a user