when find a key ,if redis is rehashing, now we should lookup both tables (ht[0] and ht[1]).
why don't we use the key's index comparing to the rehashidx,if index < rehashidx,then we don't need to lookup ht[0].
blow is the code :
for (table = 0; table <= 1; table++) {
idx = h & d->ht[table].sizemask;
he = d->ht[table].table[idx];
while(he) {
if (key==he->key || dictCompareKeys(d, key, he->key))
return he;
he = he->next;
}
if (!dictIsRehashing(d)) return NULL;
}
we can change it :
for (table = 0; table <= 1; table++) {
idx = h & d->ht[table].sizemask;
if (idx < d->rehashidx) continue;
he = d->ht[table].table[idx];
while(he) {
if (key==he->key || dictCompareKeys(d, key, he->key))
return he;
he = he->next;
}
if (!dictIsRehashing(d)) return NULL;
}
if idx < d->rehashidx,we can conclude:
1.it is rehashing(rehashidx is -1 if it is not rehashing)
2.we can't find key in ht[0]
so continue