-
Notifications
You must be signed in to change notification settings - Fork 4.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
Audit MemoryExtensions.IndexOf variants #75754
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -461,12 +461,25 @@ ref Unsafe.As<T, long>(ref MemoryMarshal.GetReference(span)), | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int LastIndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>? | ||
{ | ||
if (Unsafe.SizeOf<T>() == sizeof(byte) && RuntimeHelpers.IsBitwiseEquatable<T>()) | ||
return SpanHelpers.LastIndexOf( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
span.Length, | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), | ||
value.Length); | ||
if (RuntimeHelpers.IsBitwiseEquatable<T>()) | ||
{ | ||
if (Unsafe.SizeOf<T>() == sizeof(byte)) | ||
{ | ||
return SpanHelpers.LastIndexOf( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
span.Length, | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), | ||
value.Length); | ||
} | ||
else if (Unsafe.SizeOf<T>() == sizeof(char)) | ||
{ | ||
return SpanHelpers.LastIndexOf( | ||
ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), | ||
span.Length, | ||
ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(value)), | ||
value.Length); | ||
} | ||
} | ||
|
||
return SpanHelpers.LastIndexOf<T>(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length); | ||
} | ||
|
@@ -1301,23 +1314,50 @@ public static int IndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value | |
{ | ||
if (Unsafe.SizeOf<T>() == sizeof(byte)) | ||
{ | ||
ref byte spanRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)); | ||
ref byte valueRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)); | ||
if (values.Length == 2) | ||
{ | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
span.Length); | ||
} | ||
else if (values.Length == 3) | ||
switch (values.Length) | ||
{ | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
span.Length); | ||
case 0: | ||
return -1; | ||
|
||
case 1: | ||
return SpanHelpers.IndexOfValueType(ref spanRef, valueRef, span.Length); | ||
|
||
case 2: | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
span.Length); | ||
|
||
case 3: | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
span.Length); | ||
|
||
#if !MONO // We don't have a mono overload for 4 values | ||
case 4: | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
Unsafe.Add(ref valueRef, 3), | ||
span.Length); | ||
#endif | ||
case 5: | ||
return SpanHelpers.IndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
Unsafe.Add(ref valueRef, 3), | ||
Unsafe.Add(ref valueRef, 4), | ||
span.Length); | ||
} | ||
} | ||
|
||
|
@@ -1478,18 +1518,8 @@ ref Unsafe.As<T, short>(ref MemoryMarshal.GetReference(span)), | |
/// </summary> | ||
/// <param name="span">The span to search.</param> | ||
/// <param name="values">The set of values to search for.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int LastIndexOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>? | ||
{ | ||
if (Unsafe.SizeOf<T>() == sizeof(byte) && RuntimeHelpers.IsBitwiseEquatable<T>()) | ||
return SpanHelpers.LastIndexOfAny( | ||
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. This was calling the same function as the fallback |
||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
span.Length, | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)), | ||
values.Length); | ||
|
||
return SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length); | ||
} | ||
=> LastIndexOfAny((ReadOnlySpan<T>)span, values); | ||
|
||
/// <summary> | ||
/// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. | ||
|
@@ -1570,23 +1600,41 @@ public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> v | |
{ | ||
if (Unsafe.SizeOf<T>() == sizeof(byte)) | ||
{ | ||
ref byte spanRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)); | ||
ref byte valueRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)); | ||
if (values.Length == 2) | ||
{ | ||
return SpanHelpers.LastIndexOfAnyValueType( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
span.Length); | ||
} | ||
else if (values.Length == 3) | ||
switch (values.Length) | ||
{ | ||
return SpanHelpers.LastIndexOfAnyValueType( | ||
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
span.Length); | ||
case 0: | ||
return -1; | ||
|
||
case 1: | ||
return SpanHelpers.LastIndexOfValueType(ref spanRef, valueRef, span.Length); | ||
|
||
case 2: | ||
return SpanHelpers.LastIndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
span.Length); | ||
|
||
case 3: | ||
return SpanHelpers.LastIndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
span.Length); | ||
|
||
#if !MONO // We don't have a mono overload for 4 values | ||
case 4: | ||
return SpanHelpers.LastIndexOfAnyValueType( | ||
ref spanRef, | ||
valueRef, | ||
Unsafe.Add(ref valueRef, 1), | ||
Unsafe.Add(ref valueRef, 2), | ||
Unsafe.Add(ref valueRef, 3), | ||
span.Length); | ||
#endif | ||
} | ||
} | ||
|
||
|
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.
@Rob-Hague @adamsitnik
Was this intentional?
For better or worse, the
LastIndexOfAny(ROS)
overload is marked withAggressiveInlining
, so thisSpan
overload will now be more expensive for const values.At the very least, it's now even more inconsistent with other places (e.g.
IndexOfAny
).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 was intentional in that it became consistent with many of the other Span to ROS forwards though I agree it is (now) inconsistent with IndexOfAny(Span, ROS). I probably overlooked that.
I also believed that the annotation was superfluous in such cases - I apologise for the assumption as I did not instrument that.