-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Faster IndexOfAny for IgnoreCase Ascii letters #96588
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
MihaZupan
merged 8 commits into
dotnet:main
from
MihaZupan:indexofany-packed-ignorecase
Feb 17, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10430e6
Add packed IgnoreCase IndexOfAny variants
MihaZupan 1425886
Emit SearchValues for ASCII sets of 4/5 values in RegexCompliler
MihaZupan c63391e
Remove Any3CharPackedIgnoreCase
MihaZupan 066a29d
Update asserts
MihaZupan b467a13
Attribute order
MihaZupan 5d29af7
Fix build
MihaZupan a919e16
More comments
MihaZupan 5a2568b
Tweak ContainsCore
MihaZupan 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
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
52 changes: 52 additions & 0 deletions
52
...es/System.Private.CoreLib/src/System/SearchValues/Any1CharPackedIgnoreCaseSearchValues.cs
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Any1CharPackedIgnoreCaseSearchValues : SearchValues<char> | ||
{ | ||
// While this most commonly applies to ASCII letters, it also works for other values that differ by 0x20 (e.g. "[{" => "{"). | ||
// _lowerCase is therefore not necessarily a lower case ASCII letter, but just the higher value (the one with the 0x20 bit set). | ||
private readonly char _lowerCase, _upperCase; | ||
private readonly uint _lowerCaseUint; | ||
|
||
public Any1CharPackedIgnoreCaseSearchValues(char value) | ||
{ | ||
Debug.Assert((value | 0x20) == value); | ||
|
||
_lowerCase = value; | ||
_upperCase = (char)(value & ~0x20); | ||
_lowerCaseUint = value; | ||
} | ||
|
||
internal override char[] GetValues() => | ||
[_upperCase, _lowerCase]; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
(uint)(value | 0x20) == _lowerCaseUint; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyIgnoreCase(ref MemoryMarshal.GetReference(span), _lowerCase, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyExceptIgnoreCase(ref MemoryMarshal.GetReference(span), _lowerCase, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAny(_lowerCase, _upperCase); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_lowerCase, _upperCase); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any1CharPackedSearchValues.cs
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Any1CharPackedSearchValues : SearchValues<char> | ||
{ | ||
private readonly char _e0; | ||
|
||
public Any1CharPackedSearchValues(char value) => | ||
_e0 = value; | ||
|
||
internal override char[] GetValues() => | ||
[_e0]; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), _e0, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOf(_e0); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_e0); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any1SearchValues.cs
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
#pragma warning disable 8500 // address of managed types | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Any1SearchValues<T, TImpl> : SearchValues<T> | ||
where T : struct, IEquatable<T> | ||
where TImpl : struct, INumber<TImpl> | ||
{ | ||
private readonly TImpl _e0; | ||
|
||
public Any1SearchValues(ReadOnlySpan<TImpl> values) | ||
{ | ||
Debug.Assert(Unsafe.SizeOf<T>() == Unsafe.SizeOf<TImpl>()); | ||
Debug.Assert(values.Length == 1); | ||
_e0 = values[0]; | ||
} | ||
|
||
internal override unsafe T[] GetValues() | ||
{ | ||
TImpl e0 = _e0; | ||
return new[] { *(T*)&e0 }; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override unsafe bool ContainsCore(T value) => | ||
*(TImpl*)&value == _e0; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<T> span) => | ||
SpanHelpers.NonPackedIndexOfValueType<TImpl, SpanHelpers.DontNegate<TImpl>>(ref Unsafe.As<T, TImpl>(ref MemoryMarshal.GetReference(span)), _e0, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<T> span) => | ||
SpanHelpers.NonPackedIndexOfValueType<TImpl, SpanHelpers.Negate<TImpl>>(ref Unsafe.As<T, TImpl>(ref MemoryMarshal.GetReference(span)), _e0, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<T> span) => | ||
SpanHelpers.LastIndexOfValueType(ref Unsafe.As<T, TImpl>(ref MemoryMarshal.GetReference(span)), _e0, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<T> span) => | ||
SpanHelpers.LastIndexOfAnyExceptValueType(ref Unsafe.As<T, TImpl>(ref MemoryMarshal.GetReference(span)), _e0, span.Length); | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any2ByteSearchValues.cs
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
...es/System.Private.CoreLib/src/System/SearchValues/Any2CharPackedIgnoreCaseSearchValues.cs
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics.Arm; | ||
using System.Runtime.Intrinsics.Wasm; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Any2CharPackedIgnoreCaseSearchValues : SearchValues<char> | ||
{ | ||
// While this most commonly applies to ASCII letters, it also works for other values that differ by 0x20 (e.g. "[]{}" => "{}"). | ||
// _e0 and _e1 are therefore not necessarily lower case ASCII letters, but just the higher values (the ones with the 0x20 bit set). | ||
private readonly char _e0, _e1; | ||
private readonly uint _uint0, _uint1; | ||
private IndexOfAnyAsciiSearcher.AsciiState _state; | ||
|
||
public Any2CharPackedIgnoreCaseSearchValues(char value0, char value1) | ||
{ | ||
Debug.Assert((value0 | 0x20) == value0 && char.IsAscii(value0)); | ||
Debug.Assert((value1 | 0x20) == value1 && char.IsAscii(value1)); | ||
|
||
(_e0, _e1) = (value0, value1); | ||
(_uint0, _uint1) = (value0, value1); | ||
IndexOfAnyAsciiSearcher.ComputeAsciiState([(char)(_e0 & ~0x20), _e0, (char)(_e1 & ~0x20), _e1], out _state); | ||
} | ||
|
||
internal override char[] GetValues() => | ||
[(char)(_e0 & ~0x20), _e0, (char)(_e1 & ~0x20), _e1]; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) | ||
{ | ||
uint lowerCase = (uint)(value | 0x20); | ||
return lowerCase == _uint0 || lowerCase == _uint1; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyIgnoreCase(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyExceptIgnoreCase(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Ssse3))] | ||
[CompExactlyDependsOn(typeof(AdvSimd))] | ||
[CompExactlyDependsOn(typeof(PackedSimd))] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate, IndexOfAnyAsciiSearcher.Default>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Ssse3))] | ||
[CompExactlyDependsOn(typeof(AdvSimd))] | ||
[CompExactlyDependsOn(typeof(PackedSimd))] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearcher.Default>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any2CharPackedSearchValues.cs
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Any2CharPackedSearchValues : SearchValues<char> | ||
{ | ||
private readonly char _e0, _e1; | ||
|
||
public Any2CharPackedSearchValues(char value0, char value1) => | ||
(_e0, _e1) = (value0, value1); | ||
|
||
internal override char[] GetValues() => | ||
[_e0, _e1]; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0 || value == _e1; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
[CompExactlyDependsOn(typeof(Sse2))] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
PackedSpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAny(_e0, _e1); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_e0, _e1); | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any2CharSearchValues.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.