Skip to content

Guid.CreateVersion7() could be faster #106377

@yaakov-h

Description

@yaakov-h

Description

I got nerd-sniped on Mastodon a while back into build a highly performant UUIDv7 implementation for .NET.

I haven't published it as a library yet or anything like that.

I just saw that .NET 9 Preview 7 adds Guid.CreateVersion7() which does the same thing, but when I benchmarked it the runtime version came out as almost 3x as slow as my version.

The problem is my version depends on being smart with RandomNumberGenerator.Fill, and based on this comment it seems that random number generation is not available in this part of the runtime and what is available is slower

However I think there may be an oversight here.

RandomNumberGenerator.Fill is significantly faster on macOS, but about 20% slower on Windows. I suspect this is due to the split between CoCreateGuid() on Windows vs Interop.GetCryptographicallySecureRandomBytes() on Unix.

I'll post additional benchmarks below.

Would you be open to me doing platform-specific implementations to speed up Guid.CreateVersion7() (and if I can get benchmarks to back it up, maybe Guid.NewGuid() too?) on non-Windows platforms?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimein-prThere is an active PR which will close this issue when it is mergedneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsiderationtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions