Skip to content

Commit c299230

Browse files
committed
Optimize vec::from_elem with manual inlining (borrowck 1.85x speedup on libstd)
1 parent e516d23 commit c299230

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/libstd/vec.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
149149
do as_mut_buf(v) |p, _len| {
150150
let mut i: uint = 0u;
151151
while i < n_elts {
152-
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
153-
op(i));
152+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
154153
i += 1u;
155154
}
156155
}
@@ -166,7 +165,20 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
166165
* to the value `t`.
167166
*/
168167
pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] {
169-
from_fn(n_elts, |_i| copy t)
168+
// hack: manually inline from_fn for 2x plus speedup (sadly very important, from_elem is a
169+
// bottleneck in borrowck!)
170+
unsafe {
171+
let mut v = with_capacity(n_elts);
172+
do as_mut_buf(v) |p, _len| {
173+
let mut i = 0u;
174+
while i < n_elts {
175+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
176+
i += 1u;
177+
}
178+
}
179+
raw::set_len(&mut v, n_elts);
180+
v
181+
}
170182
}
171183

172184
/// Creates a new unique vector with the same contents as the slice

0 commit comments

Comments
 (0)