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

Improve Guid parsing performance for "D", "N", "B", and "P" #55792

Merged
merged 2 commits into from
Jul 22, 2021

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jul 16, 2021

Method Toolchain Mean Error StdDev Ratio
D \main\corerun.exe 69.87 ns 0.644 ns 0.538 ns 1.00
D \pr\corerun.exe 38.21 ns 0.097 ns 0.086 ns 0.55
N \main\corerun.exe 58.43 ns 0.838 ns 0.700 ns 1.00
N \pr\corerun.exe 37.97 ns 0.207 ns 0.173 ns 0.65
B \main\corerun.exe 69.52 ns 0.337 ns 0.281 ns 1.00
B \pr\corerun.exe 39.20 ns 0.190 ns 0.169 ns 0.56
P \main\corerun.exe 69.50 ns 0.260 ns 0.243 ns 1.00
P \pr\corerun.exe 39.92 ns 0.139 ns 0.116 ns 0.57
InvalidD \main\corerun.exe 73.47 ns 0.656 ns 0.614 ns 1.00
InvalidD \pr\corerun.exe 42.47 ns 0.142 ns 0.133 ns 0.58
WeirdD \main\corerun.exe 68.45 ns 0.214 ns 0.190 ns 1.00
WeirdD \pr\corerun.exe 102.44 ns 0.643 ns 0.537 ns 1.50
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;

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

    private string _d, _b, _p, _n;
    private string _invalidD;
    private string _weirdD;

    [GlobalSetup]
    public void Setup()
    {
        Guid g = Guid.NewGuid();
        _d = g.ToString("D");
        _b = g.ToString("B");
        _p = g.ToString("P");
        _n = g.ToString("N");
        _invalidD = _d[..^1] + "Z";
        _weirdD = "+" + _d[1..];
    }

    [Benchmark] public bool D() => Guid.TryParseExact(_d, "D", out _);
    [Benchmark] public bool N() => Guid.TryParseExact(_n, "N", out _);
    [Benchmark] public bool B() => Guid.TryParseExact(_b, "B", out _);
    [Benchmark] public bool P() => Guid.TryParseExact(_p, "P", out _);
    [Benchmark] public bool InvalidD() => Guid.TryParseExact(_invalidD, "D", out _);
    [Benchmark] public bool WeirdD() => Guid.TryParseExact(_weirdD, "D", out _);
}

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoub
Copy link
Member Author

@uweigand, I would not be at all surprised if I got the endianness wrong here. Can you take a look?

@ghost
Copy link

ghost commented Jul 20, 2021

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

Issue Details
Method Toolchain Mean Error StdDev Ratio
D \main\corerun.exe 69.87 ns 0.644 ns 0.538 ns 1.00
D \pr\corerun.exe 38.21 ns 0.097 ns 0.086 ns 0.55
N \main\corerun.exe 58.43 ns 0.838 ns 0.700 ns 1.00
N \pr\corerun.exe 37.97 ns 0.207 ns 0.173 ns 0.65
B \main\corerun.exe 69.52 ns 0.337 ns 0.281 ns 1.00
B \pr\corerun.exe 39.20 ns 0.190 ns 0.169 ns 0.56
P \main\corerun.exe 69.50 ns 0.260 ns 0.243 ns 1.00
P \pr\corerun.exe 39.92 ns 0.139 ns 0.116 ns 0.57
InvalidD \main\corerun.exe 73.47 ns 0.656 ns 0.614 ns 1.00
InvalidD \pr\corerun.exe 42.47 ns 0.142 ns 0.133 ns 0.58
WeirdD \main\corerun.exe 68.45 ns 0.214 ns 0.190 ns 1.00
WeirdD \pr\corerun.exe 102.44 ns 0.643 ns 0.537 ns 1.50
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;

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

    private string _d, _b, _p, _n;
    private string _invalidD;
    private string _weirdD;

    [GlobalSetup]
    public void Setup()
    {
        Guid g = Guid.NewGuid();
        _d = g.ToString("D");
        _b = g.ToString("B");
        _p = g.ToString("P");
        _n = g.ToString("N");
        _invalidD = _d[..^1] + "Z";
        _weirdD = "+" + _d[1..];
    }

    [Benchmark] public bool D() => Guid.TryParseExact(_d, "D", out _);
    [Benchmark] public bool N() => Guid.TryParseExact(_n, "N", out _);
    [Benchmark] public bool B() => Guid.TryParseExact(_b, "B", out _);
    [Benchmark] public bool P() => Guid.TryParseExact(_p, "P", out _);
    [Benchmark] public bool InvalidD() => Guid.TryParseExact(_invalidD, "D", out _);
    [Benchmark] public bool WeirdD() => Guid.TryParseExact(_weirdD, "D", out _);
}
Author: stephentoub
Assignees: -
Labels:

area-System.Runtime

Milestone: -

@stephentoub
Copy link
Member Author

@tannergooding and/or @GrabYourPitchforks, could you help with a review here? Thanks.

Copy link
Member

@GrabYourPitchforks GrabYourPitchforks left a comment

Choose a reason for hiding this comment

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

Left a comment with potential for even further optimization, but the logic LGTM!

@DrewScoggins
Copy link
Member

We are seeing this improvement in the lab as well.

dotnet/perf-autofiling-issues#27

@stephentoub
Copy link
Member Author

Excellent :)

@DrewScoggins
Copy link
Member

Also improved on Arm64. dotnet/perf-autofiling-issues#75

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

Successfully merging this pull request may close these issues.

6 participants