mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 20:45:45 +03:00
Split branches in heapsort child selection
This allows even better code-gen, cmp + adc. While also more clearly communicating the intent.
This commit is contained in:
@@ -198,7 +198,12 @@ pub fn heapsort<T, F>(v: &mut [T], mut is_less: F)
|
||||
}
|
||||
|
||||
// Choose the greater child.
|
||||
child += (child + 1 < v.len() && is_less(&v[child], &v[child + 1])) as usize;
|
||||
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`.
|
||||
if !is_less(&v[node], &v[child]) {
|
||||
|
||||
Reference in New Issue
Block a user