Skip to content

Commit a0e4303

Browse files
committed
Select tiny sorts for 16-bit platforms
Also skips stack alloc in stable sort if 16-bit target platform.
1 parent adb0e27 commit a0e4303

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

library/core/src/slice/sort/stable/mod.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,27 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
4040
}
4141

4242
cfg_if! {
43-
if #[cfg(feature = "optimize_for_size")] {
43+
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
4444
let alloc_len = len / 2;
4545

46-
// For small inputs 4KiB of stack storage suffices, which allows us to avoid
47-
// calling the (de-)allocator. Benchmarks showed this was quite beneficial.
48-
let mut stack_buf = AlignedStorage::<T, 4096>::new();
49-
let stack_scratch = stack_buf.as_uninit_slice_mut();
50-
let mut heap_buf;
51-
let scratch = if stack_scratch.len() >= alloc_len {
52-
stack_scratch
53-
} else {
54-
heap_buf = BufT::with_capacity(alloc_len);
55-
heap_buf.as_uninit_slice_mut()
56-
};
46+
cfg_if! {
47+
if #[cfg(target_pointer_width = "16")] {
48+
let heap_buf = BufT::with_capacity(alloc_len);
49+
let scratch = heap_buf.as_uninit_slice_mut();
50+
} else {
51+
// For small inputs 4KiB of stack storage suffices, which allows us to avoid
52+
// calling the (de-)allocator. Benchmarks showed this was quite beneficial.
53+
let mut stack_buf = AlignedStorage::<T, 4096>::new();
54+
let stack_scratch = stack_buf.as_uninit_slice_mut();
55+
let mut heap_buf;
56+
let scratch = if stack_scratch.len() >= alloc_len {
57+
stack_scratch
58+
} else {
59+
heap_buf = BufT::with_capacity(alloc_len);
60+
heap_buf.as_uninit_slice_mut()
61+
};
62+
}
63+
}
5764

5865
tiny::mergesort(v, scratch, is_less);
5966
} else {

library/core/src/slice/sort/unstable/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
3131
}
3232

3333
cfg_if! {
34-
if #[cfg(feature = "optimize_for_size")] {
34+
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
3535
heapsort::heapsort(v, is_less);
3636
} else {
3737
// More advanced sorting methods than insertion sort are faster if called in

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy