Skip to content

Vectorize Guid.ToString #81650

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

Merged
merged 5 commits into from
Feb 5, 2023
Merged

Vectorize Guid.ToString #81650

merged 5 commits into from
Feb 5, 2023

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 5, 2023

I tried to keep the implementation compact - around +66 lines to handle D, N, P and B formats (except X basically, which is rarely-used). For both x64 and Arm64.

public static IEnumerable<Guid> TestData()
{
    yield return new Guid("{483BC244-A481-4D29-986F-8C7D647AA20D}");
}

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringD(Guid guid) => guid.ToString(); // Default - "D"

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringN(Guid guid) => guid.ToString("N");

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringB(Guid guid) => guid.ToString("B");

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringP(Guid guid) => guid.ToString("P");
Method Toolchain guid Mean Error StdDev Ratio
ToStringD \Core_Root\corerun.exe 483bc(...)aa20d [36] 13.26 ns 0.035 ns 0.033 ns 0.57
ToStringD \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 23.27 ns 0.081 ns 0.071 ns 1.00
ToStringN \Core_Root\corerun.exe 483bc(...)aa20d [36] 12.69 ns 0.072 ns 0.056 ns 0.58
ToStringN \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 22.00 ns 0.048 ns 0.040 ns 1.00
ToStringB \Core_Root\corerun.exe 483bc(...)aa20d [36] 14.45 ns 0.083 ns 0.065 ns 0.59
ToStringB \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 24.29 ns 0.167 ns 0.139 ns 1.00
ToStringP \Core_Root\corerun.exe 483bc(...)aa20d [36] 15.11 ns 0.247 ns 0.231 ns 0.61
ToStringP \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 24.66 ns 0.065 ns 0.051 ns 1.00

The main motivation was to slightly speed up JSON serialization for Guid fields, e.g.:

record Entity(Guid Id, string Name);

string json = JsonSerializer.Serialize(entity, EntityJsonContext.Default.Entity);

Around 5% improvement here.

@ghost
Copy link

ghost commented Feb 5, 2023

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.

@ghost ghost assigned EgorBo Feb 5, 2023
@EgorBo
Copy link
Member Author

EgorBo commented Feb 5, 2023

@stephentoub @MihaZupan anything else?
I'll simplify this impl even further by sharing code with HexConverter (and, probably, Utf8Formatter.Guid) and will move hand-made Vector128 crossplat helpers to Vector128 itself (clean up) in my next PR

@ghost
Copy link

ghost commented Feb 5, 2023

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

Issue Details

I tried to keep the implementation compact - around +66 lines to handle D, N, P and B formats (except X basically, which is rarely-used). For both x64 and Arm64.

public static IEnumerable<Guid> TestData()
{
    yield return new Guid("{483BC244-A481-4D29-986F-8C7D647AA20D}");
}

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringD(Guid guid) => guid.ToString(); // Default - "D"

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringN(Guid guid) => guid.ToString("N");

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringB(Guid guid) => guid.ToString("B");

[Benchmark]
[ArgumentsSource(nameof(TestData))]
public string ToStringP(Guid guid) => guid.ToString("P");
Method Toolchain guid Mean Error StdDev Ratio
ToStringD \Core_Root\corerun.exe 483bc(...)aa20d [36] 13.26 ns 0.035 ns 0.033 ns 0.57
ToStringD \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 23.27 ns 0.081 ns 0.071 ns 1.00
ToStringN \Core_Root\corerun.exe 483bc(...)aa20d [36] 12.69 ns 0.072 ns 0.056 ns 0.58
ToStringN \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 22.00 ns 0.048 ns 0.040 ns 1.00
ToStringB \Core_Root\corerun.exe 483bc(...)aa20d [36] 14.45 ns 0.083 ns 0.065 ns 0.59
ToStringB \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 24.29 ns 0.167 ns 0.139 ns 1.00
ToStringP \Core_Root\corerun.exe 483bc(...)aa20d [36] 15.11 ns 0.247 ns 0.231 ns 0.61
ToStringP \Core_Root_base\corerun.exe 483bc(...)aa20d [36] 24.66 ns 0.065 ns 0.051 ns 1.00

The main motivation was to slightly speed up JSON serialization for Guid fields, e.g.:

record Entity(Guid Id, string Name);

string json = JsonSerializer.Serialize(entity, EntityJsonContext.Default.Entity);

Around 5% improvement here.

Author: EgorBo
Assignees: EgorBo
Labels:

area-System.Runtime

Milestone: -

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

will move hand-made Vector128 crossplat helpers to Vector128 itself (clean up) in my next PR

You can skip the Shuffle (#80963 already does it) -- I'll rebase once your PR is merged.

@EgorBo EgorBo merged commit b708292 into dotnet:main Feb 5, 2023
@EgorBo EgorBo deleted the vectorize-guid branch February 5, 2023 18:55
@EgorBo
Copy link
Member Author

EgorBo commented Feb 6, 2023

image

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.

5 participants