-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
perf: Use for_each
in Vec::extend
#68046
Closed
Closed
Commits on Feb 6, 2020
-
Configuration menu - View commit details
-
Copy full SHA for ac93120 - Browse repository at this point
Copy the full SHA ac93120View commit details -
More chain in extend benchmark
Markus Westerlind committedFeb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for cd551b0 - Browse repository at this point
Copy the full SHA cd551b0View commit details -
Use a more complicated chain in benchmarks
Markus Westerlind committedFeb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for 9b0cdeb - Browse repository at this point
Copy the full SHA 9b0cdebView commit details -
perf: Use
for_each
inVec::extend
`for_each` are specialized for iterators such as `chain` allowing for faster iteration than a normal `for/while` loop. Note that since this only checks `size_hint` once at the start it may end up needing to call `reserve` more in the case that `size_hint` returns a larger and more accurate lower bound during iteration. This could maybe be alleviated with an implementation closure like the current one but the extra complexity will likely end up harming the normal case of an accurate or 0 (think `filter`) lower bound. ```rust while let Some(element) = iterator.next() { let (lower, _) = iterator.size_hint(); self.reserve(lower.saturating_add(1)); unsafe { let len = self.len(); ptr::write(self.get_unchecked_mut(len), element); // NB can't overflow since we would have had to alloc the address space self.set_len(len + 1); } iterator.by_ref().take(self.capacity()).for_each(|element| { unsafe { let len = self.len(); ptr::write(self.get_unchecked_mut(len), element); // NB can't overflow since we would have had to alloc the address space self.set_len(len + 1); } }); } // OR let (lower, _) = iterator.size_hint(); self.reserve(lower); loop { let result = iterator.by_ref().try_for_each(|element| { if self.len() == self.capacity() { return Err(element); } unsafe { let len = self.len(); ptr::write(self.get_unchecked_mut(len), element); // NB can't overflow since we would have had to alloc the address space self.set_len(len + 1); } Ok(()) }); match result { Ok(()) => break, Err(element) => { let (lower, _) = iterator.size_hint(); self.reserve(lower.saturating_add(1)); self.push(element); } } } ``` Closes rust-lang#63340
Configuration menu - View commit details
-
Copy full SHA for ddabd76 - Browse repository at this point
Copy the full SHA ddabd76View commit details -
Should put less stress on LLVM since there are less closures passed around and lets us refine how much we reserve with `size_hint` if the first guess is too low.
Markus Westerlind committedFeb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for 3cfa3aa - Browse repository at this point
Copy the full SHA 3cfa3aaView commit details -
Markus Westerlind committed
Feb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for 824151e - Browse repository at this point
Copy the full SHA 824151eView commit details -
Avoid a closure in Vec::extend_desugared
Markus Westerlind committedFeb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for a092fe1 - Browse repository at this point
Copy the full SHA a092fe1View commit details -
Massage extend_desugared to reduce the compiletime impact
Markus Westerlind committedFeb 6, 2020 Configuration menu - View commit details
-
Copy full SHA for e41f55e - Browse repository at this point
Copy the full SHA e41f55eView commit details
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.