From feb032fd42d24959c0ba141d8fb5d621f0570345 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 2 Mar 2022 00:40:39 -0800 Subject: [PATCH] A faster and more robust code of zslRandomLevel using RAND_MAX (#5539) 1. since ZSKIPLIST_P is float, using it directly inside the condition used to causes floating point code to be used (gcc/x86) 2. In some operating system(eg.Windows), the largest value returned from random() is 0x7FFF(15bit), so after bitwise AND with 0xFFFF, the probability of the less operation returning true in the while loop's condition is no more equal to ZSKIPLIST_P. 3. In case some library has random() returning int in range [0~ZSKIPLIST_P*65535], the while loop will be an infinite loop. 4. on Linux where RAND_MAX is higher than 0xFFFF, this change actually improves precision (despite not matching the result against a float value) --- src/t_zset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/t_zset.c b/src/t_zset.c index bc947c9658..dd0678f7ac 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -120,8 +120,9 @@ void zslFree(zskiplist *zsl) { * (both inclusive), with a powerlaw-alike distribution where higher * levels are less likely to be returned. */ int zslRandomLevel(void) { + static const int threshold = ZSKIPLIST_P*RAND_MAX; int level = 1; - while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF)) + while (random() < threshold) level += 1; return (level