Module smallsort
This module contains a variety of sort implementations that are optimized for small lengths.
Structs
Traits
- CopyMarker SAFETY: Only used for run-time optimization heuristic.
-
StableSmallSortTypeImpl
Using a trait allows us to specialize on
Freezewhich in turn allows us to make safe abstractions. - UnstableSmallSortFreezeTypeImpl FIXME(const_trait_impl) use original ipnsort approach with choose_unstable_small_sort, as found here https://github.com/Voultapher/sort-research-rs/blob/438fad5d0495f65d4b72aa87f0b62fc96611dff3/ipnsort/src/smallsort.rs#L83C10-L83C36.
-
UnstableSmallSortTypeImpl
Using a trait allows us to specialize on
Freezewhich in turn allows us to make safe abstractions.
Functions
- bidirectional_merge Merge v assuming v[..len / 2] and v[len / 2..] are sorted.
- has_efficient_in_place_swap
- insert_tail Sorts range [begin, tail] assuming [begin, tail) is already sorted.
-
insertion_sort_shift_left
Sort
vassumingv[..offset]is already sorted. - merge_down
- merge_up
- panic_on_ord_violation
- small_sort_fallback
- small_sort_general
- small_sort_general_with_scratch
- small_sort_network
-
sort13_optimal
Sorts the first 13 elements of
vwith a fast fixed function. -
sort4_stable
SAFETY: The caller MUST guarantee that
v_baseis valid for 4 reads anddstis valid for 4 writes. The result will be stored indst[0..4]. -
sort8_stable
SAFETY: The caller MUST guarantee that
v_baseis valid for 8 reads and writes,scratch_baseanddstMUST be valid for 8 writes. The result will be stored indst[0..8]. -
sort9_optimal
Sorts the first 9 elements of
vwith a fast fixed function. -
swap_if_less
Swap two values in the slice pointed to by
v_baseat the positiona_posandb_posif the value at positionb_posis less than the one at positiona_pos.
Constants
-
MAX_STACK_ARRAY_SIZE
Using a stack array, could cause a stack overflow if the type
Tis very large. To be conservative we limit the usage of small-sorts that require a stack array to types that fit within this limit. - SMALL_SORT_FALLBACK_THRESHOLD Optimal number of comparisons, and good perf.
-
SMALL_SORT_GENERAL_SCRATCH_LEN
small_sort_generalusessort8_stableas primitive and does a kind of ping-pong merge, where the output of the first twosort8_stablecalls is stored at the end of the scratch buffer. This simplifies panic handling and avoids additional copies. This affects the required scratch buffer size. - SMALL_SORT_GENERAL_THRESHOLD From a comparison perspective 20 was ~2% more efficient for fully random input, but for wall-clock performance choosing 32 yielded better performance overall.
- SMALL_SORT_NETWORK_SCRATCH_LEN
-
SMALL_SORT_NETWORK_THRESHOLD
SAFETY: If you change this value, you have to adjust
small_sort_network!