Skip to content

Remove vector methods that enforce uneccessary allocations or copies #12456

Closed
@Kimundi

Description

@Kimundi

~[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 as map, 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 calling move_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

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions