-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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.JsonProperty allocated a string in WriteTo() #88767
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionSystem.Text.Json.JsonProperty.WriteTo() uses the Name property to write name and value. This realises the property name as a string, causing an allocation. runtime/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs Line 120 in 0a50fef
It would be preferable to write the property name using the underlying JsonElement and avoid the allocation.
|
Thanks for reporting this. Would you be interested in contributing a PR that fixes the issue? |
i would try to implement a fix. |
System.Text.Json.JsonProperty.WriteTo uses get_Name, calling JsonElement.GetPropertyName() which would allocate a string. Use ReadOnlySpan<byte> from the underlying UTF8 json, when possible, by adding helper methods into JsonDocument & JsonElement. Fix dotnet#88767
…eTo` (#90074) * Avoid string allocation in WriteTo when possible. System.Text.Json.JsonProperty.WriteTo uses get_Name, calling JsonElement.GetPropertyName() which would allocate a string. Use ReadOnlySpan<byte> from the underlying UTF8 json, when possible, by adding helper methods into JsonDocument & JsonElement. Fix #88767 * Avoid alloc in unescaping & escaping. Current code unescapes & escapes property names and uses ToArray. Avoid alloc by adding internal GetRaw/WriteRaw methods. * Fix bugs on escaped property names Original code doesn't handle GetRaw/WriteRaw on escaped property names correctly. * Change IndexOf to Contains if possible. * Further avoid alloc by inlining GetUnescapedSpan Allocations are further avoided when the property name is shorter than JsonConstants.StackallocByteThreshold, by inlining JsonReaderHelper.GetUnescapedSpan. * Move writing logic to JsonElement; Shorten names of new methods; Add a test of writing out special names. * Move logic into JsonDocument * fix format * fix format 2 * improve comment * removed unused stub * added assertion * removed unnecessary test
Description
System.Text.Json.JsonProperty.WriteTo() uses the Name property to write name and value.
This realises the property name as a string, causing an allocation.
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonProperty.cs
Line 120 in 0a50fef
It would be preferable to write the property name using the underlying JsonElement and avoid the allocation.
The text was updated successfully, but these errors were encountered: