Repository navigation
Fix utf8parser midpoints - #138126
Fix utf8parser midpoints#138126lilinus wants to merge 7 commits into
Conversation
|
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. |
|
Tagging subscribers to this area: @dotnet/area-system-buffers |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
@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();
} |
66ce72c to
40cef00
Compare
| srcIndex++; | ||
| digitCount++; | ||
|
|
||
| if (digitCount >= maxDigitCount) |
There was a problem hiding this comment.
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;
}
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Addresses
UTF8PARSER-FLOAT-1andUTF8PARSER-DECIMAL-1in #134839floatanddoublewithout changing decimal scale so it matchesNumber.TryParseNumber.HasNonZeroTailonly for nonzero discarded digits.digitCount.