Skip to content

Commit f359b11

Browse files
Auto merge of #143495 - AngelicosPhosphoros:angelicos_phosphoros/skip_run_detection_in_sort_unstable, r=<try>
Skip `find_existing_run` call if head and tail pairs sorted differently This would help to avoid running comparator for all elements when user pushed an element to end of sorted Vec, breaking order only in last element. r? `@Voultapher`
2 parents 6dec76f + 5735153 commit f359b11

File tree

1 file changed

+16
-10
lines changed
  • library/core/src/slice/sort/unstable

1 file changed

+16
-10
lines changed

library/core/src/slice/sort/unstable/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,25 @@ where
6363
F: FnMut(&T, &T) -> bool,
6464
{
6565
let len = v.len();
66-
let (run_len, was_reversed) = find_existing_run(v, is_less);
66+
// Skip `find_existing_run` if head and tail sorted differently
67+
// because we are interested only if the whole slice is sorted.
68+
// This should skip in cases when user tries to sort a vector after single call to push.
69+
let check_existing_run = len < 3 || is_less(&v[0], &v[1]) == is_less(&v[len - 2], &v[len - 1]);
70+
if check_existing_run {
71+
let (run_len, was_reversed) = find_existing_run(v, is_less);
6772

68-
// SAFETY: find_existing_run promises to return a valid run_len.
69-
unsafe { intrinsics::assume(run_len <= len) };
73+
// SAFETY: find_existing_run promises to return a valid run_len.
74+
unsafe { intrinsics::assume(run_len <= len) };
7075

71-
if run_len == len {
72-
if was_reversed {
73-
v.reverse();
74-
}
76+
if run_len == len {
77+
if was_reversed {
78+
v.reverse();
79+
}
7580

76-
// It would be possible to a do in-place merging here for a long existing streak. But that
77-
// makes the implementation a lot bigger, users can use `slice::sort` for that use-case.
78-
return;
81+
// It would be possible to a do in-place merging here for a long existing streak. But that
82+
// makes the implementation a lot bigger, users can use `slice::sort` for that use-case.
83+
return;
84+
}
7985
}
8086

8187
// Limit the number of imbalanced partitions to `2 * floor(log2(len))`.

0 commit comments

Comments
 (0)