-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.
Description
~[T]
has a few legacy methods that enforce unnecessary copies or allocations:
map<T, U>(&[T], f: |&T| -> U) -> ~[U]
: requires the user to copy/clone through the &T references, and always allocates a new~[U]
.flat_map<T, U>(&[T], f: |&T| -> ~[U]) -> ~[U]
: same asmap
, but also requires allocation of temporary~[U]
vectors.push_all_move(&mut ~[T], ~[T])
: Requires a temporary~[T]
vector just to move all values in at once.
Today, all these functions can be more generally and precisely expressed with iterators:
vec.map(...) => vec.iter().map(...).collect()
, with the options of callingmove_iter()
instead, and the ability to collect into a arbitrary other type.vec.flat_map(...) => vec.iter().flat_map(...).collect()
, same as above, but depending on iterator used internally no need for allocations and temporary vectors.vec.push_all_move(v) => vec.extend(&mut v.move_iter())
, instead of passing a owned vector, passing a iterator instead that produces the values.
So, I'm proposing that these functions get removed and replaced with the alternatives above. The only problem is that its getting more verbose, but there are a few ways how that can get reduced: Iterable
trait, shorten of names like move_iter
, etc.
(Everything said here also applies to Vec<T>
due the currently in-progress transition to it.)
Metadata
Metadata
Assignees
Labels
I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.