-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Optimize Version JSON deserialization with UTF-8 parsing #118207
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <2813363+eiriktsarpalis@users.noreply.github.com>
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 <2813363+eiriktsarpalis@users.noreply.github.com>
...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 <2813363+eiriktsarpalis@users.noreply.github.com>
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.ReadTests.cs
Outdated
Show resolved
Hide resolved
...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
Show resolved
Hide resolved
...ies/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs
Outdated
Show resolved
Hide resolved
Head branch was pushed to by a user without write access
- 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 <2813363+eiriktsarpalis@users.noreply.github.com>
| ThrowHelper.ThrowFormatException(DataType.Version); | ||
| } | ||
|
|
||
| bool success = Version.TryParse(source, out Version? result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <2813363+eiriktsarpalis@users.noreply.github.com>
...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 <2813363+eiriktsarpalis@users.noreply.github.com>
...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 <2813363+eiriktsarpalis@users.noreply.github.com>
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.