-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac93120
Add extend_chain bench
Marwes cd551b0
More chain in extend benchmark
9b0cdeb
Use a more complicated chain in benchmarks
ddabd76
perf: Use `for_each` in `Vec::extend`
Marwes 3cfa3aa
Use try_fold in Vec::extend
824151e
No by_ref
a092fe1
Avoid a closure in Vec::extend_desugared
e41f55e
Massage extend_desugared to reduce the compiletime impact
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 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
This file contains 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
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.
This still has the issue of trying to reserve before advancing the iterator. taking an element and then checking the hint and capacity is strictly better because the hint will be more accurate and if you take a
None
you can skip the hint calculation and reserve attempt.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.
The benefit is that it avoids calling
next
at all. While the hint should be more accurate by callingnext
it would also be even more accurate if we callednext
twice so I don't quite buy that argument. Callingnext
once would give a fast path for the empty iterator at least which I find a more convincing argument, however it is only a benefit ifsize_hint(); reserve(0)
is slow enough to warrant this fast path.Pushed another variant which does
next
first which should optimize better for the empty iterator. Haven't got time to benchmark it yet.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.
I was more concerned about doing an suboptimal resize if we got an incorrect lower bound on the first attempt, but you're right, if it's just 0 it hopefully would be cheap enough and the next reserve will do it properly.
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.
Have gone a bit back and forth but I think the of reserving eagerly has a better tradeoff.
On
size_hint == 0
it does call reserve unnecessarily but it is only one more branch in an already fast case. Ifsize_hint > 0
then most of the time we will reserve either way and most of the time with the same value (size_hint_minus_removed_value + 1
vssize_hint
).The only case where I'd expect it to be faster to call next first is if
size_hint
is expensive, despite the iterator being empty andnext
being faster to call thansize_hint
which seems unlikely (happy to be corrected however!).