-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Optimize implementations of FromIterator and Extend for Vec #22681
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1410,42 +1410,26 @@ impl<T> ops::DerefMut for Vec<T> { | |
impl<T> FromIterator<T> for Vec<T> { | ||
#[inline] | ||
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Vec<T> { | ||
// Unroll the first iteration, as the vector is going to be | ||
// expanded on this iteration in every case when the iterable is not | ||
// empty, but the loop in extend_desugared() is not going to see the | ||
// vector being full in the few subsequent loop iterations. | ||
// So we get better branch prediction and the possibility to | ||
// construct the vector with initial estimated capacity. | ||
let mut iterator = iterable.into_iter(); | ||
let (lower, _) = iterator.size_hint(); | ||
let mut vector = Vec::with_capacity(lower); | ||
|
||
// This function should be the moral equivalent of: | ||
// | ||
// for item in iterator { | ||
// vector.push(item); | ||
// } | ||
// | ||
// This equivalent crucially runs the iterator precisely once. Below we | ||
// actually in theory run the iterator twice (one without bounds checks | ||
// and one with). To achieve the "moral equivalent", we use the `if` | ||
// statement below to break out early. | ||
// | ||
// If the first loop has terminated, then we have one of two conditions. | ||
// | ||
// 1. The underlying iterator returned `None`. In this case we are | ||
// guaranteed that less than `vector.capacity()` elements have been | ||
// returned, so we break out early. | ||
// 2. The underlying iterator yielded `vector.capacity()` elements and | ||
// has not yielded `None` yet. In this case we run the iterator to | ||
// its end below. | ||
for element in iterator.by_ref().take(vector.capacity()) { | ||
let len = vector.len(); | ||
unsafe { | ||
ptr::write(vector.get_unchecked_mut(len), element); | ||
vector.set_len(len + 1); | ||
} | ||
} | ||
|
||
if vector.len() == vector.capacity() { | ||
for element in iterator { | ||
vector.push(element); | ||
let mut vector = match iterator.next() { | ||
None => return Vec::new(), | ||
Some(element) => { | ||
let (lower, _) = iterator.size_hint(); | ||
let mut vector = Vec::with_capacity(1 + lower); | ||
unsafe { | ||
ptr::write(vector.get_unchecked_mut(0), element); | ||
vector.set_len(1); | ||
} | ||
vector | ||
} | ||
} | ||
}; | ||
vector.extend_desugared(iterator); | ||
vector | ||
} | ||
} | ||
|
@@ -1484,11 +1468,34 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> { | |
impl<T> Extend<T> for Vec<T> { | ||
#[inline] | ||
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) { | ||
let iterator = iterable.into_iter(); | ||
let (lower, _) = iterator.size_hint(); | ||
self.reserve(lower); | ||
for element in iterator { | ||
self.push(element) | ||
self.extend_desugared(iterable.into_iter()) | ||
} | ||
} | ||
|
||
impl<T> Vec<T> { | ||
fn extend_desugared<I: Iterator<Item=T>>(&mut self, mut iterator: I) { | ||
// This function should be the moral equivalent of: | ||
// | ||
// for item in iterator { | ||
// self.push(item); | ||
// } | ||
loop { | ||
match iterator.next() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can, good point. |
||
None => { | ||
break; | ||
} | ||
Some(element) => { | ||
let len = self.len(); | ||
if len == self.capacity() { | ||
let (lower, _) = iterator.size_hint(); | ||
self.reserve(lower + 1); | ||
} | ||
unsafe { | ||
ptr::write(self.get_unchecked_mut(len), element); | ||
self.set_len(len + 1); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hm, does this actually improve performance, over the simpler:
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 avoids a branch that is present in
extend_desugared
. That branch tends to be not taken, but in the first iteration the vector is always expanded.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.
Hm, why it does it avoid that branch? It seems that if the iterator has a non-zero size hint the
with_capacity
will ensure that it isn't taken, and in the case that the iterator has a zero-size hint it seems both with and without this are equally bad off?Basically, I'm asking if this was noticeable in practice.
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 are right, and it was somehow lost on me that
Vec::with_capacity(1)
allocates exactly one element. I should simplify the code to your suggestion; the performance difference will likely be negligible.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.
With the suggested change,
vec::bench_from_iter_0000
regressesfrom 29 ns/iter (+/- 2) to 87 ns/iter (+/- 48) in my testing
(rebased against commit 3dbfa74), the other benchmarks seemingly unaffected. I wonder if it can be considered an edge case.