forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#129587 - Voultapher:opt-for-size-variants-of-…
…sort-impls, r=cuviper Add `optimize_for_size` variants for stable and unstable sort as well as select_nth_unstable - Stable sort uses a simple merge-sort that re-uses the existing - rather gnarly - merge function. - Unstable sort jumps directly to the branchless heapsort fallback. - select_nth_unstable jumps directly to the median_of_medians fallback, which is augmented with a custom tiny smallsort and partition impl. Some code is duplicated but de-duplication would bring it's own problems. For example `swap_if_less` is critical for performance, if the sorting networks don't inline it perf drops drastically, however `#[inline(always)]` is also a poor fit, if the provided comparison function is huge, it gives the compiler an out to only instantiate `swap_if_less` once and call it. Another aspect that would suffer when making `swap_if_less` pub, is having to cfg out dozens of functions in in smallsort module. Part of rust-lang#125612 r? `@Kobzol`
- Loading branch information
Showing
8 changed files
with
202 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![cfg_attr(feature = "optimize_for_size", allow(dead_code))] | ||
|
||
use crate::marker::Freeze; | ||
|
||
pub(crate) mod pivot; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Binary-size optimized mergesort inspired by https://github.com/voultapher/tiny-sort-rs. | ||
|
||
use crate::mem::MaybeUninit; | ||
use crate::ptr; | ||
use crate::slice::sort::stable::merge; | ||
|
||
/// Tiny recursive top-down merge sort optimized for binary size. It has no adaptiveness whatsoever, | ||
/// no run detection, etc. | ||
#[inline(always)] | ||
pub fn mergesort<T, F: FnMut(&T, &T) -> bool>( | ||
v: &mut [T], | ||
scratch: &mut [MaybeUninit<T>], | ||
is_less: &mut F, | ||
) { | ||
let len = v.len(); | ||
|
||
if len > 2 { | ||
let mid = len / 2; | ||
|
||
// SAFETY: mid is in-bounds. | ||
unsafe { | ||
// Sort the left half recursively. | ||
mergesort(v.get_unchecked_mut(..mid), scratch, is_less); | ||
// Sort the right half recursively. | ||
mergesort(v.get_unchecked_mut(mid..), scratch, is_less); | ||
} | ||
|
||
merge::merge(v, scratch, mid, is_less); | ||
} else if len == 2 { | ||
// SAFETY: We checked the len, the pointers we create are valid and don't overlap. | ||
unsafe { | ||
let v_base = v.as_mut_ptr(); | ||
let v_a = v_base; | ||
let v_b = v_base.add(1); | ||
|
||
if is_less(&*v_b, &*v_a) { | ||
ptr::swap_nonoverlapping(v_a, v_b, 1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.