core/slice/sort/unstable/mod.rs
1//! This module contains the entry points for `slice::sort_unstable`.
2
3use crate::intrinsics;
4use crate::mem::SizedTypeProperties;
5#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
6use crate::slice::sort::shared::find_existing_run;
7#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
8use crate::slice::sort::shared::smallsort::insertion_sort_shift_left;
9
10pub(crate) mod heapsort;
11pub(crate) mod quicksort;
12
13/// Unstable sort called ipnsort by Lukas Bergdoll and Orson Peters.
14/// Design document:
15/// <https://github.com/Voultapher/sort-research-rs/blob/main/writeup/ipnsort_introduction/text.md>
16///
17/// Upholds all safety properties outlined here:
18/// <https://github.com/Voultapher/sort-research-rs/blob/main/writeup/sort_safety/text.md>
19#[inline(always)]
20pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
21 // Arrays of zero-sized types are always all-equal, and thus sorted.
22 if T::IS_ZST {
23 return;
24 }
25
26 // Instrumenting the standard library showed that 90+% of the calls to sort
27 // by rustc are either of size 0 or 1.
28 let len = v.len();
29 if intrinsics::likely(len < 2) {
30 return;
31 }
32
33 cfg_if! {
34 if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
35 heapsort::heapsort(v, is_less);
36 } else {
37 // More advanced sorting methods than insertion sort are faster if called in
38 // a hot loop for small inputs, but for general-purpose code the small
39 // binary size of insertion sort is more important. The instruction cache in
40 // modern processors is very valuable, and for a single sort call in general
41 // purpose code any gains from an advanced method are cancelled by i-cache
42 // misses during the sort, and thrashing the i-cache for surrounding code.
43 const MAX_LEN_ALWAYS_INSERTION_SORT: usize = 20;
44 if intrinsics::likely(len <= MAX_LEN_ALWAYS_INSERTION_SORT) {
45 insertion_sort_shift_left(v, 1, is_less);
46 return;
47 }
48
49 ipnsort(v, is_less);
50 }
51 }
52}
53
54/// See [`sort`]
55///
56/// Deliberately don't inline the main sorting routine entrypoint to ensure the
57/// inlined insertion sort i-cache footprint remains minimal.
58#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
59#[inline(never)]
60fn ipnsort<T, F>(v: &mut [T], is_less: &mut F)
61where
62 F: FnMut(&T, &T) -> bool,
63{
64 let len = v.len();
65 let (run_len, was_reversed) = find_existing_run(v, is_less);
66
67 // SAFETY: find_existing_run promises to return a valid run_len.
68 unsafe { intrinsics::assume(run_len <= len) };
69
70 if run_len == len {
71 if was_reversed {
72 v.reverse();
73 }
74
75 // It would be possible to a do in-place merging here for a long existing streak. But that
76 // makes the implementation a lot bigger, users can use `slice::sort` for that use-case.
77 return;
78 }
79
80 // Limit the number of imbalanced partitions to `2 * floor(log2(len))`.
81 // The binary OR by one is used to eliminate the zero-check in the logarithm.
82 let limit = 2 * (len | 1).ilog2();
83 crate::slice::sort::unstable::quicksort::quicksort(v, None, limit, is_less);
84}