1
Slight .NET 9 improvement for System.Text.Json
Date Added (UTC):
25 Apr 2024 @ 15:30
Date Updated (UTC):25 Apr 2024 @ 15:30
.NET Version(s): Tag(s):
Added By:
Senior Software Engineer
Benchmark Results:
Benchmark Code:
Originally imported from :
https://gist.github.com/sa-es-ir/577e8ce4ba6646c68e78d99117b76d8aon 25 Apr 2024 @ 15:30 (UTC) .
The original benchmark may have changed.
https://gist.github.com/sa-es-ir/577e8ce4ba6646c68e78d99117b76d8a
The original benchmark may have changed.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using System.Text.Json;
namespace Benchmarks;
[MemoryDiagnoser(false)]
[HideColumns(Column.RatioSD, Column.AllocRatio)]
[SimpleJob(RuntimeMoniker.Net80, baseline: true)]
[SimpleJob(RuntimeMoniker.Net90)]
public class SystemTextJsonBenchmark
{
private SampleModel model;
private string json;
[GlobalSetup]
public void GlobalSetup()
{
model = new SampleModel();
json = "{\"Id\":1,\"UniqueId\":\"fcd03978-0562-4166-bad9-55605e9d5ace\",\"Name\":\"test_name\",\"CreatedAt\":\"2024-04-25T09:39:10.6768869Z\"}";
}
[Benchmark]
public void Serialize() => JsonSerializer.Serialize(model);
[Benchmark]
public void Deserialize() => JsonSerializer.Deserialize<SampleModel>(json);
}
public class SampleModel
{
public int Id { get; set; } = 1;
public Guid UniqueId { get; set; } = Guid.NewGuid();
public string Name { get; set; } = "test_name";
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
Powered by SharpLab
// .NET 8
public void Serialize()
{
JsonSerializer.Serialize(model);
}
// .NET 8
public void Deserialize()
{
JsonSerializer.Deserialize<SampleModel>(json);
}
Powered by SharpLab
// .NET 8
.method public hidebysig
instance void Serialize () cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor(int32, string) = (
01 00 23 00 00 00 01 5f 00 00
)
// Method begins at RVA 0x2068
// Code size 14 (0xe)
.maxstack 8
// sequence point: (line 36, col 32) to (line 36, col 63) in _
IL_0000: ldarg.0
IL_0001: ldfld class Benchmarks.SampleModel Benchmarks.SystemTextJsonBenchmark::model
IL_0006: ldnull
IL_0007: call string [System.Text.Json]System.Text.Json.JsonSerializer::Serialize<class Benchmarks.SampleModel>(!!0, class [System.Text.Json]System.Text.Json.JsonSerializerOptions)
IL_000c: pop
IL_000d: ret
}
// .NET 8
.method public hidebysig
instance void Deserialize () cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor(int32, string) = (
01 00 26 00 00 00 01 5f 00 00
)
// Method begins at RVA 0x2077
// Code size 14 (0xe)
.maxstack 8
// sequence point: (line 39, col 34) to (line 39, col 79) in _
IL_0000: ldarg.0
IL_0001: ldfld string Benchmarks.SystemTextJsonBenchmark::json
IL_0006: ldnull
IL_0007: call !!0 [System.Text.Json]System.Text.Json.JsonSerializer::Deserialize<class Benchmarks.SampleModel>(string, class [System.Text.Json]System.Text.Json.JsonSerializerOptions)
IL_000c: pop
IL_000d: ret
}
Powered by SharpLab
|
|
Benchmark Description:
There is a very slight improvement for Serialize and Deserialize in .NET 9 for System.Text.Json, Still memory allocation is the same as .NET 8.
The provided benchmark code is designed to measure the performance of the `System.Text.Json` library in .NET for serializing and deserializing objects. The setup includes configurations for running the benchmarks under two different .NET runtime versions, specifically .NET 8.0 and .NET 9.0, allowing for a comparison between these versions regarding the performance of JSON operations.
### General Setup
- **.NET Versions:** The benchmarks are set to run under .NET 8.0 (as the baseline) and .NET 9.0. This setup is intended to compare the performance improvements or regressions between these two versions of the .NET runtime.
- **Memory Diagnoser:** The `MemoryDiagnoser` attribute is used with `false` to disable it, indicating that memory allocation metrics are not the primary focus of this benchmark.
- **Columns:** The `HideColumns` attribute is used to exclude specific columns related to standard deviation ratio and allocation ratio from the results, focusing the output on the primary performance metrics.
- **Jobs:** Two `SimpleJob` configurations are defined for running the benchmarks under the specified .NET runtime versions.
### Benchmark Methods
#### 1. Serialize Method
- **Purpose:** This method measures the performance of serializing a `SampleModel` object to a JSON string using `System.Text.Json.JsonSerializer`.
- **Performance Aspect:** It tests how quickly the `JsonSerializer` can convert an object instance into a JSON string representation. This operation is crucial in applications that frequently serialize data to send over networks, store in files, or communicate between different parts of an application.
- **Expected Insights:** Running this benchmark will provide insights into the serialization speed and any performance improvements or regressions between .NET 8.0 and .NET 9.0. Faster serialization times are generally desired, especially in high-throughput or latency-sensitive applications.
#### 2. Deserialize Method
- **Purpose:** This method evaluates the performance of deserializing a JSON string back into a `SampleModel` object.
- **Performance Aspect:** It focuses on the efficiency of the `JsonSerializer` in parsing a JSON string and creating an object instance with the corresponding data. Deserialization is a common operation when receiving JSON data from web services, files, or external systems, making its performance critical for many applications.
- **Expected Insights:** The benchmark will reveal how quickly the .NET runtime can parse and instantiate objects from JSON strings. Improvements in deserialization times can significantly benefit applications that rely on consuming JSON-formatted data, indicating more efficient data processing capabilities between different .NET versions.
### Conclusion
By running these benchmarks, developers can gain valuable insights into the performance characteristics of JSON serialization and deserialization operations in different .NET runtime versions. This information can guide optimization efforts, framework version decisions, and understanding of how .NET's JSON processing capabilities evolve over time. The specific focus on serialization and deserialization performance is important due to the widespread use of JSON for data interchange in modern software development.