Optimize Version JSON deserialization with UTF-8 parsing#118207
Optimize Version JSON deserialization with UTF-8 parsing#118207
Conversation
Co-authored-by: eiriktsarpalis <[email protected]>
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Rather than making checks stricter, this should make the reader more permissive allowing inputs that Version.Parse permits. Please update the PR title and description to reflect that.
…rse behavior Co-authored-by: eiriktsarpalis <[email protected]>
Updated the implementation to make the JSON Version converter more permissive rather than stricter. Removed the restrictive validation that was rejecting leading plus signs, leading/trailing whitespace, and now the converter accepts all inputs that |
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Show resolved
Hide resolved
…lback for VersionConverter Co-authored-by: eiriktsarpalis <[email protected]>
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Show resolved
Hide resolved
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: eiriktsarpalis <[email protected]>
Co-authored-by: eiriktsarpalis <[email protected]>
- Fix MaximumVersionLength -> MaximumFormattedVersionLength references - Add Debug.Assert for formattedSuccessfully to document invariant - Remove trailing whitespace from test comment - Remove UTF8-Parsing-Optimization-Tracking.md per feedback Co-authored-by: eiriktsarpalis <[email protected]>
| ThrowHelper.ThrowFormatException(DataType.Version); | ||
| } | ||
|
|
||
| bool success = Version.TryParse(source, out Version? result); |
There was a problem hiding this comment.
@copilot please change this so that the UTF-8 variant of Version.TryParse is used here. This should eliminate the intermediate transcoding to UTF-16. Use the UTF-8 variant of Utf8JsonReader.CopyTo to copy to an intermediate UTF-8 buffer.
There was a problem hiding this comment.
Implemented UTF-8 optimization in commit f5dd272. The converter now uses Version.TryParse(ReadOnlySpan<byte>) and CopyString(Span<byte>) to parse directly from UTF-8 bytes, eliminating the intermediate transcoding to UTF-16. Added IsWhiteSpaceByte helper to properly validate leading/trailing whitespace on UTF-8 bytes while maintaining consistent behavior with the char-based validation.
- Use UTF-8 variant of Version.TryParse to eliminate transcoding overhead - Use CopyString(Span<byte>) to copy directly to UTF-8 buffer - Add IsWhiteSpaceByte helper for whitespace validation on UTF-8 bytes - Maintain conditional stackalloc/ArrayPool pattern with byte buffers - Preserve leading/trailing whitespace rejection behavior Co-authored-by: eiriktsarpalis <[email protected]>
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
- Use if (Version.TryParse(...)) pattern instead of bool success variable - Inline empty check with whitespace validation for cleaner code Co-authored-by: eiriktsarpalis <[email protected]>
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Show resolved
Hide resolved
Co-authored-by: eiriktsarpalis <[email protected]>
|
/ba-g test failures unrelated |
Summary
Fixes #118201
Successfully implemented optimizations for Version JSON deserialization to remove leading plus sign validation and add UTF-8 parsing support for improved performance.
Changes Made
VersionConverterchar.IsWhiteSpacevalidation instead of restrictive digit-only checks"+1.1"in success cases and proper whitespace validation in failure casesVersion.TryParse(ReadOnlySpan<byte>)to eliminate UTF-8→UTF-16 transcoding overheadCopyString(Span<byte>)to copy directly to UTF-8 bufferIsWhiteSpaceBytehelper for proper whitespace validation on UTF-8 bytesMaximumFormattedVersionLength)Testing
"+1.1") now deserialize successfully toVersion: 1.1"1 .1","1. 1") continues to work properly" 1.2.3.4","1.2.3.4 ") is correctly rejectedPerformance Improvements
The UTF-8 optimization in .NET 11+ provides:
Future Work
Other System.Text.Json value converters could benefit from similar UTF-8 optimizations. Types with UTF-8
TryParsesupport in .NET 10+ include: Byte, SByte, Int16/32/64/128, UInt16/32/64/128, IntPtr, UIntPtr, Decimal, Single, Double, Half, Char, and Guid. This should be tracked in a separate issue for future optimization work.The converter is now more permissive and properly aligned with
Version.Parsebehavior while maintaining appropriate validation for JSON deserialization consistency.💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.