Warning
This project is for experimental/curiosity purposes only and is not intended for production use.
StrictGuid is a high-performance .NET library for working with typed identifiers based on the RFC 9562 standard (UUID v8). It allows you to embed the entity type directly into the GUID, ensuring strict typing at the domain model level without sacrificing performance.
- 🚀 Extreme Speed: 55x faster than standard
Guid.NewGuid()inFastmode. - 💎 Zero-Allocation: Zero heap allocations (0 bytes).
- 🛡️ Type-Safe: Binds IDs to your
enum : byte(supports up to 256 types). - 🔒 Entropy Choice:
Fast(Random),Secure(Crypto), andSequential(DB-friendly). - 📏 UUID v8 Compliance: Fully compliant with RFC 9562.
- 🛡️ Reliability: Strict protection against regular UUID v4 and "garbage" GUIDs.
src/StrictGuid.Library: The core library (Extension methods, generator).tests/StrictGuid.Tests: Unit tests (xUnit).benchmarks/StrictGuid.Benchmarks: Performance tests (BenchmarkDotNet).samples/StrictGuid.Demo: Usage example.
Performance comparison on .NET 10.0.202 (Apple M1):
| Method | Mean | Ratio | Allocated |
|---|---|---|---|
| StandardNewGuid | 255.00 ns | 1.00 | - |
| StrictGuidFast | 4.65 ns | 0.02 | - |
| StrictGuidSequential | 27.81 ns | 0.11 | - |
| StrictGuidSecure | 256.86 ns | 1.01 | - |
| ExtractType | 6.73 ns | 0.03 | - |
The Sequential mode provides an excellent balance: it is 9x faster than a standard GUID and guarantees correct sorting (prevents index fragmentation) in the DB.
The Fast mode outperforms the regular GUID generator by 55 times.
You can install the package from NuGet via the .NET CLI:
dotnet add package StrictGuidOr via the Package Manager Console in Visual Studio:
Install-Package StrictGuidYour enums must be based on the byte type. You can define both domain entities and service types (e.g., logging, webhooks):
public enum EntityType : byte
{
// Service types
Log = 0,
WebHook = 1,
Request = 2,
// Domain entities
User = 3,
Order = 4,
Invoice = 5,
// ... supports up to 256 types
}using StrictGuid.Library;
// Sequential ID (UUID v7 style) — recommended for DBs
Guid orderId = EntityType.Order.NewStrictGuid(StrictGuidEntropy.Sequential);
// Ultra-fast random ID (for logs or transactions)
Guid logId = EntityType.Log.NewStrictGuid(StrictGuidEntropy.Fast);
// Cryptographically secure ID (tokens)
Guid secretId = EntityType.User.NewStrictGuid(StrictGuidEntropy.Secure);// Extracting type
EntityType type = id.GetEntityType<EntityType>();
// Fast validation (throws StrictGuidException on mismatch)
id.ValidateEntityType(EntityType.Order);
// Safe logical check
if (id.IsEntityType(EntityType.User))
{
// ...
}| Mode | Source | Order | When to use |
|---|---|---|---|
| Fast | Random.Shared |
Random | Regular IDs, high speed, logs. |
| Secure | Crypto |
Random | Secret tokens, passwords, sessions. |
| Sequential | Time + Random |
Sequential | Primary Keys (PK) in DB, sortable lists. |
The library reliably isolates entity types. The validation or type extraction method is guaranteed to detect mismatches.
If a standard UUID v4 is passed to your system (e.g., generated by a normal Guid.NewGuid()), the library will not confuse it with an entity (even an entity with code 0, like Log).
The algorithm strictly checks the version bits (UUID v8 requires the value 0x80 in the upper bits of the 7th byte). If a v4 or garbage GUID is passed:
IsEntityTypewill safely returnfalse.GetEntityTypeorValidateEntityTypewill throw aStrictGuidException.
- .NET 8.0+, .NET 6.0, or .NET Standard 2.0
- Support for
System.Runtime.CompilerServices.Unsafe(included automatically in newer .NET, added as a NuGet package for .NET Standard 2.0)
AggressiveInlining&AggressiveOptimizationto eliminate call overhead.Unsafe.Asfor Zero-allocation type conversions.Span<byte>&stackallocfor stack-based memory operations.