Skip to content

Commit 14b8e67

Browse files
committed
[EXPERIMENT] Double-check that the load-store approach in 94412 wasn't a problem
Mark suggests that it wasn't, but I'll make this just to check that copy_nonoverlapping isn't better.
1 parent f0c4da4 commit 14b8e67

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

library/core/src/mem/mod.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -740,26 +740,19 @@ pub const fn swap<T>(x: &mut T, y: &mut T) {
740740
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
741741
#[inline]
742742
pub(crate) const fn swap_simple<T>(x: &mut T, y: &mut T) {
743-
// We arrange for this to typically be called with small types,
744-
// so this reads-and-writes approach is actually better than using
745-
// copy_nonoverlapping as it easily puts things in LLVM registers
746-
// directly and doesn't end up inlining allocas.
747-
// And LLVM actually optimizes it to 3×memcpy if called with
748-
// a type larger than it's willing to keep in a register.
749-
// Having typed reads and writes in MIR here is also good as
750-
// it lets MIRI and CTFE understand them better, including things
751-
// like enforcing type validity for them.
743+
// Make sure the same operations are done on the two sides.
752744
// Importantly, read+copy_nonoverlapping+write introduces confusing
753745
// asymmetry to the behaviour where one value went through read+write
754746
// whereas the other was copied over by the intrinsic (see #94371).
755747

756748
// SAFETY: exclusive references are always valid to read/write,
757749
// including being aligned, and nothing here panics so it's drop-safe.
758750
unsafe {
759-
let a = ptr::read(x);
760-
let b = ptr::read(y);
761-
ptr::write(x, b);
762-
ptr::write(y, a);
751+
let mut z = MaybeUninit::<T>::uninit();
752+
let z = z.as_mut_ptr();
753+
ptr::copy_nonoverlapping(x, z, 1);
754+
ptr::copy_nonoverlapping(y, x, 1);
755+
ptr::copy_nonoverlapping(z, y, 1);
763756
}
764757
}
765758

0 commit comments

Comments
 (0)