Skip to content

Commit 7e12e67

Browse files
committed
Optimize Slice::reverse
This makes the completely safe implementation of fannkuchredux perform the same as C++. Yay, Rust.
1 parent fc3b638 commit 7e12e67

File tree

4 files changed

+7
-197
lines changed

4 files changed

+7
-197
lines changed

src/etc/licenseck.py

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"libsync/mpsc_intrusive.rs", # BSD
4545
"test/bench/shootout-binarytrees.rs", # BSD
4646
"test/bench/shootout-fannkuch-redux.rs", # BSD
47-
"test/bench/shootout-fannkuch-redux-safe.rs", # BSD
4847
"test/bench/shootout-k-nucleotide.rs", # BSD
4948
"test/bench/shootout-mandelbrot.rs", # BSD
5049
"test/bench/shootout-meteor.rs", # BSD

src/libcore/slice.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,12 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] {
806806
let mut i: uint = 0;
807807
let ln = self.len();
808808
while i < ln / 2 {
809-
self.swap(i, ln - i - 1);
809+
// Unsafe swap to avoid the bounds check in safe swap.
810+
unsafe {
811+
let pa: *mut T = self.unsafe_mut_ref(i);
812+
let pb: *mut T = self.unsafe_mut_ref(ln - i - 1);
813+
ptr::swap(pa, pb);
814+
}
810815
i += 1;
811816
}
812817
}

src/test/bench/shootout-fannkuch-redux-safe.rs

-188
This file was deleted.

src/test/bench/shootout-fannkuch-redux.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ impl Perm {
125125

126126

127127
fn reverse(tperm: &mut [i32], mut k: uint) {
128-
let p = tperm.as_mut_ptr();
129-
130-
unsafe {
131-
for off in range(0, k as int / 2) {
132-
std::ptr::swap(p.offset(off), p.offset(k as int - 1 - off));
133-
}
134-
}
128+
tperm.mut_slice_to(k).reverse()
135129
}
136130

137131
fn work(mut perm: Perm, n: uint, max: uint) -> (i32, i32) {

0 commit comments

Comments
 (0)