From 17904780ae5b4793ad133020d6bfa7f4a266b20c Mon Sep 17 00:00:00 2001 From: WangYu Date: Wed, 16 Aug 2023 15:45:26 +0800 Subject: [PATCH] skip the rehashed entries in dictNext (#12386) If dict is rehashing, the entries in the head of table[0] is moved to table[1] and all entries in `table[0][0:rehashidx]` is NULL. `dictNext` start looking for non-NULL entry from table 0 index 0, and the first call of `dictNext` on a rehashing dict will Iterate many times to skip those NULL entries. We can easily skip those entries by setting `iter->index` as `iter->d->rehashidx` when dict is rehashing and it's the first call of dictNext (`iter->index == -1 && iter->table == 0`). Co-authored-by: sundb --- src/dict.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/dict.c b/src/dict.c index 6760da154..2eb9aa41f 100644 --- a/src/dict.c +++ b/src/dict.c @@ -950,6 +950,11 @@ dictEntry *dictNext(dictIterator *iter) dictPauseRehashing(iter->d); else iter->fingerprint = dictFingerprint(iter->d); + + /* skip the rehashed slots in table[0] */ + if (dictIsRehashing(iter->d)) { + iter->index = iter->d->rehashidx - 1; + } } iter->index++; if (iter->index >= (long) DICTHT_SIZE(iter->d->ht_size_exp[iter->table])) {