A .NET Standard 2.0 port of Segment’s K-Sortable Unique IDentifiers(KSUID) library
So what are KSUID’s?
They are global unique identifiers (27 chars) prefixed with an encoded timestamp, which provides a 1-second resolution. The characteristics of KSUID’s are:
- Natural ordering by generation time
- Collision-free, coordination-free & dependency-free nature
- Highly portable representation
Compared to other global unique identifiers such as GUID/UUID’s, the main advantage is their sortability.
Segment had open-sourced their implementation of KSUID’s for Go lang only. Since I primarily work with C# & .NET, I was curious to check if any open source ports were made for it. There was a single port made 3 years ago by GitHub user JoyMoe. But it was only released as a preview.
Fast forward 2 weeks, we at work, faced a need for something like KSUID. Ultimately we didn’t choose KSUID’s and instead went with GUID’s even tho KSUID’s might have been a better fit for our scenario. So I thought of working on a better & improved port of KSUID for the .NET ecosystem. So Today I am launching the
1.0.0 of DotKsuid which is heavily inspired by the original Segment implementation for Go & JoyMoe’s .NET port. The library is available as a package via NuGet. This port supports .NET Standard 2.0. On an API level, I have tried to expose the same methods that a GUID API exposes. The project is open-sourced under MIT license & I am committing to maintaining it going forward. Since the .NET ecosystem is going through rapid changes & massive performance improvements, I wish to keep updating the library to better take advantage of the framework level changes.
The current implementation itself is 2.5x slower than GUID for generation & 3.8x slower for conversion to a string representation. This is expected since GUID generation is offloaded into native APIs at the OS level. You can see the same from the below benchmark results.
But this port is 9x faster for generation & 6.5x faster for conversion to a string representation than the original port made by JoyMoe, the only port available right now. I have tried to take advantage of the .NET framework level changes in recent years to achieve this. Check the benchmark results below for more details.