f4b6a3 / uuid-creator

UUID Creator is a Java library for generating Universally Unique Identifiers.
MIT License
430 stars 44 forks source link

optimize default random function #55

Closed fabiolimace closed 2 years ago

fabiolimace commented 2 years ago

The DefaultRandomFunction was refactored to use a fixed pool of SecureRandom. The previous implementation used a thread local SecureRandom.

The thread pool SIZE is equal to the number of processors (or cores) available to the runtime. For example, if there's only 1 processor available, the pool size will be 1. If there are 16 processors available, the pool size will be 16. The maximum pool size is 32.

The current INDEX in the thread pool is calculated with this formula: INDEX = CURRENT_THREAD_ID % POOL_SIZE.

A BENCHMARK using a high number of threads (100) showed that the fixed pool performs almost the same as the thread local to generate random numbers using the SHA1PRNG algorithm.

-----------------------------------------------------------------
Benchmark                Mode  Cnt      Score      Error   Units
-----------------------------------------------------------------
Throughput.singleton    thrpt    3   3020,402 ±  449,643  ops/ms
Throughput.fixedpool    thrpt    3  14639,183 ± 1205,735  ops/ms  (NEW)
Throughput.threadlocal  thrpt    3  15163,027 ±  905,890  ops/ms  (OLD)
-----------------------------------------------------------------

Benchmark settings:

fabiolimace commented 2 years ago

Benchmark files are available on this GitHub Gist

fabiolimace commented 2 years ago

Released version v4.3.0.