From 5d4cd43ef88be4ba39af34494c65223f4641ef2a Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 7 Sep 2016 12:41:23 +0200 Subject: dict.c better dictPushEntry initial index strategy. --- src/dict.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dict.c b/src/dict.c index a304ee64a..551f83025 100644 --- a/src/dict.c +++ b/src/dict.c @@ -182,9 +182,14 @@ dictEntry *dictPushEntry(dictEntryVector **table, unsigned int idx, const void * memset(dv->entry+dv->used,0,sizeof(dictEntry)*dv->free); } else { uint32_t entries = dv->used+dv->free; - uint32_t j; - for (j = 0; j < entries; j++) { + /* Start iterating with j set to the number of buckets already + * used: it is likely there are empty buckets at the end after + * a reallocation of the entries vector. */ + uint32_t j = dv->used; + uint32_t i = entries; + while (i--) { if (dv->entry[j].key == NULL) break; + j = (j+1)%entries; } he = dv->entry+j; dv->used++; -- cgit v1.2.1