Implements some micro-optimizations
All checks were successful
Build / Build (push) Successful in 51s

This commit is contained in:
2025-07-05 15:46:32 +02:00
parent c795f20e54
commit 8a857ed232
11 changed files with 80 additions and 21 deletions

View File

@@ -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]);
}