Implements some micro-optimizations
All checks were successful
Build / Build (push) Successful in 51s
All checks were successful
Build / Build (push) Successful in 51s
This commit is contained in:
@@ -107,4 +107,21 @@ public class RandomImpl : IRandom
|
||||
throw new ArgumentException("List cannot be empty.", nameof(list));
|
||||
return list[GetInt(list.Count)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random <see cref="Guid"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The goal of this method is to create a new unique identifier that can be used for various purposes,
|
||||
/// without having to rely on the system's built-in GUID generation. The built-in GUID generation
|
||||
/// can be slow (see also: https://github.com/dotnet/runtime/issues/13628)
|
||||
/// </remarks>
|
||||
public Guid NewGuid()
|
||||
{
|
||||
var guidBytes = GuidCache.Value.AsSpan();
|
||||
_random.NextBytes(guidBytes);
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
|
||||
private static readonly ThreadLocal<byte[]> GuidCache = new(() => new byte[16]);
|
||||
}
|
||||
Reference in New Issue
Block a user