Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink dict when deleting dictEntry #12850

Merged
merged 14 commits into from
Jan 15, 2024
Merged
8 changes: 4 additions & 4 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ int _dictInit(dict *d, dictType *type)
d->type = type;
d->rehashidx = -1;
d->pauserehash = 0;
d->disallowResize = 0;
d->pauseAutoResize = 0;
return DICT_OK;
}

Expand Down Expand Up @@ -1429,7 +1429,7 @@ static int dictTypeResizeAllowed(dict *d) {
static int _dictExpandIfNeeded(dict *d)
{
/* Automatic resizing is disallowed. Return */
if (d->disallowResize > 0) return DICT_OK;
if (d->pauseAutoResize > 0) return DICT_OK;

/* Incremental rehashing already in progress. Return. */
if (dictIsRehashing(d)) return DICT_OK;
Expand All @@ -1456,7 +1456,7 @@ static int _dictExpandIfNeeded(dict *d)
static int _dictShrinkIfNeeded(dict *d)
{
/* Automatic resizing is disallowed. Return */
if (d->disallowResize > 0) return DICT_OK;
if (d->pauseAutoResize > 0) return DICT_OK;

/* Incremental rehashing already in progress. Return. */
if (dictIsRehashing(d)) return DICT_OK;
Expand Down Expand Up @@ -1529,7 +1529,7 @@ void dictEmpty(dict *d, void(callback)(dict*)) {
_dictClear(d,1,callback);
d->rehashidx = -1;
d->pauserehash = 0;
d->disallowResize = 0;
d->pauseAutoResize = 0;
}

void dictSetResizeEnabled(dictResizeEnable enable) {
Expand Down
6 changes: 3 additions & 3 deletions src/dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct dict {
/* Keep small vars at end for optimal (minimal) struct padding */
int16_t pauserehash; /* If >0 rehashing is paused (<0 indicates coding error) */
signed char ht_size_exp[2]; /* exponent of size. (size = 1<<exp) */
int16_t disallowResize; /* If >0 automatic resizing is disallowed (<0 indicates coding error) */
int16_t pauseAutoResize; /* If >0 automatic resizing is disallowed (<0 indicates coding error) */
void *metadata[];
};

Expand Down Expand Up @@ -159,8 +159,8 @@ typedef struct {
#define dictIsRehashing(d) ((d)->rehashidx != -1)
#define dictPauseRehashing(d) ((d)->pauserehash++)
#define dictResumeRehashing(d) ((d)->pauserehash--)
#define dictDisallowResize(d) ((d)->disallowResize++)
#define dictAllowResize(d) ((d)->disallowResize--)
#define dictPauseAutoResize(d) ((d)->pauseAutoResize++)
#define dictResumeAutoResize(d) ((d)->pauseAutoResize--)

/* If our unsigned long type can store a 64 bit number, use a 64 bit PRNG. */
#if ULONG_MAX >= 0xffffffffffffffff
Expand Down
4 changes: 2 additions & 2 deletions src/t_zset.c
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ void zremrangeGenericCommand(client *c, zrange_type rangetype) {
}
} else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = zobj->ptr;
dictDisallowResize(zs->dict);
dictPauseAutoResize(zs->dict);
oranagra marked this conversation as resolved.
Show resolved Hide resolved
switch(rangetype) {
case ZRANGE_AUTO:
case ZRANGE_RANK:
Expand All @@ -2023,7 +2023,7 @@ void zremrangeGenericCommand(client *c, zrange_type rangetype) {
deleted = zslDeleteRangeByLex(zs->zsl,&lexrange,zs->dict);
break;
}
dictAllowResize(zs->dict);
dictResumeAutoResize(zs->dict);
if (htNeedsResize(zs->dict)) dictResize(zs->dict);
oranagra marked this conversation as resolved.
Show resolved Hide resolved
if (dictSize(zs->dict) == 0) {
dbDelete(c->db,key);
Expand Down