Skip to content
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 3 commits into from
Oct 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 95 additions & 47 deletions src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)]
Copy link
Member

@MihaZupan MihaZupan Oct 26, 2022

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 with AggressiveInlining, so this Span 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).

Copy link
Contributor Author

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.

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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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
}
}

Expand Down