Skip to content

Improve multi-threaded generation and GUID conversion performance - #129

Merged
Seramis merged 2 commits into
ByteAether:mainfrom
Seramis:perf/guid-conversion-and-lock-layout
Nov 3, 2026
Merged

Seramis merged 2 commits into
ByteAether:mainfrom
Seramis:perf/guid-conversion-and-lock-layout

Conversation

@Seramis

@Seramis Seramis commented Nov 3, 2026

Copy link
Copy Markdown
Member

Description

Two independent performance improvements:

  • Co-locate the generation lock and the last ULID on one cache line. The lock and the last generated ULID are always used together inside the critical section. 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.
  • Speed up GUID conversion on .NET 7-9 and in the scalar fallback. 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.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Other (please specify): Performance improvements

Checklist

  • The PR is submitted to the correct branch (main).
  • My code follows the project's coding style. (.editorconfig)
  • I have commented my code, particularly in hard-to-understand areas and public interfaces.
  • I have added or updated tests for the changes I made.
  • All new and existing tests passed.
  • I have updated the documentation where applicable.

Additional Notes

Arm CPUs on .NET 7-9 now use the scalar fallback, which was not measured.

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.
@Seramis Seramis self-assigned this Nov 3, 2026
@Seramis Seramis added enhancement New feature or request pkg:Ulid ByteAether.Ulid .NET Pull requests that update .NET code labels Nov 3, 2026
@Seramis
Seramis merged commit fe8335b into ByteAether:main Nov 3, 2026
16 checks passed
@Seramis
Seramis deleted the perf/guid-conversion-and-lock-layout branch November 3, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request .NET Pull requests that update .NET code pkg:Ulid ByteAether.Ulid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant