-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optimize_for_size
variants for stable and unstable sort as well as select_nth_unstable
#129587
Merged
bors
merged 9 commits into
rust-lang:master
from
Voultapher:opt-for-size-variants-of-sort-impls
Sep 24, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
13d7b54
Add binary-size optimized variants for stable and unstable sort as we…
Voultapher 89c9a29
Reduce code duplication by moving partition_lomuto_branchless_simple …
Voultapher 8756ba5
Convert cfg blocks to cfg_if
Voultapher 7815d77
Use last swap optimization in bubblesort
Voultapher 00eca77
Use simpler branchy swap logic in tiny merge sort
Voultapher f2d4198
Drop bubble_sort
Voultapher adb0e27
Shrink heapsort further by combining sift_down loops
Voultapher a0e4303
Select tiny sorts for 16-bit platforms
Voultapher 5439198
Use non-overlapping swap for inner heapsort loop
Voultapher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This likely needs to be
#[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))]
, see #130818There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.