Improve deserialization perf for case-insensitive and missing-property cases#35848
Merged
steveharter merged 2 commits intodotnet:masterfrom May 11, 2020
Merged
Improve deserialization perf for case-insensitive and missing-property cases#35848steveharter merged 2 commits intodotnet:masterfrom
steveharter merged 2 commits intodotnet:masterfrom
Conversation
|
Tagging subscribers to this area: @jozkee |
pranavkm
reviewed
May 5, 2020
src/libraries/System.Text.Json/src/System/Text/Json/JsonEncodedText.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
layomia
reviewed
May 6, 2020
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.Cache.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PropertyRef.cs
Outdated
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment.
Could these be internal properties instead?
...ystem.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs
Outdated
Show resolved
Hide resolved
stephentoub
reviewed
May 7, 2020
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.Cache.cs
Outdated
Show resolved
Hide resolved
stephentoub
reviewed
May 7, 2020
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.Cache.cs
Outdated
Show resolved
Hide resolved
steveharter
commented
May 7, 2020
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.cs
Outdated
Show resolved
Hide resolved
layomia
approved these changes
May 7, 2020
Contributor
There was a problem hiding this comment.
nit: we should assert that jsonPropertyInfo.NameAsString and jsonParameterInfo.NameAsString are equal (I can do this in a follow up)
Contributor
Author
|
Test failures in runtime |
This was referenced May 11, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
~1.75x faster deserialization of a simple POCO when:
JsonSerializerOptions.PropertyNameCaseInsensitiveis true AND[JsonPropertyName], or the property naming policy) ANDIn addition, similar perf results for property-misses cases (property names in JSON do not match any property).
Also there are less cache memory allocations for property names. This also has a startup CPU improvement - a simple POCO resulted in a .5ms startup improvements on first deserialization (~23.5ms to ~23.0ms). Private bytes savings were also verified using Process Explorer (very long property names were used).
Fixes #30789 and the increased startup perf and reduced memory consumption helps with 5.0 serialization goals per dotnet/designs#113.
In addition, there are less stack-based allocations for case-insensitive and missing-property scenarios (shown below in the benchmarks).
This may help with ASP.NET deserialization scenarios since they enable case-insensitive by default (cc @pranavkm). However, since ASP.NET also uses a camel-casing property naming policy, the benefit will not occur if the incoming JSON exactly matches what was produced by the camel-casing policy, as would be the case if all serialization occurs with System.Text.Json.
No measurable perf regressions on existing deserialization benchmarks (within +- 3%). Todo: add a case-insensitive and missing-property benchmark.
The main CPU benefit comes from avoiding the dictionary-based lookup for case-insensitive and missing-property scenarios and instead use the array-backed cache. The previous code would also unnecessarily grow and max out that array (64 elements) in certain cases before using the dictionary.
During testing, it was found that "missing properties" in JSON that are added to extension data did not properly support JsonPath semantics (used when a
JsonExceptionis thrown to report the invalid property). That was fixed and a test added.Below are benchmarks for a simple 4-property POCO test class that has property names > 7 bytes.