Skip to content
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

Add System.Text.Json to JSON serializer benchmarks #2740

Merged

Conversation

eiriktsarpalis
Copy link
Member

Adds System.Text.Json (reflection & source gen) to the benchmark suite comparing .NET JSON serializers. Here's a few representative results using IndexViewModel values:

String Serialization

Method Mean Error StdDev Median Min Max Gen0 Gen1 Allocated
SystemTextJson_Reflection 15.04 μs 0.832 μs 0.924 μs 14.82 μs 13.79 μs 16.71 μs 2.4789 0.1771 24.85 KB
SystemTextJson_SourceGen 10.51 μs 0.114 μs 0.096 μs 10.49 μs 10.35 μs 10.65 μs 2.4715 - 24.55 KB
JSON.NET 33.38 μs 1.074 μs 1.149 μs 33.09 μs 31.62 μs 36.20 μs 5.9487 1.1154 59.33 KB
Jil 39.15 μs 1.095 μs 1.172 μs 38.73 μs 37.49 μs 42.10 μs 5.7115 0.9791 56.7 KB
Utf8Json 26.08 μs 1.140 μs 1.313 μs 25.71 μs 24.51 μs 28.99 μs 2.4222 0.1938 24.55 KB

String Deserialization

Method Mean Error StdDev Median Min Max Gen0 Gen1 Allocated
SystemTextJson_Reflection 24.53 μs 0.437 μs 0.388 μs 24.65 μs 23.93 μs 25.31 μs 2.1980 0.2867 22.01 KB
SystemTextJson_SourceGen 24.23 μs 0.464 μs 0.388 μs 24.10 μs 23.82 μs 25.00 μs 2.1654 0.2953 22.01 KB
JSON.NET 53.29 μs 2.270 μs 2.614 μs 53.07 μs 50.14 μs 58.43 μs 3.0352 0.4047 31.31 KB
Jil 41.49 μs 0.542 μs 0.453 μs 41.63 μs 40.56 μs 42.18 μs 2.3217 0.1658 23.49 KB
Utf8Json 39.39 μs 2.146 μs 2.471 μs 39.98 μs 36.40 μs 42.61 μs 3.3665 0.2927 33.87 KB

Stream Serialization

Method Mean Error StdDev Median Min Max Gen0 Allocated
DataContractJsonSerializer 81.58 μs 4.635 μs 5.337 μs 81.90 μs 75.71 μs 93.49 μs - 2416 B
SystemTextJson_Reflection 14.04 μs 0.648 μs 0.746 μs 13.93 μs 12.99 μs 15.38 μs - 464 B
SystemTextJson_SourceGen 14.05 μs 0.858 μs 0.989 μs 13.69 μs 13.06 μs 16.27 μs - 464 B
JSON.NET 33.31 μs 1.696 μs 1.953 μs 32.63 μs 30.73 μs 37.13 μs 0.1249 2448 B
Jil 40.21 μs 2.319 μs 2.670 μs 40.84 μs 36.95 μs 47.08 μs - 96 B
Utf8Json 24.51 μs 1.040 μs 1.156 μs 24.01 μs 23.42 μs 26.92 μs - -

Stream Deserialization

Method Mean Error StdDev Median Min Max Gen0 Gen1 Allocated
DataContractJsonSerializer 223.90 μs 2.331 μs 1.820 μs 223.84 μs 221.09 μs 226.39 μs 9.0726 1.0081 94.25 KB
SystemTextJson_Reflection 25.92 μs 1.005 μs 1.158 μs 25.56 μs 24.81 μs 28.44 μs 2.1930 0.2990 22.01 KB
SystemTextJson_SourceGen 25.27 μs 0.583 μs 0.599 μs 25.22 μs 24.32 μs 26.64 μs 2.1619 0.2948 22.01 KB
JSON.NET 56.12 μs 2.302 μs 2.651 μs 55.36 μs 53.00 μs 60.91 μs 3.4079 0.4260 34.27 KB
Jil 61.04 μs 1.036 μs 0.919 μs 60.84 μs 60.07 μs 62.32 μs 2.6918 0.2692 26.7 KB
Utf8Json 35.55 μs 0.341 μs 0.267 μs 35.55 μs 35.14 μs 35.98 μs 2.1067 0.2809 21.58 KB

Fix dotnet/runtime#78451.

<!-- this repo needs to build with netcoreapp3.1, so we can't use latest C# syntax -->
<LangVersion>8.0</LangVersion>
<EnableXlfLocalization>false</EnableXlfLocalization>
<LangVersion>9.0</LangVersion>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that source gen requires C# 9 since it is setting init-only properties. I've conditioned the lang version on the target framework as it seemed like the smallest possible fix, but I defer to maintainers on the right approach.

cc @adamsitnik

@eiriktsarpalis eiriktsarpalis force-pushed the add-system-text-json-to-serializers branch from 40a9e3d to 45e6a44 Compare November 18, 2022 13:48
@adamsitnik
Copy link
Member

@eiriktsarpalis currently this repo has no CI leg to verify that 3.1, 6.0 and 7.0 builds are still working. I've created #2732 to address that. Once I get it merged (somewhen next week), I am going to review this PR and merge it too.

@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Nov 19, 2022

@eiriktsarpalis currently this repo has no CI leg to verify that 3.1, 6.0 and 7.0 builds are still working. I've created #2732 to address that. Once I get it merged (somewhen next week), I am going to review this PR and merge it too.

FWIW I have checked locally and it compiles for all TFMs. Given that 3.1 is going out of support next month, perhaps we could just remove support for it altogether? We can then use a consistent language version for all TFMs.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good to me. Please take a look at my comments before merging the PR.

Thank you for adding more benchmarks @eiriktsarpalis !

eiriktsarpalis and others added 2 commits November 25, 2022 15:50
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance Benchmark
2 participants