about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-06-01 00:55:19 -0400
committerCorey Richardson <corey@octayn.net>2013-06-01 00:55:19 -0400
commitc299230f3d7a43a4307ce317aa41b7e8f361a2e6 (patch)
tree8a92d3c8089dfef9194594a7e94084fffd5e3963 /src/libstd
parente516d2333fa38ce29f1e6e119beb4d5711cf8fd5 (diff)
downloadrust-c299230f3d7a43a4307ce317aa41b7e8f361a2e6.tar.gz
rust-c299230f3d7a43a4307ce317aa41b7e8f361a2e6.zip
Optimize vec::from_elem with manual inlining (borrowck 1.85x speedup on libstd)
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/vec.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 795c3cdb405..8eedb70b3a6 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -149,8 +149,7 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
         do as_mut_buf(v) |p, _len| {
             let mut i: uint = 0u;
             while i < n_elts {
-                intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
-                                          op(i));
+                intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
                 i += 1u;
             }
         }
@@ -166,7 +165,20 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
  * to the value `t`.
  */
 pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] {
-    from_fn(n_elts, |_i| copy t)
+    // hack: manually inline from_fn for 2x plus speedup (sadly very important, from_elem is a
+    // bottleneck in borrowck!)
+    unsafe {
+        let mut v = with_capacity(n_elts);
+        do as_mut_buf(v) |p, _len| {
+            let mut i = 0u;
+            while i < n_elts {
+                intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
+                i += 1u;
+            }
+        }
+        raw::set_len(&mut v, n_elts);
+        v
+    }
 }
 
 /// Creates a new unique vector with the same contents as the slice