about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Bergdoll <lukas.bergdoll@gmail.com>2024-09-04 19:53:56 +0200
committerLukas Bergdoll <lukas.bergdoll@gmail.com>2024-09-04 19:53:56 +0200
commita0e4303ba2ed087a85e0218a4049d8cc73d7f404 (patch)
tree282e452223581c2003158140bf8a12ff95826a09
parentadb0e27acd065354b6635c3c5a66dcbbc2e9d9c5 (diff)
downloadrust-a0e4303ba2ed087a85e0218a4049d8cc73d7f404.tar.gz
rust-a0e4303ba2ed087a85e0218a4049d8cc73d7f404.zip
Select tiny sorts for 16-bit platforms
Also skips stack alloc in stable sort if 16-bit target platform.
-rw-r--r--library/core/src/slice/sort/stable/mod.rs31
-rw-r--r--library/core/src/slice/sort/unstable/mod.rs2
2 files changed, 20 insertions, 13 deletions
diff --git a/library/core/src/slice/sort/stable/mod.rs b/library/core/src/slice/sort/stable/mod.rs
index 3472401c4dc..00eb3785e0f 100644
--- a/library/core/src/slice/sort/stable/mod.rs
+++ b/library/core/src/slice/sort/stable/mod.rs
@@ -40,20 +40,27 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
     }
 
     cfg_if! {
-        if #[cfg(feature = "optimize_for_size")] {
+        if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
             let alloc_len = len / 2;
 
-            // For small inputs 4KiB of stack storage suffices, which allows us to avoid
-            // calling the (de-)allocator. Benchmarks showed this was quite beneficial.
-            let mut stack_buf = AlignedStorage::<T, 4096>::new();
-            let stack_scratch = stack_buf.as_uninit_slice_mut();
-            let mut heap_buf;
-            let scratch = if stack_scratch.len() >= alloc_len {
-                stack_scratch
-            } else {
-                heap_buf = BufT::with_capacity(alloc_len);
-                heap_buf.as_uninit_slice_mut()
-            };
+            cfg_if! {
+                if #[cfg(target_pointer_width = "16")] {
+                    let heap_buf = BufT::with_capacity(alloc_len);
+                    let scratch = heap_buf.as_uninit_slice_mut();
+                } else {
+                    // For small inputs 4KiB of stack storage suffices, which allows us to avoid
+                    // calling the (de-)allocator. Benchmarks showed this was quite beneficial.
+                    let mut stack_buf = AlignedStorage::<T, 4096>::new();
+                    let stack_scratch = stack_buf.as_uninit_slice_mut();
+                    let mut heap_buf;
+                    let scratch = if stack_scratch.len() >= alloc_len {
+                        stack_scratch
+                    } else {
+                        heap_buf = BufT::with_capacity(alloc_len);
+                        heap_buf.as_uninit_slice_mut()
+                    };
+                }
+            }
 
             tiny::mergesort(v, scratch, is_less);
         } else {
diff --git a/library/core/src/slice/sort/unstable/mod.rs b/library/core/src/slice/sort/unstable/mod.rs
index 953c27ab6f4..8bbd85443d4 100644
--- a/library/core/src/slice/sort/unstable/mod.rs
+++ b/library/core/src/slice/sort/unstable/mod.rs
@@ -31,7 +31,7 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
     }
 
     cfg_if! {
-        if #[cfg(feature = "optimize_for_size")] {
+        if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
             heapsort::heapsort(v, is_less);
         } else {
             // More advanced sorting methods than insertion sort are faster if called in