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 v7 performance on Unix #106525

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Guid.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,22 @@ public static unsafe Guid NewGuid()

return g;
}

private static unsafe Guid CreateRandomizedPartialVersion7()
{
Guid g;
Copy link
Member

Choose a reason for hiding this comment

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

is it ok for UUIDv7 that you use a non-initialized variable here so the upper 6 bytes will be zero or stack garbage? (since corelib is compiled with SkipLocalsInit)


byte* randomDataStartOffset = ((byte*)&g) + 6;
int randomDataLength = sizeof(Guid) - 6;

yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
#if !TARGET_WASI
Interop.GetCryptographicallySecureRandomBytes(randomDataStartOffset, randomDataLength);
#else
// TODOWASI: crypto secure random bytes
Interop.GetRandomBytes(randomDataStartOffset, randomDataLength);
#endif

return g;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ private static void ThrowForHr(int hr)
// we ever hit this condition.
throw new Exception() { HResult = hr };
}

private static Guid CreateRandomizedPartialVersion7()
{
// CoCreateGuid (above) is faster than Interop.BCrypt.BCryptGenRandom and does more than what we need, so
// we may as well use that here.
return NewGuid();
}
}
}
11 changes: 5 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ public Guid(string g)
/// </remarks>
public static Guid CreateVersion7(DateTimeOffset timestamp)
{
// NewGuid uses CoCreateGuid on Windows and Interop.GetCryptographicallySecureRandomBytes on Unix to get
// cryptographically-secure random bytes. We could use Interop.BCrypt.BCryptGenRandom to generate the random
// bytes on Windows, as is done in RandomNumberGenerator, but that's measurably slower than using CoCreateGuid.
// And while CoCreateGuid only generates 122 bits of randomness, the other 6 bits being for the version / variant
// fields, this method also needs those bits to be non-random, so we can just use NewGuid for efficiency.
Guid result = NewGuid();
// Version 7 contains two major components: a timestamp, and randomized data.
// This function call creates a GUID with _at least_ the randomized data set.
// On Windows this creates a full Version 4 GUID, as it is faster than just getting random data.
// On Unix this only fills the random data bytes and leaves the rest unset.
Guid result = CreateRandomizedPartialVersion7();

// 2^48 is roughly 8925.5 years, which from the Unix Epoch means we won't
// overflow until around July of 10,895. So there isn't any need to handle
Expand Down
Loading