summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-02-05 12:15:58 +0100
committerantirez <antirez@gmail.com>2015-02-11 11:01:46 +0100
commit252ebcd973614c14e81d7744826cd1ab0248546c (patch)
tree1d17ba08a40fe9bd7cbd40c3368835ce5a8022af
parenta355568c1a9297e3126df2574805f29e4501c7ac (diff)
downloadredis-252ebcd973614c14e81d7744826cd1ab0248546c.tar.gz
dict.c: don't try buckets that are empty for sure in dictGetRandomKey().
This is very similar to the optimization applied to dictGetRandomKeys, but applied to the single key variant. Related to issue #2306.
-rw-r--r--src/dict.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/dict.c b/src/dict.c
index af3623d0d..e0adfbc99 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -628,7 +628,11 @@ dictEntry *dictGetRandomKey(dict *d)
if (dictIsRehashing(d)) _dictRehashStep(d);
if (dictIsRehashing(d)) {
do {
- h = random() % (d->ht[0].size+d->ht[1].size);
+ /* We are sure there are no elements in indexes from 0
+ * to rehashidx-1 */
+ h = d->rehashidx + (random() % (d->ht[0].size +
+ d->ht[1].size -
+ d->rehashidx));
he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] :
d->ht[0].table[h];
} while(he == NULL);