Skip to content
Closed
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 @@ -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
{
Expand All @@ -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)
Comment on lines 24 to +28
Copy link

Copilot AI Apr 9, 2026

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.

Copilot uses AI. Check for mistakes.
{
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
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The new Vector128 fast-path only searches for the quote character ("), but this helper’s contract (and its callers) require returning the first occurrence of any of: quote, backslash, or any control character (< 0x20). If a backslash or control char appears before the closing quote within the first 16 bytes, this will now incorrectly return the quote index and skip required validation/escape handling. Please either remove this fast-path or extend it to compute the earliest index across all three conditions before returning.

Suggested change
// 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);
}
}
}

Copilot uses AI. Check for mistakes.
return span.IndexOfAny(s_controlQuoteBackslash);
}
}
}
Loading