Skip to content
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

[WIP] Improve performance of Utf8Parser.TryParseInt32D #9

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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 @@ -7,6 +7,13 @@
using System.Runtime.CompilerServices;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.Buffers.Text
{
internal static class ParserHelpers
Expand Down Expand Up @@ -55,6 +62,27 @@ public static bool IsDigit(int i)
return (uint)(i - '0') <= ('9' - '0');
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryConvertToDigit(int byteValue, out int digit)
{
digit = byteValue - '0';
return (uint)digit <= 9;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetDigitAt(ReadOnlySpan<byte> source, nuint index, out int digit)
{
if (source.TryGetElementAt(index, out byte tempByte) && TryConvertToDigit(tempByte, out digit))
{
return true;
}
else
{
digit = default;
return false;
}
}

//
// Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= default; bytesConsumed = 0; return false" boilerplate.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.Buffers.Text
{
public static partial class Utf8Parser
Expand Down Expand Up @@ -194,144 +203,128 @@ private static bool TryParseInt16D(ReadOnlySpan<byte> source, out short value, o

private static bool TryParseInt32D(ReadOnlySpan<byte> source, out int value, out int bytesConsumed)
{
if (source.Length < 1)
if (source.IsEmpty)
goto FalseExit;

int sign = 1;
int index = 0;
int num = source[index];
if (num == '-')
{
sign = -1;
index++;
if ((uint)index >= (uint)source.Length)
goto FalseExit;
num = source[index];
}
else if (num == '+')
{
index++;
if ((uint)index >= (uint)source.Length)
goto FalseExit;
num = source[index];
}
int sign = 0;
nuint index = 0;
int answer = source[0];

int answer = 0;
TryAgain:

if (ParserHelpers.IsDigit(num))
if (ParserHelpers.TryConvertToDigit(answer, out answer))
{
if (num == '0')
if (answer == 0)
{
do
{
index++;
if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
} while (num == '0');
if (!ParserHelpers.IsDigit(num))
goto Done;
if (!source.TryGetElementAt(index, out byte thisByte))
goto ReturnZero;
answer = thisByte;
} while (answer == '0');
if (!ParserHelpers.TryConvertToDigit(answer, out answer))
goto ReturnZero;
}

answer = num - '0';
index++;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out int num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
answer = 10 * answer + num - '0';
answer = 10 * answer + num;

// Potential overflow
if ((uint)index >= (uint)source.Length)
goto Done;
num = source[index];
if (!ParserHelpers.IsDigit(num))
if (!ParserHelpers.TryGetDigitAt(source, index, out num))
goto Done;
index++;
if (answer > int.MaxValue / 10)
goto FalseExit; // Overflow
answer = answer * 10 + num - '0';
// if sign < 0, (-1 * sign + 1) / 2 = 1
// else, (-1 * sign + 1) / 2 = 0
if ((uint)answer > (uint)int.MaxValue + (-1 * sign + 1) / 2)
answer = answer * 10 + num;

// if sign = 0, checks answer against 0x7FFFFFFF (int.MaxValue)
// if sign = -1, checks answer against 0x80000000 (int.MinValue)
if ((uint)answer > (uint)(int.MaxValue - sign))
goto FalseExit; // Overflow

if ((uint)index >= (uint)source.Length)
goto Done;
if (!ParserHelpers.IsDigit(source[index]))

if (!ParserHelpers.TryGetDigitAt(source, index, out _))
goto Done;

// Guaranteed overflow
goto FalseExit;
}
else
{
if (answer == '-' - '0')
{
sign = -1;
}
else if (answer != '+' - '0')
{
goto FalseExit;
}

if (index != 0 || (uint)1 >= (uint)source.Length)
{
goto FalseExit;
}

answer = source[1];
index++;
goto TryAgain;
}

FalseExit:
bytesConsumed = default;
value = default;
return false;

ReturnZero:
answer = 0;

Done:
bytesConsumed = index;
value = answer * sign;
bytesConsumed = (int)index;

// If sign = 0, value = (answer ^ 0) - 0 = answer
// If sign = -1, value = (answer ^ -1) - (-1) = ~answer + 1 = -answer
Debug.Assert(sign == 0 || sign == -1);
value = (answer ^ sign) - sign;
return true;
}

Expand Down
Loading