summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorJed Davis <jld@panix.com>2012-08-27 21:17:42 -0700
committerNiko Matsakis <niko@alum.mit.edu>2012-08-28 06:37:06 -0700
commitc5528198abd91c19da22ccf370b84ce1d71fc3c2 (patch)
tree7a06c1b3119c49eec430ef8e1257f0c9c49a4fcc /src/libstd
parent206edf66c92e12f1d98b56ea48a340e7e86d8552 (diff)
downloadrust-c5528198abd91c19da22ccf370b84ce1d71fc3c2.tar.gz
rust-c5528198abd91c19da22ccf370b84ce1d71fc3c2.zip
De-abstract std::sort:qsort3, which uses only the trait-based lt/eq.
quick_sort3 was converted from fn parameters to traits in d9cdddeb, 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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sort.rs17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs
index 256ee4ecd73..52d25f53940 100644
--- a/src/libstd/sort.rs
+++ b/src/libstd/sort.rs
@@ -95,8 +95,7 @@ fn quick_sort<T: copy>(compare_func: le<T>, arr: ~[mut T]) {
     qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
 }
 
-fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
-                  arr: ~[mut T], left: int, right: int) {
+fn qsort3<T: copy Ord Eq>(arr: ~[mut T], left: int, right: int) {
     if right <= left { return; }
     let v: T = arr[right];
     let mut i: int = left - 1;
@@ -105,19 +104,19 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
     let mut q: int = j;
     loop {
         i += 1;
-        while compare_func_lt(&arr[i], &v) { i += 1; }
+        while arr[i] < v { i += 1; }
         j -= 1;
-        while compare_func_lt(&v, &arr[j]) {
+        while v < arr[j] {
             if j == left { break; }
             j -= 1;
         }
         if i >= j { break; }
         arr[i] <-> arr[j];
-        if compare_func_eq(&arr[i], &v) {
+        if arr[i] == v {
             p += 1;
             arr[p] <-> arr[i];
         }
-        if compare_func_eq(&v, &arr[j]) {
+        if v == arr[j] {
             q -= 1;
             arr[j] <-> arr[q];
         }
@@ -139,8 +138,8 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
         i += 1;
         if k == 0 { break; }
     }
-    qsort3::<T>(compare_func_lt, compare_func_eq, arr, left, j);
-    qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
+    qsort3::<T>(arr, left, j);
+    qsort3::<T>(arr, i, right);
 }
 
 /**
@@ -155,7 +154,7 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
  */
 fn quick_sort3<T: copy Ord Eq>(arr: ~[mut T]) {
     if arr.len() <= 1 { return; }
-    qsort3(core::cmp::lt, core::cmp::eq, arr, 0, (arr.len() - 1) as int);
+    qsort3(arr, 0, (arr.len() - 1) as int);
 }
 
 #[cfg(test)]