Skip to content

Track required properties with ValueBitArray #116165

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

Closed

Conversation

PranavSenthilnathan
Copy link
Member

No description provided.

@PranavSenthilnathan

This comment was marked as resolved.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@PranavSenthilnathan
Copy link
Member Author

@EgorBot -arm -amd

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

BenchmarkSwitcher.FromAssembly(typeof(RequiredJson).Assembly).Run(args);

public class RequiredJson
{
    private string? json;

    [GlobalSetup]
    public void Setup()
    {
        json = "{ \"Foo\": \"qux\", \"Bar\": \"qux\" }";
    }

    [Benchmark]
    public ClassWithRequired DeserializeClassWithRequired() =>
        JsonSerializer.Deserialize<ClassWithRequired>(json!)!;

    [Benchmark]
    public ClassWithoutRequired DeserializeClassWithoutRequired() =>
        JsonSerializer.Deserialize<ClassWithoutRequired>(json!)!;

    public class ClassWithRequired
    {
        [JsonRequired]
        public string? Foo { get; set; }
        public string? Bar { get; set; }
        public string? Baz { get; set; }
    }

    public class ClassWithoutRequired
    {
        public string? Foo { get; set; }
        public string? Bar { get; set; }
        public string? Baz { get; set; }
    }
}

@eiriktsarpalis
Copy link
Member

Is there a way to command EgorBot to also include allocations? cc @EgorBo

@PranavSenthilnathan
Copy link
Member Author

@EgorBot -amd

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

BenchmarkSwitcher.FromAssembly(typeof(RequiredJson).Assembly).Run(args);

[MemoryDiagnoser]
public class RequiredJson
{
    private string? json;

    [GlobalSetup]
    public void Setup()
    {
        json = "{ \"Foo\": \"qux\", \"Bar\": \"qux\" }";
    }

    [Benchmark]
    public ClassWithRequired DeserializeClassWithRequired() =>
        JsonSerializer.Deserialize<ClassWithRequired>(json!)!;

    [Benchmark]
    public ClassWithoutRequired DeserializeClassWithoutRequired() =>
        JsonSerializer.Deserialize<ClassWithoutRequired>(json!)!;

    public class ClassWithRequired
    {
        [JsonRequired]
        public string? Foo { get; set; }
        public string? Bar { get; set; }
        public string? Baz { get; set; }
    }

    public class ClassWithoutRequired
    {
        public string? Foo { get; set; }
        public string? Bar { get; set; }
        public string? Baz { get; set; }
    }
}

@PranavSenthilnathan
Copy link
Member Author

This is what I get locally:


BenchmarkDotNet v0.14.1-nightly.20250107.205, Windows 11 (10.0.26100.4061)
AMD Ryzen 9 9950X 4.30GHz, 1 CPU, 32 logical and 16 physical cores
.NET SDK 10.0.100-preview.3.25201.16
  [Host]     : .NET 10.0.0 (10.0.25.17105), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-PTIRIP : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-AHYCXR : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

PowerPlanMode=00000000-0000-0000-0000-000000000000  IterationTime=250ms  MaxIterationCount=20  
MinIterationCount=15  WarmupCount=1  

Method Toolchain Mean Error StdDev Median Min Max Ratio Gen0 Allocated Alloc Ratio
DeserializeClassWithRequired main 114.6 ns 1.18 ns 1.11 ns 114.6 ns 112.5 ns 116.1 ns 1.06 0.0100 168 B 1.62
DeserializeClassWithRequired PR 108.2 ns 0.58 ns 0.48 ns 108.2 ns 107.4 ns 108.8 ns 1.00 0.0060 104 B 1.00
DeserializeClassWithoutRequired main 107.7 ns 0.44 ns 0.41 ns 107.6 ns 107.0 ns 108.5 ns 1.01 0.0060 104 B 1.00
DeserializeClassWithoutRequired PR 106.9 ns 0.99 ns 0.83 ns 106.8 ns 106.0 ns 108.9 ns 1.00 0.0061 104 B 1.00

private const BackingType One = 1;

private readonly BitArray? _allocatedBitArray;
private BackingType _inlineBitArray;
Copy link
Member

Choose a reason for hiding this comment

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

BitVector32?

Copy link
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants