Skip to content

Conversation

@Seramis
Copy link
Member

@Seramis Seramis commented Aug 13, 2025

Description

This pull request introduces several performance enhancements and code simplifications.

  • The FillTime method was refactored to use bitwise operations, making it more straightforward and performant.
  • The FillRandom method's performance was improved by changing the monotonic comparison logic. Instead of comparing byte sequences, which can be slow, it now compares timestamps as ulong (unsigned 64-bit integer) values.
  • A lightweight spinlock has replaced the heavier lock keyword in Ulid.New. The lock was a bottleneck, especially in high-concurrency scenarios, due to its overhead.
  • The [MethodImpl(MethodImplOptions.AggressiveOptimization)] attribute was added to several methods across different classes (CryptographicallySecureRandomProvider.cs, PseudoRandomProvider.cs, Ulid.Comparable.cs, Ulid.Equatable.cs, Ulid.Guid.cs, Ulid.IsValid.cs, Ulid.New.cs, Ulid.String.cs, and Ulid.cs).
  • Benchmark results in README.md have been updated to reflect the new performance gains.

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 (performance improvements, code refactoring)

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.

Testing Instructions

The changes are primarily for performance. The existing test suite should be sufficient to verify that the changes do not break any existing functionality. Performance can be verified by running the benchmarks.

Additional Notes

These changes significantly improve the performance of ULID generation and other core operations, making the library more suitable for high-throughput applications. The move from a heavy lock to a lightweight spinlock is particularly beneficial for concurrent use cases.

…andom` performance by comparing timestamps as `ulong` values instead of as sequences of bytes. Updated benchmark results in README.
@Seramis Seramis self-assigned this Aug 13, 2025
@Seramis Seramis added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 13, 2025
@Seramis Seramis merged commit 4578dde into ByteAether:main Aug 13, 2025
5 checks passed
@Seramis Seramis deleted the PerformanceOptimizations branch August 13, 2025 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant