-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Optimize pointer alignment in utf8 validation #61339
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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.
Does this work correctly for the case where
align == usize::max_value()
? See the docs foralign_offset
.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.
It looks like it does not to me. The previous code does, since it just takes the non-aligned path when
align_offset
returnsusize::max_value
.When is it not possible to align the pointer?
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.
One case where alignment is not possible is when running in Miri (which is why
align_offset
is a lang item). I do not know if there are others. It might be a good idea to re-visit that design decision for thealign_offset
API, but until we do IMO we shouldn't land code that relies on a changed contract.Miri is very close to having enough support for ptr-to-int-casting to support this in principle, but the issue is that this will make it much harder to detect code that has alignment problems. By making
align_offset
always returnmax_value
, Miri can ensure to find all possibly misaligned accesses in a single run of the program. (And Miri has found several such issues in libstd and also in rand.) Once we makealign_offset
work like on real hardware, that becomes impossible; there might be requests that are aligned "by chance".How hard would it be to change this code to work with the current contract of
align_offset
? Would that cause run-time performance regressions?Cc @oli-obk, who however is on vacation.
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 suppose if the size of
T
is a big prime and the offset fromptr
to the alignment also has the right value, the smallestn
such that(ptr + n*size_of::<T>()) % align == 0
can be very big.But in this case,
T
isu8
, so that seems unlikely. ;)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 should have read
align_offset
's doc better, I didn't realize it might not be possible to align the offsets. I can manually check foralign != usize::max_value()
here to avoid this. Interestingly, it seems that check will disappear entirely on supported platforms? Compare with and without, which generate the same assembly.This could also be added to a
Alignment
impl, if we wanted to go that route instead.