Skip to content

Commit c552819

Browse files
jldnikomatsakis
authored andcommitted
De-abstract std::sort:qsort3, which uses only the trait-based lt/eq.
quick_sort3 was converted from fn parameters to traits in d9cddde, but was still passing around closures over core::cmp::{eq,lt} internally, and LLVM doesn't and/or can't pick up that they're effectively constant. Reduces time spent to sort a large random ~[uint] by 16% in my testing.
1 parent 206edf6 commit c552819

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/libstd/sort.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ fn quick_sort<T: copy>(compare_func: le<T>, arr: ~[mut T]) {
9595
qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
9696
}
9797

98-
fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
99-
arr: ~[mut T], left: int, right: int) {
98+
fn qsort3<T: copy Ord Eq>(arr: ~[mut T], left: int, right: int) {
10099
if right <= left { return; }
101100
let v: T = arr[right];
102101
let mut i: int = left - 1;
@@ -105,19 +104,19 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
105104
let mut q: int = j;
106105
loop {
107106
i += 1;
108-
while compare_func_lt(&arr[i], &v) { i += 1; }
107+
while arr[i] < v { i += 1; }
109108
j -= 1;
110-
while compare_func_lt(&v, &arr[j]) {
109+
while v < arr[j] {
111110
if j == left { break; }
112111
j -= 1;
113112
}
114113
if i >= j { break; }
115114
arr[i] <-> arr[j];
116-
if compare_func_eq(&arr[i], &v) {
115+
if arr[i] == v {
117116
p += 1;
118117
arr[p] <-> arr[i];
119118
}
120-
if compare_func_eq(&v, &arr[j]) {
119+
if v == arr[j] {
121120
q -= 1;
122121
arr[j] <-> arr[q];
123122
}
@@ -139,8 +138,8 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
139138
i += 1;
140139
if k == 0 { break; }
141140
}
142-
qsort3::<T>(compare_func_lt, compare_func_eq, arr, left, j);
143-
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
141+
qsort3::<T>(arr, left, j);
142+
qsort3::<T>(arr, i, right);
144143
}
145144

146145
/**
@@ -155,7 +154,7 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
155154
*/
156155
fn quick_sort3<T: copy Ord Eq>(arr: ~[mut T]) {
157156
if arr.len() <= 1 { return; }
158-
qsort3(core::cmp::lt, core::cmp::eq, arr, 0, (arr.len() - 1) as int);
157+
qsort3(arr, 0, (arr.len() - 1) as int);
159158
}
160159

161160
#[cfg(test)]

0 commit comments

Comments
 (0)