Skip to content

Remove mono specific SpanHelpers #78015

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
<ItemGroup Condition="'$(FeatureMono)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)System\SpanHelpers.Mono.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,6 @@ public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> v
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,
Expand All @@ -1936,7 +1935,6 @@ public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> v
Unsafe.Add(ref valueRef, 2),
Unsafe.Add(ref valueRef, 3),
span.Length);
#endif

default:
return ProbabilisticMap.LastIndexOfAny(ref Unsafe.As<short, char>(ref spanRef), span.Length, ref Unsafe.As<short, char>(ref valueRef), values.Length);
Expand Down
44 changes: 28 additions & 16 deletions src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,22 @@ internal static unsafe int IndexOfNullByte(ref byte searchSpace)
{
lengthToExamine -= 8;

if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
ref byte current = ref Unsafe.AddByteOffset(ref searchSpace, offset);
if (uValue == current)
goto Found;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
if (uValue == Unsafe.AddByteOffset(ref current, 1))
goto Found1;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
if (uValue == Unsafe.AddByteOffset(ref current, 2))
goto Found2;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
if (uValue == Unsafe.AddByteOffset(ref current, 3))
goto Found3;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 4))
if (uValue == Unsafe.AddByteOffset(ref current, 4))
goto Found4;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 5))
if (uValue == Unsafe.AddByteOffset(ref current, 5))
goto Found5;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 6))
if (uValue == Unsafe.AddByteOffset(ref current, 6))
goto Found6;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 7))
if (uValue == Unsafe.AddByteOffset(ref current, 7))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these changes actually help? On coreclr at least it's not clear to me just from the diff that it's a net win.
https://www.diffchecker.com/du4Cevvb
Can you share throughput numbers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any improvement on coreclr. I wouldn't be surprised if the generated code is more or less identical, since the JIT might make use of common subexpression elimination optimization. Also the code paths tweaked in this PR are for non vectorized cases, which are mostly not hit at all with coreclr.

The BCL change is intended for less optimized compilers to be able to generate good quality code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any improvement on coreclr.

I'm more concerned it might regress. At least according to the diff I linked above, the new asm is a bit longer and appears to use a different addressing mode. I don't have a good sense for what that might mean in terms of throughput.

Also the code paths tweaked in this PR are for non vectorized cases, which are mostly not hit at all with coreclr.

Aren't they used for shorter inputs that don't have enough data to fill a vector?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The local perf numbers on some benchmarks are unclear, I'm waiting for a full performance run.

goto Found7;

offset += 8;
Expand All @@ -388,13 +389,14 @@ internal static unsafe int IndexOfNullByte(ref byte searchSpace)
{
lengthToExamine -= 4;

if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset))
ref byte current = ref Unsafe.AddByteOffset(ref searchSpace, offset);
if (uValue == current)
goto Found;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 1))
if (uValue == Unsafe.AddByteOffset(ref current, 1))
goto Found1;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 2))
if (uValue == Unsafe.AddByteOffset(ref current, 2))
goto Found2;
if (uValue == Unsafe.AddByteOffset(ref searchSpace, offset + 3))
if (uValue == Unsafe.AddByteOffset(ref current, 3))
goto Found3;

offset += 4;
Expand Down Expand Up @@ -985,13 +987,23 @@ public static nuint CommonPrefixLength(ref byte first, ref byte second, nuint le

for (; (nint)i <= (nint)length - 4; i += 4)
{
if (Unsafe.Add(ref first, i + 0) != Unsafe.Add(ref second, i + 0)) return i + 0;
if (Unsafe.Add(ref first, i + 1) != Unsafe.Add(ref second, i + 1)) return i + 1;
if (Unsafe.Add(ref first, i + 2) != Unsafe.Add(ref second, i + 2)) return i + 2;
if (Unsafe.Add(ref first, i + 3) != Unsafe.Add(ref second, i + 3)) return i + 3;
ref byte currentFirst = ref Unsafe.Add(ref first, i);
ref byte currentSecond = ref Unsafe.Add(ref second, i);
if (currentFirst != currentSecond) goto Found0;
if (Unsafe.Add(ref currentFirst, 1) != Unsafe.Add(ref currentSecond, 1)) goto Found1;
if (Unsafe.Add(ref currentFirst, 2) != Unsafe.Add(ref currentSecond, 2)) goto Found2;
if (Unsafe.Add(ref currentFirst, 3) != Unsafe.Add(ref currentSecond, 3)) goto Found3;
}

return length;
Found0:
return i;
Found1:
return i + 1;
Found2:
return i + 2;
Found3:
return i + 3;
}

Debug.Assert(length >= (uint)Vector128<byte>.Count);
Expand Down
Loading