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:
bors 2023-02-12 03:30:10 +00:00
commit b7089e0dd3

View File

@ -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`.