Skip to content

Latest commit

 

History

History
110 lines (93 loc) · 4.04 KB

README.md

File metadata and controls

110 lines (93 loc) · 4.04 KB

FastGuid NuGet

10 times faster than Guid.NewGuid()

Static APIs

  • Guid FastGuid.NewGuid()

    • Returns a cryptographically random GUID.
    • ~10x faster than Guid.NewGuid().
  • FastGuid.Fill(Span<byte> data)

    • Fills a span with cryptographically strong random bytes.
    • ~5x faster for <512 bytes, otherwise calls RandomNumberGenerator.Fill()
  • Guid FastGuid.NewSqlServerGuid()

    • Returns new Guid optimized for use as a SQL-Server clustered key.
    • Guid structure is [8 random bytes] [8 bytes of SQL-Server-ordered DateTime.UtcNow]
    • Each Guid is sequential accross 100-nanosecond UtcNow precision limits.
    • 64-bit cryptographic randomness adds uniqueness for timestamp collisions and provides reasonable unguessability and protection against online brute-force attacks.
  • Helper methods for Guids generated by NewSqlServerGuid():

    • DateTime FastGuid.SqlServer.GetTimestamp(Guid guid)
      • Extracts SqlServer guid creation timestamp (UTC). Full DateTime.UtcNow precision.
    • Guid FastGuid.SqlServer.MinGuidForTimestamp(DateTime timestampUtc)
    • Guid FastGuid.SqlServer.MaxGuidForTimestamp(DateTime timestampUtc)
      • Return the smallest/largest Guid for a given timestamp (useful for time-based database range searches).

Usage

Replace all calls to Guid.NewGuid() with FastGuid.NewGuid()

..from this:

Guid guid = Guid.NewGuid(); // your current code

..to this:

// using SecurityDriven;
Guid guid = FastGuid.NewGuid(); // 10x faster
  • Thread-safe
  • 128 bits of cryptographically-strong randomness

Switch from this:

Span<byte> key = stackalloc byte[32];
RandomNumberGenerator.Fill(key); // 145 nanoseconds

..to this:

Span<byte> key = stackalloc byte[32];
FastGuid.Fill(key); // 25 nanoseconds

Benchmark #1:

public class Bench
{
	[Benchmark(Baseline = true)]
	public void FastGuid_NewGuid() // 12 calls
	{
		FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid();
		FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid();
		FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid(); FastGuid.NewGuid();
	}

	[Benchmark]
	public void Guid_NewGuid() // 12 calls
	{
		Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid();
		Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid();
		Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid(); Guid.NewGuid();
	}
}//class Bench
BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19045.2364)
Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=7.0.101
  [Host] : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
Method Mean Error StdDev Ratio
FastGuid_NewGuid 116.6 ns 2.26 ns 5.19 ns 1.00
Guid_NewGuid 1,215.9 ns 23.85 ns 45.96 ns 10.39

Benchmark #2:

static Guid Test_Guid_CreateVersion7() => Guid.CreateVersion7();
static Guid Test_Guid_NewGuid() => Guid.NewGuid();
static Guid Test_FastGuid_NewGuid() => FastGuid.NewGuid();
static Guid Test_FastGuid_NewSqlServerGuid() => FastGuid.NewSqlServerGuid();
BenchmarkDotNet v0.13.8, Windows 10 (10.0.19045.4780/22H2/2022Update)
Intel Core i7-10510U CPU 1.80GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.100-preview.7.24407.12
  [Host] : .NET 9.0.0 (9.0.24.40507), X64 RyuJIT AVX2
Method Mean Error StdDev Ratio
Test_Guid_CreateVersion7 149.7 ns 3.0 ns 5.9 ns 16x
Test_Guid_NewGuid 84.7 ns 1.7 ns 3.5 ns 9x
Test_FastGuid_NewGuid (base) 9.6 ns 0.2 ns 0.4 ns 1x
Test_FastGuid_NewSqlServerGuid 42.5 ns 0.9 ns 1.4 ns 4x