Fix Http.Json serialization performance by using static options #35040
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.
Fixes #34440
There was a considerable performance regression compared against
Microsoft.AspNetCore.Blazor.HttpClientcaused by always instantiating aJsonSerializerOptionsobject when not provided, which means thatSystem.Net.Http.Jsonwas not actively using the caching mechanism of theJsonSerializerfor types and converters.I got the following results using the aspnet/Benchmarks repo to measure the amount of requests made to a simple WeatherForecast app that returns a
List<WeatherForecast>.The scenario keeps a thread sending Get requests for the specified duration (15s) after a specified warm-up time lapse (15s).
The benchmarks used can be found in https://github.com/Jozkee/HttpJsonBenchmarks.
I ran the scenarios 5 times each in order to get a larger sample.
I also ensured that the same version of System.Text.Json was being used (4.7.1 latest stable).
Results
Microsoft.AspNetCore.Blazor.HttpClient Version 3.2.0-preview3.20168.3[client] 55458 iterations in 15s
[client] 53794 iterations in 15s
[client] 52779 iterations in 15s
[client] 53429 iterations in 15s
[client] 51582 iterations in 15s
System.Net.Http.Json Version 3.2.0-preview3.20175.8Before fix[client] 13159 iterations in 15s
[client] 12441 iterations in 15s
System.Net.Http.Json 3.2.0-dev(Private build) After fix[client] 55352 iterations in 15s
[client] 51981 iterations in 15s
[client] 53777 iterations in 15s
[client] 52569 iterations in 15s
[client] 52655 iterations in 15s
Note: There are further optimizations that can be made/be explored, specially on the Post/Put methods but those transcend the quick gain of using the static options for cache on serialization.
cc @ericstj @joperezr @layomia