Repository navigation
Improve multi-threaded generation and GUID conversion performance - #129
Merged
Seramis merged 2 commits intoNov 3, 2026
Merged
Conversation
The lock and the last generated ULID are always used together inside the critical section, so keeping them on separate cache lines made every lock hand-off between cores move two lines. They now share one line, while the leading and trailing padding in `Ulid.New.GenerationOptions.cs` still keeps neighboring heap objects off it. With one shared `GenerationOptions` instance, throughput on 2-16 threads is mostly 1.4-3.4x higher; single-threaded use and per-thread instances are unchanged.
`Vector128.Shuffle` is only a single instruction from .NET 10 on, so `ToGuid` and `New(Guid)` took 12-15 ns on .NET 7-9. Those targets now use the SSSE3 shuffle like `net5.0` and `net6.0`, bringing them to about 0.5 ns. The scalar fallback now writes a ULID result with one 8-byte store instead of two 4-byte stores read back as one load, which brings `New(Guid)` from 5.2 ns to 0.9 ns. A new test pins the byte mapping with a known GUID. Arm CPUs on .NET 7-9 now use the scalar fallback, which was not measured.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two independent performance improvements:
Ulid.New.GenerationOptions.csstill keeps neighboring heap objects off it. With one sharedGenerationOptionsinstance, throughput on 2-16 threads is mostly 1.4-3.4x higher. Single-threaded use and per-thread instances are unchanged.Vector128.Shuffleis only a single instruction from .NET 10 on, soToGuidandNew(Guid)took 12-15 ns on .NET 7-9. Those targets now use the SSSE3 shuffle likenet5.0andnet6.0, bringing them to about 0.5 ns. The scalar fallback now writes a ULID result with one 8-byte store instead of two 4-byte stores read back as one load, which bringsNew(Guid)from 5.2 ns to 0.9 ns. A new test pins the byte mapping with a known GUID.Type of Change
Checklist
main)..editorconfig)Additional Notes
Arm CPUs on .NET 7-9 now use the scalar fallback, which was not measured.