-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Prevent ConsumeNextToken from inlining into ConsumeNextTokenOrRollback #126659
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Buffers; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Numerics; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Runtime.CompilerServices; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Runtime.InteropServices; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Runtime.Intrinsics; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Runtime.Intrinsics.Arm; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| namespace System.Text.Json | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -12,14 +16,38 @@ internal static partial class JsonReaderHelper | |||||||||||||||||||||||||||||||||||||||||||||||
| /// <remarks>https://tools.ietf.org/html/rfc8259</remarks> | ||||||||||||||||||||||||||||||||||||||||||||||||
| private static readonly SearchValues<byte> s_controlQuoteBackslash = SearchValues.Create( | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Any Control, < 32 (' ') | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F"u8 + | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Quote | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\""u8 + | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Backslash | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\\"u8); | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009"u8 + | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013"u8 + | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D"u8 + | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\u001E\u001F\"\\"u8); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| public static int IndexOfQuoteOrAnyControlOrBackSlash(this ReadOnlySpan<byte> span) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| span.IndexOfAny(s_controlQuoteBackslash); | ||||||||||||||||||||||||||||||||||||||||||||||||
| public static int IndexOfQuoteOrAnyControlOrBackSlash(this ReadOnlySpan<byte> span) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Fast path for " in the first 16 bytes | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (Vector128.IsHardwareAccelerated && span.Length >= 16) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ref byte ptr = ref MemoryMarshal.GetReference(span); | ||||||||||||||||||||||||||||||||||||||||||||||||
| Vector128<byte> matches = Vector128.Equals(Vector128.LoadUnsafe(ref ptr), Vector128.Create((byte)'"')); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (AdvSimd.IsSupported) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: use Vector128.IndexOf for both AdvSimd and Sse2 once | ||||||||||||||||||||||||||||||||||||||||||||||||
| ulong mask = AdvSimd.ShiftRightLogicalNarrowingLower(matches.AsUInt16(), 4).AsUInt64().ToScalar(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (mask != 0) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return BitOperations.TrailingZeroCount(mask) >> 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| uint mask = matches.ExtractMostSignificantBits(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (mask != 0) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return BitOperations.TrailingZeroCount(mask); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+49
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Fast path for " in the first 16 bytes | |
| if (Vector128.IsHardwareAccelerated && span.Length >= 16) | |
| { | |
| ref byte ptr = ref MemoryMarshal.GetReference(span); | |
| Vector128<byte> matches = Vector128.Equals(Vector128.LoadUnsafe(ref ptr), Vector128.Create((byte)'"')); | |
| if (AdvSimd.IsSupported) | |
| { | |
| // TODO: use Vector128.IndexOf for both AdvSimd and Sse2 once | |
| ulong mask = AdvSimd.ShiftRightLogicalNarrowingLower(matches.AsUInt16(), 4).AsUInt64().ToScalar(); | |
| if (mask != 0) | |
| { | |
| return BitOperations.TrailingZeroCount(mask) >> 2; | |
| } | |
| } | |
| else | |
| { | |
| uint mask = matches.ExtractMostSignificantBits(); | |
| if (mask != 0) | |
| { | |
| return BitOperations.TrailingZeroCount(mask); | |
| } | |
| } | |
| } |
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.
PR title mentions preventing ConsumeNextToken from inlining into ConsumeNextTokenOrRollback, but the change here is a new SIMD fast-path inside IndexOfQuoteOrAnyControlOrBackSlash. If the intent is to affect inlining decisions, please document that relationship here (or adjust the PR title/description) so reviewers can understand why low-level string scanning logic is being changed for that purpose.