-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Match VecDeque::extend
to Vec::extend_desugared
#66341
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
1824aea
to
164d1a2
Compare
Ping from Triage: Any update? @hellow554 @Kimundi? |
r? @SimonSapin |
Ping from triage @SimonSapin any updates on this? Thanks. |
Sorry. I’m not familiar enough with internals of r? @Dylan-DPC |
r? @dtolnay |
r? @Amanieu |
@bors r+ |
📌 Commit 164d1a2 has been approved by |
⌛ Testing commit 164d1a2 with merge 6c6235d30d605fa38b55d059a8c57b3e1149885b... |
Match `VecDeque::extend` to `Vec::extend_desugared` Currently, `VecDeque::extend` [does not reserve at all](rust-lang#65069 (comment)). This implementation still runs a check every iteration of the loop, but should reallocate at most once for the common cases where the `size_hint` lower bound is exact. Further optimizations in the future could improve this for some common cases, but given the complexity of the `Vec::extend` implementation it's not immediately clear that this would be worthwhile.
@bors retry rolled up. |
Rollup of 6 pull requests Successful merges: - #66341 (Match `VecDeque::extend` to `Vec::extend_desugared`) - #67243 (LinkedList: drop remaining items when drop panics) - #67247 (Don't suggest wrong snippet in closure) - #67250 (Remove the `DelimSpan` from `NamedMatch::MatchedSeq`.) - #67251 (Require `allow_internal_unstable` for stable min_const_fn using unsta…) - #67269 (parser: recover on `&'lifetime mut? $pat`.) Failed merges: r? @ghost
// } | ||
let mut iter = iter.into_iter(); | ||
while let Some(element) = iter.next() { | ||
if self.len() == self.capacity() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation still runs a check every iteration of the loop
Wouldn't it be possible to move this out of the loop?
let capacity = self.capacity() - self.len();
let (lower, _) = iter.size_hint();
if capacity < lower {
self.reserve(...);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do it for trusted length iterators specifically, but otherwise because it's a lower bound you have to check every time. At some point it could be good to specialize, though
Currently,
VecDeque::extend
does not reserve at all. This implementation still runs a check every iteration of the loop, but should reallocate at most once for the common cases where thesize_hint
lower bound is exact. Further optimizations in the future could improve this for some common cases, but given the complexity of theVec::extend
implementation it's not immediately clear that this would be worthwhile.