-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Vectorized MemoryExtensions.CommonPrefixLength #68210
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
tannergooding
merged 4 commits into
dotnet:main
from
gfoidl:commonprefixlength_vectorization
Apr 27, 2022
Merged
Changes from all 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
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 |
|---|---|---|
|
|
@@ -2033,6 +2033,29 @@ public static int CommonPrefixLength<T>(this Span<T> span, ReadOnlySpan<T> other | |
| /// <returns>The length of the common prefix shared by the two spans. If there's no shared prefix, 0 is returned.</returns> | ||
| public static int CommonPrefixLength<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) | ||
| { | ||
| if (RuntimeHelpers.IsBitwiseEquatable<T>()) | ||
| { | ||
| nuint length = Math.Min((nuint)(uint)span.Length, (nuint)(uint)other.Length); | ||
| nuint size = (uint)Unsafe.SizeOf<T>(); | ||
| nuint index = SpanHelpers.CommonPrefixLength( | ||
| ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
| ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(other)), | ||
| length * size); | ||
|
|
||
| // A byte-wise comparison in CommonPrefixLength can be used for multi-byte types, | ||
| // that are bitwise-equatable, too. In order to get the correct index in terms of type T | ||
| // of the first mismatch, integer division by the size of T is used. | ||
| // | ||
| // Example for short: | ||
| // index (byte-based): b-1, b, b+1, b+2, b+3 | ||
| // index (short-based): s-1, s, s+1 | ||
| // byte sequence 1: { ..., [0x42, 0x43], [0x37, 0x38], ... } | ||
| // byte sequence 2: { ..., [0x42, 0x43], [0x37, 0xAB], ... } | ||
| // So the mismatch is a byte-index b+3, which gives integer divided by the size of short: | ||
| // 3 / 2 = 1, thus the expected index short-based. | ||
| return (int)(index / size); | ||
|
Member
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. The use of byte and then dividing to get rid of partials is clever... but deserves a big comment :)
Member
Author
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. Too verbose now? |
||
| } | ||
|
|
||
| // Shrink one of the spans if necessary to ensure they're both the same length. We can then iterate until | ||
| // the Length of one of them and at least have bounds checks removed from that one. | ||
| SliceLongerSpanToMatchShorterLength(ref span, ref other); | ||
|
|
||
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
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.
I was going to suggest
nuint.Minbut then I remembered thatIntPtrandUIntPtrstill have them explicitly implemented until the work around dotnet/csharplang#6031 goes in.