Skip to content

Commit d973b35

Browse files
committed
Auto merge of rust-lang#94342 - ibraheemdev:swap-regression, r=Dylan-DPC
Revert implementation of `slice::swap` Due to the perf regressions noticed here, possible due to inlining? rust-lang#88540 (comment) r? `@kennytm`
2 parents d3ad51b + 072d35d commit d973b35

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

library/core/src/slice/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,17 @@ impl<T> [T] {
587587
#[inline]
588588
#[track_caller]
589589
pub const fn swap(&mut self, a: usize, b: usize) {
590-
let _ = &self[a];
591-
let _ = &self[b];
592-
593-
// SAFETY: we just checked that both `a` and `b` are in bounds
594-
unsafe { self.swap_unchecked(a, b) }
590+
// FIXME: use swap_unchecked here (https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343)
591+
// Can't take two mutable loans from one vector, so instead use raw pointers.
592+
let pa = ptr::addr_of_mut!(self[a]);
593+
let pb = ptr::addr_of_mut!(self[b]);
594+
// SAFETY: `pa` and `pb` have been created from safe mutable references and refer
595+
// to elements in the slice and therefore are guaranteed to be valid and aligned.
596+
// Note that accessing the elements behind `a` and `b` is checked and will
597+
// panic when out of bounds.
598+
unsafe {
599+
ptr::swap(pa, pb);
600+
}
595601
}
596602

597603
/// Swaps two elements in the slice, without doing bounds checking.

0 commit comments

Comments
 (0)