mareek / UUIDNext

A fast and modern .NET library to generate UUID/GUID that are either sequential and database friendly (versions 7), name based (versions 5) or random (version 4).
BSD Zero Clause License
240 stars 13 forks source link

Eliminate extra buffer usage for timestamp in UUIDv7 generation #13

Closed firenero closed 3 months ago

firenero commented 3 months ago

UUIDv7 generation utilizes an additional 8-bytes buffer for setting timestamp. This PR removes the need for additional buffer.

Changes:

mareek commented 3 months ago

Thank you for your pull request. Unfortunately, the range syntax ([start .. end]) isn't supported when we target the .NET Framework. Can you use span.Slice(start, length) instead of the range syntax ?

mareek commented 3 months ago

Can you also do some benchmark comparing your code to the existing version, please ? You can use the project UUIDNext.Benchmarks for that

firenero commented 3 months ago

Sorry, I don't have .NET Framework so I couldn't test it. Updated the PR to use Slice().

I ran UuidBench.NewUuidV7() benchmark using .NET 8 runtime against my changes and the main branch and got these results:

Method Mean Error StdDev Allocated
NewUuidV7 (new) 100.4 ns 2.00 ns 1.67 ns -
NewUuidV7 (old) 101.9 ns 2.00 ns 1.87 ns -

The diff is within the error which is somewhat expected given that the change only removes copying 6 bytes from stack allocated buffer. Unfortunately I can't run it against .NET Framework to see if results are different there.

mareek commented 3 months ago

I've done some benchmarks on the .NET framework and I get similar results: a few percent of improvement around the margin of error

mareek commented 3 months ago

PR Merged, thank you 👍