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:
Lukas Bergdoll
2023-02-10 18:00:31 +01:00
parent d1ac43a9b9
commit 7e072199a6
+1 -3
View File
@@ -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]) {