Skip to content

Fix utf8parser midpoints - #138126

Closed
lilinus wants to merge 7 commits into
dotnet:mainfrom
lilinus:fix-utf8parser-midpoints
Closed

lilinus wants to merge 7 commits into
dotnet:mainfrom
lilinus:fix-utf8parser-midpoints

Conversation

@lilinus

@lilinus lilinus commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Addresses UTF8PARSER-FLOAT-1 and UTF8PARSER-DECIMAL-1 in #134839

  • Add fixup to normalize trailing fractional zeros for float and double without changing decimal scale so it matches Number.TryParseNumber.
  • Set HasNonZeroTail only for nonzero discarded digits.
  • Exclude leading fractional zeros from digitCount.
  • Add test cases.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 23, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-buffers
See info in area-owners.md if you want to be subscribed.

@lilinus

This comment was marked as outdated.

@lilinus

This comment was marked as outdated.

@lilinus

This comment was marked as outdated.

@lilinus

lilinus commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@EgorBot -arm -amd --envvars DOTNET_JitDisasm:Bench

using BenchmarkDotNet.Attributes;
using System.Buffers.Text;

public class MyBenchmarks
{
    private static readonly byte[] input= "123.456"u8.ToArray();
    [Benchmark]
    public double ParseDouble() => Utf8Parser.TryParse(input, out double value, out _) ? value : throw new InvalidOperationException();
    [Benchmark]
    public decimal ParseDecimal() => Utf8Parser.TryParse(input, out decimal value, out _) ? value : throw new InvalidOperationException();
}

@lilinus
lilinus force-pushed the fix-utf8parser-midpoints branch from 66ce72c to 40cef00 Compare November 24, 2026 15:14
srcIndex++;
digitCount++;

if (digitCount >= maxDigitCount)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is overall an overly complex change that's adding in a goto (undesirable), when it could be much simpler.

Since we're already tracking hasNonZeroTail so either its being tracked or consumed incorrectly and it should be a much smaller localized fix intead.

Namely, I think that this line should've been digitCount > maxDigitCount, same with the other a bit further down. Then the int startIndexOfDigitsAfterDecimalToCopy = startIndexDigitsAfterDecimal; if (dstIndex == 0) { ... } needs to move up slightly to just after int startIndexDigitsAfterDecimal, so its:

                int startIndexDigitsAfterDecimal = srcIndex;

                if (dstIndex == 0)
                {
                    // Not copied any digits to the Number struct yet. This means we must continue discarding leading zeroes even though they appeared after the decimal point.
                    while (srcIndex < source.Length && source[srcIndex] == '0')
                    {
                        number.Scale--;
                        srcIndex++;
                    }
                }

                int startIndexOfDigitsAfterDecimalToCopy = srcIndex;

Finally, you need to update fractionalDigitsPresent != 0 in Number.NumberToFloatingPointBits.cs to instead be number.HasNonZeroTail || fractionalDigitsPresent != 0 and number.hasNonZeroTail, respectively. You also need to add a TrimTrailingFractionalZeros function to ConvertDecimalIeee754ToFloat and TryParseNormalAsFloatingPoint, where that is functionally:

        internal static void TrimTrailingFractionalZeros(ref NumberBuffer number)
        {
            int fractionalStart = Math.Max(number.Scale, 0);
            int digitsCount = number.DigitsCount;

            if (!number.HasNonZeroTail && fractionalStart < digitsCount && number.Digits[digitsCount - 1] == '0')
            {
                digitsCount = fractionalStart + number.Digits.Slice(fractionalStart, digitsCount - fractionalStart).LastIndexOfAnyExcept((byte)'0') + 1;
                number.DigitsCount = digitsCount;
                number.Digits[digitsCount] = 0;
            }
        }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can put up the relevant patch if needed, since I've already done it locally, but it simplifies things without causing too much churn and helps ensure things are more consistent/normalized across all paths

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I struggled a bit to get a fix without regressing perf and I only wanted to touch code isolated to these methods in Utf8Parser. The gotos were a last resort... If you have the fix locally, then be my guest and do as you see fit with this PR.

Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Put up #138242

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Buffers community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants