Skip to content
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

Vec could use insert_all and insert_all_move methods #15435

Closed
pnkfelix opened this issue Jul 4, 2014 · 4 comments
Closed

Vec could use insert_all and insert_all_move methods #15435

pnkfelix opened this issue Jul 4, 2014 · 4 comments
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jul 4, 2014

The Vec type currently provides an push method that allows one to insert a single element at the end of the vector, as well as push_all and push_all_move methods that allow inserting many elements at the end of the vector more efficiently than would happen if the client code called push in a loop (because it avoids repeatedly re-growing the vector).

Vec also offers an insert method that allows inserting a single element somewhere in the innards of the vector.

I believe we could usefully add insert_all and insert_all_move methods that insert a sequence of elements at once. These methods would:

  1. Reserve the necessary space,
  2. Shift over all of the pre-existing elements to make room,
  3. Copy in the new values.

Caveat: we would need to ensure that fail! does not occur between steps 2 and 3.

Much like with push_all/push_all_move, the advantage would be avoiding repeatedly re-growing the vector, the way that calling insert in a loop will do.

(The insert_all and insert_all_move may have to have their own dedicated implementations, rather than being layed atop an iterator-based abstraction the way that push_all/push_all_move are atop Extendable::extend, because of the caveat given above (we cannot call out to arbitrary iterator code during step 3 because we cannot allow failure while the vector is in an intermediate state where it has partially blank innards).

I am filing this mostly as a note because while I was working on #15418, I found a potential need for methods like these to avoid quadratic asymptotic runtimes. But it is probably not a high priority.

@pnkfelix
Copy link
Member Author

pnkfelix commented Jul 4, 2014

Likewise repeated element removal (via pop or remove) is another operation that we might consider optimizing. pop probably does not need it right now since it does not seem like we currently resize the vector when elements are removed (via pop or via remove). But remove still needs to shift elements over, so a variant that removes a range of values could still be potentially useful. (Of course there is still the issue of whether it would return the removed values in their own Vec, or just drop them itself, or if we would need both variants supported in some way.)

I'm largely just noting this here so that I can use the same ticket number in all of the FIXME notes I am adding a number to. :)

@huonw
Copy link
Member

huonw commented Jul 5, 2014

#12884 is similar (although this issue is more general).

@steveklabnik
Copy link
Member

Traige: extend still does the efficient append to the end, but we don't have something that shifts existing elements over.

@steveklabnik
Copy link
Member

Closing in favor of rust-lang/rfcs#1065

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 21, 2023
Derive block attributes from block item tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

3 participants