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.
This commit is contained in:
antirez 2015-02-05 10:58:28 +01:00
parent cd0fcf11e7
commit 88cd9ebc09

View File

@ -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) {