Refactored FillTime for simplicity and performance, improved FillRandom performance, and replaced lock with spinlock.
#18
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
This pull request introduces several performance enhancements and code simplifications.
FillTimemethod was refactored to use bitwise operations, making it more straightforward and performant.FillRandommethod's performance was improved by changing the monotonic comparison logic. Instead of comparing byte sequences, which can be slow, it now compares timestamps asulong(unsigned 64-bit integer) values.lockkeyword inUlid.New. Thelockwas a bottleneck, especially in high-concurrency scenarios, due to its overhead.[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, andUlid.cs).README.mdhave been updated to reflect the new performance gains.Type of Change
Checklist
main)..editorconfig)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
lockto a lightweight spinlock is particularly beneficial for concurrent use cases.