about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <eddyb@lyken.rs>2021-03-11 14:39:37 +0200
committerEduard-Mihai Burtescu <eddyb@lyken.rs>2021-04-04 22:26:00 +0300
commit3c3d3ddde967f66938966d6d557a3a4fe4d267ff (patch)
tree60cc79cefd29aae5f7c8bb02ad0c97567e49f2c3
parent5b0ab79116ba3231f447bc7a67c16ce93ecca0a3 (diff)
downloadrust-3c3d3ddde967f66938966d6d557a3a4fe4d267ff.tar.gz
rust-3c3d3ddde967f66938966d6d557a3a4fe4d267ff.zip
core: rearrange `ptr::swap_nonoverlapping_one`'s cases (no functional changes).
-rw-r--r--library/core/src/ptr/mod.rs24
1 files changed, 13 insertions, 11 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index 6412bd41a8c..15aa4769697 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -473,19 +473,21 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
 #[inline]
 #[rustc_const_unstable(feature = "const_swap", issue = "83163")]
 pub(crate) const unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
-    // For types smaller than the block optimization below,
-    // just swap directly to avoid pessimizing codegen.
-    if mem::size_of::<T>() < 32 {
-        // SAFETY: the caller must guarantee that `x` and `y` are valid
-        // for writes, properly aligned, and non-overlapping.
-        unsafe {
-            let z = read(x);
-            copy_nonoverlapping(y, x, 1);
-            write(y, z);
-        }
-    } else {
+    // Only apply the block optimization in `swap_nonoverlapping_bytes` for types
+    // at least as large as the block size, to avoid pessimizing codegen.
+    if mem::size_of::<T>() >= 32 {
         // SAFETY: the caller must uphold the safety contract for `swap_nonoverlapping`.
         unsafe { swap_nonoverlapping(x, y, 1) };
+        return;
+    }
+
+    // Direct swapping, for the cases not going through the block optimization.
+    // SAFETY: the caller must guarantee that `x` and `y` are valid
+    // for writes, properly aligned, and non-overlapping.
+    unsafe {
+        let z = read(x);
+        copy_nonoverlapping(y, x, 1);
+        write(y, z);
     }
 }