-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
System.Text.Json.JsonSerializer: boxing when serialize structs #94908
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionI noticed that JsonSerializer produces at least one boxing per every serialized struct with public properties/fields. I was trying to implement some allocation-free processing with buffers and Utf8JsonWrite instances pooling, and found that it anyway impossible with current JsonSerializer implementation because of the boxing. ConfigurationTested on .NET7 and .NET8 DataAny generic or non-generic struct with public properties/fields, for example PointF or something like:
or
AnalysisObviously, boxing happens (at least) in ObjectDefaultConverter, method OnTryWrite: Line 313 in 479df47
Wouldn't it be good to make this and related code boxing-free?
|
This is a side-effect of the current design of the
Duplicate of #82720 |
@eiriktsarpalis, thanks for explanation. |
Description
I noticed that JsonSerializer produces at least one boxing per every serialized struct with public properties/fields.
And maybe because of that, serialization of class instances is noticeably faster than serialization of structs with similar structure.
I was trying to implement some allocation-free processing with buffers and Utf8JsonWriter instances pooling, and found that it anyway impossible with current JsonSerializer implementation because of the boxing.
(because if i make my types i need to serialize classes, then i get rid of boxing, but instances i need to create and serialize becoming the source of allocations).
Configuration
Tested on .NET7 and .NET8
Data
Any generic or non-generic struct with public properties/fields, for example PointF or something like:
or
Analysis
Obviously, boxing happens (at least) in ObjectDefaultConverter, method OnTryWrite:
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectDefaultConverter.cs
Line 313 in 479df47
Wouldn't it be good to make this and related code boxing-free?
The text was updated successfully, but these errors were encountered: