forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Double IndexOf throughput for chars (dotnet#78861)
* Add PackedIndexOf for chars * Add Contains and IndexOfValue(3 chars) * Stop using PackedIndexOf on ARM * Improve code comment
- Loading branch information
Showing
15 changed files
with
1,275 additions
and
98 deletions.
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
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
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
49 changes: 49 additions & 0 deletions
49
src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAny1CharValue.cs
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class IndexOfAny1CharValue<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _e0; | ||
|
||
public IndexOfAny1CharValue(char value) => | ||
_e0 = value; | ||
|
||
internal override char[] GetValues() => new[] { _e0 }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), _e0, span.Length) | ||
: SpanHelpers.NonPackedIndexOfValueType<short, SpanHelpers.DontNegate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, span.Length) | ||
: SpanHelpers.NonPackedIndexOfValueType<short, SpanHelpers.Negate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _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); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAny2CharValues.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class IndexOfAny2CharValue<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _e0, _e1; | ||
|
||
public IndexOfAny2CharValue(char value0, char value1) => | ||
(_e0, _e1) = (value0, value1); | ||
|
||
internal override char[] GetValues() => new[] { _e0, _e1 }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0 || value == _e1; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.DontNegate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _e1), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.Negate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _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); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAny3CharValues.cs
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class IndexOfAny3CharValue<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _e0, _e1, _e2; | ||
|
||
public IndexOfAny3CharValue(char value0, char value1, char value2) => | ||
(_e0, _e1, _e2) = (value0, value1, value2); | ||
|
||
internal override char[] GetValues() => new[] { _e0, _e1, _e2 }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0 || value == _e1 || value == _e2; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), _e0, _e1, _e2, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.DontNegate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _e1), | ||
Unsafe.As<char, short>(ref _e2), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? PackedSpanHelpers.IndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, _e1, _e2, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.Negate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _e1), | ||
Unsafe.As<char, short>(ref _e2), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAny(_e0, _e1, _e2); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_e0, _e1, _e2); | ||
} | ||
} |
Oops, something went wrong.