KsuidDotNet [](https://www.nuget.org/packages/KsuidDotNet)

March 12, 2026 · View on GitHub

A high-performance, zero-allocation KSUID (Key Sortable Unique Identifier) generator for .NET 8 and .NET 10.

Designed for high-throughput database engines and distributed systems where unique ID generation is on the hot path.

🚀Performance

This library is engineered for speed. It uses hardware intrinsics, stackalloc, and a buffered RNG strategy to outperform standard implementations.

  • creates crypto-random KSUID strings
  • blazing-fast performance
  • no additional memory overhead, memory allocation only for string
  • thread-safe and lock-free
  • targets .net 8, .net 10

Key Optimizations

  • Zero Allocation: Uses string.Create and Span<T> to write directly to the heap, bypassing intermediate string allocations.

  • Buffered RNG: Uses a thread-static buffer to amortize the cost of Syscall (randomness) overhead by ~99%.

  • Math Intrinsics: Leverages modern .NET Math.DivRem and BinaryPrimitives to optimize the Base62 encoding loop.

installation

$ dotnet add package KsuidDotNet

how to use

Create a random KSUID:

string id = Ksuid.NewKsuid();

Console.WriteLine(id); // outputs 29faSiN1gPB6IzM74u6tMfTO02L

Creating a random KSUID string with the prefix cust_:

string id = Ksuid.NewKsuid("cust_");

Console.WriteLine(id); // outputs c_29faSiN1gPB6IzM74u6tMfTO02L

micro benchmarks

Benchmark results for KsuidDotNet and other KSUID libraries.

X64: Ryzen 9950X3D

BenchmarkDotNet v0.15.6, Windows 11 (10.0.26200.8037)
AMD Ryzen 9 9950X3D 4.30GHz, 1 CPU, 32 logical and 16 physical cores
.NET SDK 10.0.104
  [Host]     : .NET 10.0.4 (10.0.4, 10.0.426.12010), X64 RyuJIT x86-64-v4
  DefaultJob : .NET 10.0.4 (10.0.4, 10.0.426.12010), X64 RyuJIT x86-64-v4
MethodMeanMinMaxOp/sGen0Allocated
'NewKsuid() - Real World'62.27 ns62.13 ns62.36 ns16,059,914.60.001580 B
'NewKsuid(Prefix) - Real World'63.03 ns62.66 ns63.27 ns15,865,375.50.001788 B
'NewKsuid(DateTime, Prefix) - Deterministic'43.31 ns43.19 ns43.41 ns23,089,671.40.001788 B

ARM64: M2 Ultra

BenchmarkDotNet v0.15.6, macOS Sequoia 15.4 (24E248) [Darwin 24.4.0]
Apple M2 Ultra, 1 CPU, 24 logical and 24 physical cores
.NET SDK 10.0.201
  [Host]     : .NET 10.0.5 (10.0.5, 10.0.526.15411), Arm64 RyuJIT armv8.0-a
  DefaultJob : .NET 10.0.5 (10.0.5, 10.0.526.15411), Arm64 RyuJIT armv8.0-a
MethodMeanMinMaxOp/sGen0Allocated
'NewKsuid() - Real World'76.91 ns76.57 ns77.23 ns13,001,667.40.009580 B
'NewKsuid(Prefix) - Real World'77.70 ns76.70 ns78.19 ns12,870,678.60.010588 B
'NewKsuid(DateTime, Prefix) - Deterministic'58.82 ns58.51 ns59.09 ns17,001,186.10.010588 B

license

This library is open-source software released under the MIT License.