-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Vectorize IndexOfAnyExcept for four values #73696
Vectorize IndexOfAnyExcept for four values #73696
Conversation
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsI took a look and the usage of
Since the first two got vectorized by @stephentoub in #73488 I've vectorized the 4 values case. Benchmarks: public class IndexOfAnyExcept
{
private string _whiteSpaces, _noWhiteSpaces;
[Params(1, 4, 16, 64, 256, 1024)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{
_whiteSpaces = new string(' ', Length);
_noWhiteSpaces = new string('a', Length);
}
[Benchmark]
public int ImmediateMismatch() => _noWhiteSpaces.AsSpan().IndexOfAnyExcept(" \t\r\n");
[Benchmark]
public int NoMismatch() => _whiteSpaces.AsSpan().IndexOfAnyExcept(" \t\r\n");
} BenchmarkDotNet=v0.13.1.1845-nightly, OS=Windows 11 (10.0.22000.795/21H2)
AMD Ryzen Threadripper PRO 3945WX 12-Cores, 1 CPU, 24 logical and 12 physical cores
.NET SDK=7.0.100-preview.7.22377.5
[Host] : .NET 7.0.0 (7.0.22.37506), X64 RyuJIT AVX2
Job-OQFSEO : .NET 7.0.0 (42.42.42.42424), X64 RyuJIT AVX2
Job-main : .NET 7.0.0 (42.42.42.42424), X64 RyuJIT AVX2
It's clearly a tradeoff:
|
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
Outdated
Show resolved
Hide resolved
Some pretty nice improvements! |
The CI did not start... I am going to close and re-open the PR |
@@ -619,6 +680,9 @@ public static ReadOnlyMemory<char> AsMemory(this string? text, Range range) | |||
case 3: | |||
return IndexOfAnyExcept(span, values[0], values[1], values[2]); | |||
|
|||
case 4: // common for searching whitespaces |
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.
Nit: (don't restart CI just for this)
case 4: // common for searching whitespaces | |
case 4: // common for searching ASCII whitespaces |
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static int ComputeIndex<T>(ref T searchSpace, ref T current, Vector128<T> notEquals) where T : struct, IEquatable<T> |
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.
There are many other methods in this type, whereas this "ComputeIndex" is only relevant to LastIndexOfExcept. Consider renaming it accordingly.
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.
Thanks!
@stephentoub Since I currently can't merge #73768, I am going to merge this PR now and address your feedback later (renaming |
I took a look and the usage of
IndexOfAnyExcept
and the patterns are following:IndexOfAnyExcept(byte)
- most frequentIndexOfAnyExcept(int)
,IndexOfAnyExcept(char)
- sporadicIndexOfAnyExcept(" \t\r\n")
- few placesSince the first two got vectorized by @stephentoub in #73488 I've vectorized the 4 values case.
Benchmarks:
It's clearly a tradeoff: