-
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
Manually resolving JsonTypeInfo
+ Serialize methods
slower than call Serializer
directly
#80750
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionWhile working on update ASP.NET Core to use aot/trimmer-safe using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.CsProj;
using BenchmarkDotNet.Toolchains.DotNetCli;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
[MemoryDiagnoser]
public class JsonTypeInfoBenchmark
{
private Poco _pocoInstance = new Poco();
private JsonSerializerOptions _options = new JsonSerializerOptions() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() };
[Benchmark]
public string SerializerWithTypeInfo()
{
var typeInfo = _options.GetTypeInfo(typeof(Poco));
return JsonSerializer.Serialize(_pocoInstance, typeInfo);
}
[Benchmark]
public string Serialize()
{
return JsonSerializer.Serialize(_pocoInstance, _options);
}
public class Poco { }
} DataBenchmarkDotNet=v0.13.4, OS=Windows 11 (10.0.22623.1095)
11th Gen Intel Core i7-11850H 2.50GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=8.0.100-alpha.1.23061.8
[Host] : .NET 8.0.0 (8.0.23.5802), X64 RyuJIT AVX2
Job-SZDHAS : .NET 8.0.0 (8.0.23.6702), X64 RyuJIT AVX2
Toolchain=.NET 8.0
AnalysisBoth, ASP.NET Core benchmarks and this simple benchmark, try to serialize the same type (eg.: Line 103 in bd3e1f5
|
The Lines 92 to 107 in 3763345
Hitting that cache saves a lookup to the Even though we could extend the same caching scheme to |
Description
While working on update ASP.NET Core to use aot/trimmer-safe
S.T.J.Serializer
methods I found some performance differences between calling the serializer methods directly in comparison with call theoptions.GetTypeInfo
and provide the obtainedTypeInfo
to the serializer method.Data
Analysis
Both, ASP.NET Core benchmarks and this simple benchmark, try to serialize the same type (eg.:
Poco
) multiple times and, I did not have a chance to do a deep investigation, but we are missing cache of thelastTypeInfo
when theJsonTypeInfo
is obtained manually might be related the performance difference or maybe it is unrelated.runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
Line 103 in bd3e1f5
The text was updated successfully, but these errors were encountered: