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,10 @@
// 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.Intrinsics;
using System.Runtime.Intrinsics.Arm;
Comment on lines +5 to +8

namespace System.Text.Json
{
Expand All @@ -19,7 +22,32 @@ internal static partial class JsonReaderHelper
"\\"u8);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfQuoteOrAnyControlOrBackSlash(this ReadOnlySpan<byte> span) =>
public static int IndexOfQuoteOrAnyControlOrBackSlash(this ReadOnlySpan<byte> span)
{
// For most inputs, we have a large span and we typically find a match within the first 16 bytes
// Usually, it's a quote in a property name.
if (!Vector128.IsHardwareAccelerated || !BitConverter.IsLittleEndian || span.Length < Vector128<byte>.Count)
return IndexOfQuoteOrAnyControlOrBackSlash_Fallback(span);

Vector128<byte> vec = Vector128.Create(span);

// Any control character (i.e. 0 to 31) or '"' or '\'
Vector128<byte> cmp = Vector128.LessThan(vec, Vector128.Create(JsonConstants.Space)) |
Vector128.Equals(vec, Vector128.Create(JsonConstants.Quote)) |
Vector128.Equals(vec, Vector128.Create(JsonConstants.BackSlash));

int idx = Vector128.IndexOfWhereAllBitsSet(cmp);
if (idx >= 0)
{
return idx;
}

int fallbackIndex = IndexOfQuoteOrAnyControlOrBackSlash_Fallback(span.Slice(Vector128<byte>.Count));
return fallbackIndex >= 0 ? fallbackIndex + Vector128<byte>.Count : fallbackIndex;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static int IndexOfQuoteOrAnyControlOrBackSlash_Fallback(ReadOnlySpan<byte> span) =>
span.IndexOfAny(s_controlQuoteBackslash);
}
}
Loading