Skip to content

Monotonic ULID generation performance improvements#47

Merged
Seramis merged 5 commits into
ByteAether:mainfrom
Seramis:generation_performance
Jul 7, 2026
Merged

Monotonic ULID generation performance improvements#47
Seramis merged 5 commits into
ByteAether:mainfrom
Seramis:generation_performance

Conversation

@Seramis

@Seramis Seramis commented Jul 7, 2026

Copy link
Copy Markdown
Member

Description

  • Refactored and reworked the monotonic ULID generation
    • Introduced LowLatencyLock that uses using syntax. This is faster than manual try-finally block.
    • Rewrote increment/addition logic. Now a single universal method increments by 1 or by 1-4 bytes (+1). Simpler and faster.
    • Stored last ULID and Lock struct in a separate state object that pads these fields into separate cache lines to prevent false-sharing. (Better CPU cache usage as a result)
    • Cleaned up MemoryMarshal compatibility file as some methods were not necessary anymore.
    • Performance improvement during simple +1 incrementing monotonic generation is about 10%. (from 38ns to 34ns on dev machine)
  • Improved slightly parsing of ulong timestamp into bytes avoiding unnecessary span creation. Improves all ULID generation scenarios a tiny bit.
  • Generalized endianness reversing throughout the implementation. Introduced Ulid.Util.cs for shared methods.
  • Updated README.md with benchmarks.

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

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.

@Seramis Seramis self-assigned this Jul 7, 2026
@Seramis Seramis merged commit 8ead6e1 into ByteAether:main Jul 7, 2026
5 checks passed
@Seramis Seramis deleted the generation_performance branch July 7, 2026 15:14
@Seramis Seramis added pkg:Ulid ByteAether.Ulid Nuget enhancement New feature or request labels Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pkg:Ulid ByteAether.Ulid Nuget

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant