Skip to content

Commit 3c3d3dd

Browse files
committed
core: rearrange ptr::swap_nonoverlapping_one's cases (no functional changes).
1 parent 5b0ab79 commit 3c3d3dd

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

library/core/src/ptr/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,21 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
473473
#[inline]
474474
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
475475
pub(crate) const unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
476-
// For types smaller than the block optimization below,
477-
// just swap directly to avoid pessimizing codegen.
478-
if mem::size_of::<T>() < 32 {
479-
// SAFETY: the caller must guarantee that `x` and `y` are valid
480-
// for writes, properly aligned, and non-overlapping.
481-
unsafe {
482-
let z = read(x);
483-
copy_nonoverlapping(y, x, 1);
484-
write(y, z);
485-
}
486-
} else {
476+
// Only apply the block optimization in `swap_nonoverlapping_bytes` for types
477+
// at least as large as the block size, to avoid pessimizing codegen.
478+
if mem::size_of::<T>() >= 32 {
487479
// SAFETY: the caller must uphold the safety contract for `swap_nonoverlapping`.
488480
unsafe { swap_nonoverlapping(x, y, 1) };
481+
return;
482+
}
483+
484+
// Direct swapping, for the cases not going through the block optimization.
485+
// SAFETY: the caller must guarantee that `x` and `y` are valid
486+
// for writes, properly aligned, and non-overlapping.
487+
unsafe {
488+
let z = read(x);
489+
copy_nonoverlapping(y, x, 1);
490+
write(y, z);
489491
}
490492
}
491493

0 commit comments

Comments
 (0)