-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Buffered PRNG #45
Comments
I also looked into the PRNG behavior, as the necessity of an Lines 283 to 289 in bb685ba
… together with the use of 0xff instead of 0x100 here: Line 89 in bb685ba
But it turned out just to be an unusual construct for someone used to either
For anyone interested in the frequency analysis, here is a branch supporting it: https://github.com/MarcelWaldvogel/ulidx/tree/frequency-analysis (I am not creating a PR for this, as it would increase the public API unnecessarily. Even though such a test might be a good one to have included.) |
This is the benchmark output on my development machine after applying #46 :
I compared a few pieces of code which would result in relatively small code changes, but big performance improvements. The code run by
The result is about a 14-fold improvement with comparable code and complexity as before. According to the benchmark, the "more computation" alternative would be some 20% faster. However, the difference was particularly high for this benchmark run; it is often only half the difference and sometimes even the other way round:
Given that the extra complexity would require additional careful testing of the logic (that each random bit really only used once and that always all 5 bits are fresh from the random pool), I do suggest sticking to the simple code. |
Hello,
I'm using ulidx in my project and was concerned about its performance based on benchmarks. While the built-in functionality seemed slow (compared to uuid v7 for example), I decided to investigate further.
Local Machine Benchmark
Here is the result of the benchmark on my local machine, a Mac M1 16 Go
Profiling and Bottleneck Identification:
I profiled ulidx (for Node.js only with --prof) to pinpoint the performance bottleneck. The results indicated that the
encodeRandom
method was the primary culprit. Upon closer inspection, I observed that this method callscrypto.getRandomValues
16 times per ulid generation, which seemed excessive.Proposed Solution: Buffered PRNG
To address this, I implemented a custom PRNG that reduces the number of calls to
crypto.getRandomValues
while using a larger buffer size. Here's the code:This custom PRNG generates random values from a pre-filled buffer, reducing the number of calls to
crypto.getRandomValues
.Integration with Benchmark
I incorporated the
BufferedPRNG
class into the benchmark to compare performance:Benchmark Results:
The results demonstrate significant performance improvements. Using a buffer size of 16 (which is the minimum required for a single ulid) yields a roughly 14x speedup.
Request for Feedback
Please let me know if there are any errors in my implementation or approach. I'm open to any suggestions because I plan to use this optimization in my project.
The text was updated successfully, but these errors were encountered: