mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-21 17:52:12 +03:00
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:
@@ -198,9 +198,7 @@ pub fn heapsort<T, F>(v: &mut [T], mut is_less: F)
|
||||
}
|
||||
|
||||
// Choose the greater child.
|
||||
if child + 1 < v.len() && is_less(&v[child], &v[child + 1]) {
|
||||
child += 1;
|
||||
}
|
||||
child += (child + 1 < v.len() && is_less(&v[child], &v[child + 1])) as usize;
|
||||
|
||||
// Stop if the invariant holds at `node`.
|
||||
if !is_less(&v[node], &v[child]) {
|
||||
|
||||
Reference in New Issue
Block a user