Hi Michael
Is there any downside to cache the state within quickrand_cache:init/1 in a global cache?
_ = erlang:put(?TUPLE_PDICT_KEY, state_to_tuple(new(Options))),
Something along these lines quickrand_cache:init_/1
:
-spec init_(Options :: options()) ->
ok.
init_(Options) ->
persistent_term:put(?TUPLE_PDICT_KEY, state_to_tuple(new(Options))).
Then, add this new quickrand_cache:rand_bytes_/1:
-spec rand_bytes_(N :: pos_integer()) ->
binary().
rand_bytes_(N)
when is_integer(N), N > 0 ->
{I, CacheSize, Cache} = persistent_term:get(?TUPLE_PDICT_KEY),
{Bytes, NewI, NewCache} = bytes_get(N, I, CacheSize, Cache),
%% !!! don't know what can be done here as persistent_term:put/2 is really slow (triggers GC) !!!
ok = persistent_term:put(?TUPLE_PDICT_KEY, {NewI, CacheSize, NewCache}),
Bytes.
My application is heavily using your uuid
module to generate v4
UUIDs.
While checking your code today i realized that you've added a nice caching layer to it.
I'm generating about 10 million v4 UUIDs per day and would like to switch to uuid:get_v4(cached_)
:
get_v4(cached) ->
<<Rand1:48, _:4, Rand2:12, _:2, Rand3:62>> =
quickrand_cache:rand_bytes_(16),
<<Rand1:48,
0:1, 1:1, 0:1, 0:1, % version 4 bits
Rand2:12,
1:1, 0:1, % RFC 4122 variant bits
Rand3:62>>;
With this in place, all my processes will use the same cached state and will avoid to store extra data in their process dictionnary:
ok = quickrand_cache:init_([{cache_size, 16}]),
[...]
V4 = uuid:get_v4(cached),
Any issue with this?
Thanks.