From 7e12e67936dd2ad12e529278344dab369ccb75a0 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 4 Sep 2014 19:54:41 -0700 Subject: Optimize Slice::reverse This makes the completely safe implementation of fannkuchredux perform the same as C++. Yay, Rust. --- src/libcore/slice.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index c3c18e36617..cc2b01e3bb5 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -806,7 +806,12 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { let mut i: uint = 0; let ln = self.len(); while i < ln / 2 { - self.swap(i, ln - i - 1); + // Unsafe swap to avoid the bounds check in safe swap. + unsafe { + let pa: *mut T = self.unsafe_mut_ref(i); + let pb: *mut T = self.unsafe_mut_ref(ln - i - 1); + ptr::swap(pa, pb); + } i += 1; } } -- cgit 1.4.1-3-g733a5