You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling JsonSerializer.Serilaize with an options object instantiated inline (or from a local variable) results in an unexpectedly large performance penalty, compared to passing the options object from a class member. The performance penalty is in the order of 1 ms.
Lots of example code in migrating to System.Text.Json uses inline instantiation of the options object, meaning that many developers will get this performance hit without expecting it.
*_Default means that the options object was created with the default constructor, no properties where changed that could affect the serialization performance.
Just to make sure, I did a benchmark on instantiation of the options object too:
Since instantiating the options object with the default constructor takes well below 1 μs, it surely cannot account for the performance penalty in the benchmark above.
The text was updated successfully, but these errors were encountered:
The serializer undergoes a warm-up phase during the first (de)serialization of every type in the object graph when a new options instance is passed to it. This warm-up includes creating a cache of metadata it needs to perform (de)serialization: funcs to property getters, setters, ctor arguments, specified attributes etc. This metadata caches is stored in the options instance. This process is not cheap, and it is recommended to cache options instances for reuse on subsequent calls to the serializer to avoid unnecessarily undergoing the warm-up repeatedly.
Description
Calling
JsonSerializer.Serilaize
with an options object instantiated inline (or from a local variable) results in an unexpectedly large performance penalty, compared to passing the options object from a class member. The performance penalty is in the order of 1 ms.Lots of example code in migrating to System.Text.Json uses inline instantiation of the options object, meaning that many developers will get this performance hit without expecting it.
I have written a blog post with a more detailed description of the issue, including benchmarks.
https://dev.to/bjowes/keeping-system-text-json-lean-12jc
The source code for all the benchmarks can be found here
https://github.com/bjowes/SystemTextJson-Benchmark
Configuration
Has been tested on .Net Core 3.1.5 and 3.1.7
Windows 10, x64
See benchmarks below for specs
Regression?
Not known
Data
Benchmark of serialization of a small object (6 properties, where two of them contain objects with more properties):
*_Default means that the options object was created with the default constructor, no properties where changed that could affect the serialization performance.
Just to make sure, I did a benchmark on instantiation of the options object too:
Since instantiating the options object with the default constructor takes well below 1 μs, it surely cannot account for the performance penalty in the benchmark above.
The text was updated successfully, but these errors were encountered: