diff options
| author | bors <bors@rust-lang.org> | 2022-02-25 23:47:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-02-25 23:47:00 +0000 |
| commit | d973b358c6dc5261f9c13e4ef7f9ab58586d628e (patch) | |
| tree | 838fb7bce373461c9f0d13333f3ba3672fc85af9 | |
| parent | d3ad51b48f83329fac0cd8a9f1253f3146613c1c (diff) | |
| parent | 072d35dc2db394589a99ac35854ebe1ba584b0a2 (diff) | |
| download | rust-d973b358c6dc5261f9c13e4ef7f9ab58586d628e.tar.gz rust-d973b358c6dc5261f9c13e4ef7f9ab58586d628e.zip | |
Auto merge of #94342 - ibraheemdev:swap-regression, r=Dylan-DPC
Revert implementation of `slice::swap` Due to the perf regressions noticed here, possible due to inlining? https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343 r? `@kennytm`
| -rw-r--r-- | library/core/src/slice/mod.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index cd38c3a7547..7d89fc7eced 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -587,11 +587,17 @@ impl<T> [T] { #[inline] #[track_caller] pub const fn swap(&mut self, a: usize, b: usize) { - let _ = &self[a]; - let _ = &self[b]; - - // SAFETY: we just checked that both `a` and `b` are in bounds - unsafe { self.swap_unchecked(a, b) } + // FIXME: use swap_unchecked here (https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343) + // Can't take two mutable loans from one vector, so instead use raw pointers. + let pa = ptr::addr_of_mut!(self[a]); + let pb = ptr::addr_of_mut!(self[b]); + // SAFETY: `pa` and `pb` have been created from safe mutable references and refer + // to elements in the slice and therefore are guaranteed to be valid and aligned. + // Note that accessing the elements behind `a` and `b` is checked and will + // panic when out of bounds. + unsafe { + ptr::swap(pa, pb); + } } /// Swaps two elements in the slice, without doing bounds checking. |
