From f25fdd6246f01b6ee3c0ce557e2911bc8c068518 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 Feb 2015 10:48:13 +0100 Subject: dict.c: avoid code repetition in dictRehash(). Avoid code repetition introduced with PR #2367, also fixes the return value to always return 0 if there is nothing more to rehash. --- src/dict.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/dict.c b/src/dict.c index 3b23fbcd0..c8aaf1529 100644 --- a/src/dict.c +++ b/src/dict.c @@ -246,18 +246,9 @@ int dictRehash(dict *d, int n) { int empty_visits = n*10; /* Max number of empty buckets to visit. */ if (!dictIsRehashing(d)) return 0; - while(n--) { + while(n-- && d->ht[0].used != 0) { dictEntry *de, *nextde; - /* Check if we already rehashed the whole table... */ - if (d->ht[0].used == 0) { - zfree(d->ht[0].table); - d->ht[0] = d->ht[1]; - _dictReset(&d->ht[1]); - d->rehashidx = -1; - return 0; - } - /* Note that rehashidx can't overflow as we are sure there are more * elements because ht[0].used != 0 */ assert(d->ht[0].size > (unsigned long)d->rehashidx); @@ -282,13 +273,17 @@ int dictRehash(dict *d, int n) { d->ht[0].table[d->rehashidx] = NULL; d->rehashidx++; } - /* Check again if we already rehashed the whole table... */ + + /* Check if we already rehashed the whole table... */ if (d->ht[0].used == 0) { zfree(d->ht[0].table); d->ht[0] = d->ht[1]; _dictReset(&d->ht[1]); d->rehashidx = -1; + return 0; } + + /* More to rehash... */ return 1; } -- cgit v1.2.1