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

JsonSerializer.Serialize<T>(Utf8JsonWriter, T) regression in .NET 6 #56993

Closed
stephentoub opened this issue Aug 6, 2021 · 3 comments · Fixed by #57327
Closed

JsonSerializer.Serialize<T>(Utf8JsonWriter, T) regression in .NET 6 #56993

stephentoub opened this issue Aug 6, 2021 · 3 comments · Fixed by #57327
Assignees
Labels
Milestone

Comments

@stephentoub
Copy link
Member

Benchmark:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.IO;
using System.Text.Json;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    private Data _data = new Data { Name = "Stephen", Address = "Here" };
    private MemoryStream _stream = new MemoryStream();
    private Utf8JsonWriter _writer;

    [GlobalSetup]
    public void Setup()
    {
        _writer = new Utf8JsonWriter(_stream);
    }

    [Benchmark(OperationsPerInvoke = 1000)]
    public void Serialize()
    {
        for (int i = 0; i < 1000; i++)
        {
            _stream.Position = 0;
            _writer.Reset();
            JsonSerializer.Serialize(_writer, _data);
        }
    }

    internal class Data
    {
        public string Name;
        public string Address;
    }
}

On my machine, running this with:

dotnet run -c Release -f net5.0 --filter ** --join --runtimes net5.0 net6.0

with .NET 5.0.8 and .NET 6.0.0 (6.0.21.40103), I get these results:

Method Job Runtime Toolchain Mean Error StdDev Ratio
Serialize Job-DDTUUM .NET 5.0 net5.0 85.54 ns 0.487 ns 0.407 ns 1.00
Serialize Job-EGSUAL .NET 6.0 net6.0 106.80 ns 0.328 ns 0.273 ns 1.25

For me at least, that 25% regression is very consistent.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Aug 6, 2021
@ghost
Copy link

ghost commented Aug 6, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Benchmark:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.IO;
using System.Text.Json;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    private Data _data = new Data { Name = "Stephen", Address = "Here" };
    private MemoryStream _stream = new MemoryStream();
    private Utf8JsonWriter _writer;

    [GlobalSetup]
    public void Setup()
    {
        _writer = new Utf8JsonWriter(_stream);
    }

    [Benchmark(OperationsPerInvoke = 1000)]
    public void Serialize()
    {
        for (int i = 0; i < 1000; i++)
        {
            _stream.Position = 0;
            _writer.Reset();
            JsonSerializer.Serialize(_writer, _data);
        }
    }

    internal class Data
    {
        public string Name;
        public string Address;
    }
}

On my machine, running this with:

dotnet run -c Release -f net5.0 --filter ** --join --runtimes net5.0 net6.0

with .NET 5.0.8 and .NET 6.0.0 (6.0.21.40103), I get these results:

Method Job Runtime Toolchain Mean Error StdDev Ratio
Serialize Job-DDTUUM .NET 5.0 net5.0 85.54 ns 0.487 ns 0.407 ns 1.00
Serialize Job-EGSUAL .NET 6.0 net6.0 106.80 ns 0.328 ns 0.273 ns 1.25

For me at least, that 25% regression is very consistent.

Author: stephentoub
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@danmoseley danmoseley added this to the 6.0.0 milestone Aug 6, 2021
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Aug 7, 2021
@steveharter steveharter self-assigned this Aug 9, 2021
@steveharter steveharter added the tenet-performance Performance related issue label Aug 9, 2021
@steveharter
Copy link
Member

I can repro this; this is a regression mostly from refactoring done in #53212.

However, the sample POCO above:

  internal class Data
  {
      public string Name;
      public string Address;
  }

only measures serializer overhead, since fields are not serialized by default. [JsonInclude] must be used for fields.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 13, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 15, 2021
@steveharter
Copy link
Member

Please re-verify based on #57327. Thanks

@ghost ghost locked as resolved and limited conversation to collaborators Sep 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants