Skip to content

Optimize vec::from_elem with manual inlining #6876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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
Expand Down