-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Fast path for vec.clear/truncate #64375
Merged
Merged
Changes from all commits
Commits
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
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.
Why isn't this function just implemented as:
?
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.
cc @SimonSapin
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 don’t know, maybe it’s older than
drop_in_place
?One difference however, as hinted by the code comment, is that the explicit loop +
SetLenOnDrop
doesn’t leak other items whenT::drop
panics.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.
Might be.
Yes, I think this leaks as little as it possibly can - only some fields of the
T
for whichT::drop
panics might be leaked with the current implementation.I don't see this guaranteed or documented anywhere, but it is a reasonable thing to do. We don't do this for, e.g.,
Vec<T>::drop
, but observing a partially droppedVec
is hard, while iftruncate
panics, then maybe observing a partially truncatedVec
is easier ?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.
Well yes, it is really easy:
Does
ptr::drop_in_place([T])
stop dropping elements the first time a panic happens ? Or does it continue dropping elements and re-raises the panic at the end?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.
No.
It continues dropping elements, and re-raises the panic at the end, just like for all other aggregates. If a second element panics, then we have a double panic. See here.
So the main difference between this implementation and one using
ptr::drop_in_place
, is that this one stops dropping elements the moment there is a panic, and withptr::drop_in_place
, all elements are always dropped, except at most one if it panics, and if more than one panic the program aborts.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.
@bjorn3 so that example works with the current implementation, but it wouldn't work if we were to use
ptr::drop_in_place
(it would abort due to a double panic).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.
Oh I didn’t realize
drop_in_place
did that. So maybe it’s preferable to use here.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 specific implementation has more instructions for
clear
, because it changes unconditionalstore 0
into a check. Changing it toif len > self.len { return; }
generates the same code foru8
. Drop cases are different of course.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.
Nice catch! Yes, with
if len > self.len { return; }
the assembly forVec::clear
looks much nicer: https://rust.godbolt.org/z/6pSj7K