From 88cd9ebc0964c9daf32d97631b5eeba5fd0e8b09 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 Feb 2015 10:58:28 +0100 Subject: [PATCH] dict.c: dictGetRandomKeys() visit pattern optimization. We use the invariant that the original table ht[0] is never populated up to the index before the current rehashing index. Related to issue #2306. --- src/dict.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dict.c b/src/dict.c index ad74d222..fbcdd35f 100644 --- a/src/dict.c +++ b/src/dict.c @@ -701,6 +701,10 @@ unsigned int dictGetRandomKeys(dict *d, dictEntry **des, unsigned int count) { unsigned int i = random() & maxsizemask; while(stored < count) { for (j = 0; j < tables; j++) { + /* Invariant of the dict.c rehashing: up to the indexes already + * visited in ht[0] during the rehashing, there are no populated + * buckets, so we can skip ht[0] for indexes between 0 and idx-1. */ + if (j == 0 && i < d->rehashidx) continue; if (i >= d->ht[j].size) continue; /* Out of range for this table. */ dictEntry *he = d->ht[j].table[i]; while (he) {