File tree Expand file tree Collapse file tree 1 file changed +16
-10
lines changed
library/core/src/slice/sort/unstable Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Original file line number Diff line number Diff 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))`.
You can’t perform that action at this time.
0 commit comments