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

Change VecDeque::new() to not use any allocation #1173

Closed
bluss opened this issue Jun 24, 2015 · 13 comments
Closed

Change VecDeque::new() to not use any allocation #1173

bluss opened this issue Jun 24, 2015 · 13 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@bluss
Copy link
Member

bluss commented Jun 24, 2015

Allocation free empty collections are very useful, for example for algorithms where a large portion of its cases don't need to store any elements.

@Gankra
Copy link
Contributor

Gankra commented Jun 24, 2015

👍 as long as it can be done without significantly affecting perf.

@ticki
Copy link
Contributor

ticki commented Dec 6, 2015

Maybe new_empty() or new_no_alloc()?

@bluss
Copy link
Member Author

bluss commented Dec 6, 2015

@ticki I don't think it needs a new method for it. Main issue is that I don't see an easy way to make a no allocation VecDeque into a valid state that we can handle without more complicated conditionals to check capacity. I didn't spend too long time trying to implement it, though.

@ghost
Copy link

ghost commented Dec 16, 2015

👎 I'd rather not. This violates RAII, IMO, and you can just use Option<VecDeque<T>> if not allocating until the last moment is necessary.

Changing my mind because I didn't know about the behaviour with Vec::new.

@bluss
Copy link
Member Author

bluss commented Dec 16, 2015

How does it violate RAII? It would only bring VecDeque into the behavior that Vec already has. Vec::new() does not allocate.

@huonw
Copy link
Member

huonw commented Dec 16, 2015

@Undeterminant, hm, I'm missing the connection between not allocating and RAII, e.g. Vec is extremely RAII-y but Vec::new() doesn't allocate. Using Option has non-trivial overhead (mainly for the programmer, but also at runtime), so that doesn't seem like a particularly nice solution.

@ranma42
Copy link
Contributor

ranma42 commented Dec 16, 2015

It looks like most methods go though the computation of len()... has anybody already tried using head and len instead of head and tail to evaluate if the code worsen (in readability and/or performance)?

@ghost
Copy link

ghost commented Dec 16, 2015

Now that you mention it, you're right. I was under the assumption that all of these containers allocated on creation.

@Gankra
Copy link
Contributor

Gankra commented Dec 17, 2015

This is completely out of cache, but I recall that head + len was indeed an inferior representation. Happy to be wrong, 'course.

@Gankra
Copy link
Contributor

Gankra commented Dec 17, 2015

Note that rust-lang/rust#30426 "regresses" BTreeMap to behave like VecDeque.

@bluss
Copy link
Member Author

bluss commented Dec 17, 2015

I don't think that's correct? In the present code, BtreeMap::new calls Node::make_leaf_root(6), which allocates.

@Gankra
Copy link
Contributor

Gankra commented Dec 17, 2015

@bluss Ah, I was just going off the PR's description. I've long since forgot such details :)

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Aug 29, 2016
@Centril
Copy link
Contributor

Centril commented Feb 10, 2020

Closing in favor of rust-lang/rust#68990.

@Centril Centril closed this as completed Feb 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

7 participants