perf: Optimize regular numbers parse logics#990
Conversation
432f828 to
a926285
Compare
Microbenchmark results
Baseline
Optimized
Test Code [MemoryDiagnoser]
[ExceptionDiagnoser]
[ShortRunJob(RuntimeMoniker.Net80)]
[ShortRunJob(RuntimeMoniker.Net47)]
public class AttemptUnknownTypeDeserializationBenchmarks
{
private static readonly IDeserializer deserializer = new DeserializerBuilder().WithAttemptingUnquotedStringTypeDeserialization().Build();
private static readonly string TestData =
"""
byteMaxValue: 255
shortMaxValue: 32767
longMaxValue : 9223372036854775807
ulongMaxValue: 18446744073709551615
floatMaxValue: 3.4028235E+38
doubleMaxValue: 1.7976931348623157E+308
""";
[Benchmark]
public object? Deserialize()
{
return deserializer.Deserialize(TestData);
}
} |
|
I like performance fixes. And this is a pretty big one! If you can address the 2 comments then I'll merge this in and get it out. |
|
Apart from this PR. Above regex checking Is it able to change Regex to use more strict regex with |
|
You can add those to that regex without any issue I would think. |
Thanks for your response. |
|
I've removed wrong comment in commit.
|
This PR intended to optimize performance for number deserialization by changing Parse to
TryParsemethod.It's discussed at #794
Additionally this PR fix
doublevalue deserialization problems that are found on UnitTests.What's Changes
Parselogics withTryParsemethod.float/doubleparse logics for.NET Framework.double, then checks float value ranges.UnquotedStringTypeDeserialization_RegularNumbers).NET Behavior differences
On .NET Framework.
float.Parse((double.MaxValue + 1).ToString())throwOverflowException.But on .NET Core. It returns PosititiveInfinite(
∞).These behavior differences are described at Single.Parse Method page.
On current implementation.
Values outside the range of the
floattype are converted to-∞/∞.So I've added logics to check float value ranges.