Skip to content

Commit 1265244

Browse files
committed
make ptr::rotate smaller when using optimize_for_size
code to reproduce https://github.com/folkertdev/optimize_for_size-slice-rotate In the example the size of `.text` goes down from 1624 to 276 bytes.
1 parent 1aaf0a9 commit 1265244

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

core/src/slice/rotate.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mut mid: *mut T, mut right: usize)
7171
if (right == 0) || (left == 0) {
7272
return;
7373
}
74-
if (left + right < 24) || (mem::size_of::<T>() > mem::size_of::<[usize; 4]>()) {
74+
if !cfg!(feature = "optimize_for_size")
75+
&& ((left + right < 24) || (mem::size_of::<T>() > mem::size_of::<[usize; 4]>()))
76+
{
7577
// Algorithm 1
7678
// Microbenchmarks indicate that the average performance for random shifts is better all
7779
// the way until about `left + right == 32`, but the worst case performance breaks even
@@ -158,7 +160,9 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mut mid: *mut T, mut right: usize)
158160
}
159161
return;
160162
// `T` is not a zero-sized type, so it's okay to divide by its size.
161-
} else if cmp::min(left, right) <= mem::size_of::<BufType>() / mem::size_of::<T>() {
163+
} else if !cfg!(feature = "optimize_for_size")
164+
&& cmp::min(left, right) <= mem::size_of::<BufType>() / mem::size_of::<T>()
165+
{
162166
// Algorithm 2
163167
// The `[T; 0]` here is to ensure this is appropriately aligned for T
164168
let mut rawarray = MaybeUninit::<(BufType, [T; 0])>::uninit();

0 commit comments

Comments
 (0)