Skip to content

Commit 859ce3c

Browse files
committed
Keep heapsorts implementation equal to std
rust-lang/rust#93765 shows a 10% speedup and had been merged into std. Given that rayon's code was an exact copy of the heapsort in std, this PR implements the same optimization.
1 parent 99b1ff5 commit 859ce3c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/slice/quicksort.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,25 @@ where
191191
// This binary heap respects the invariant `parent >= child`.
192192
let sift_down = |v: &mut [T], mut node| {
193193
loop {
194-
// Children of `node`:
195-
let left = 2 * node + 1;
196-
let right = 2 * node + 2;
194+
// Children of `node`.
195+
let mut child = 2 * node + 1;
196+
if child >= v.len() {
197+
break;
198+
}
197199

198200
// Choose the greater child.
199-
let greater = if right < v.len() && is_less(&v[left], &v[right]) {
200-
right
201-
} else {
202-
left
203-
};
201+
if child + 1 < v.len() && is_less(&v[child], &v[child + 1]) {
202+
child += 1;
203+
}
204204

205205
// Stop if the invariant holds at `node`.
206-
if greater >= v.len() || !is_less(&v[node], &v[greater]) {
206+
if !is_less(&v[node], &v[child]) {
207207
break;
208208
}
209209

210210
// Swap `node` with the greater child, move one step down, and continue sifting.
211-
v.swap(node, greater);
212-
node = greater;
211+
v.swap(node, child);
212+
node = child;
213213
}
214214
};
215215

0 commit comments

Comments
 (0)