From 3b3d16f7488063f0d2ee7d5cfaeba736169046de Mon Sep 17 00:00:00 2001 From: Binbin Date: Wed, 13 Mar 2024 14:30:20 +0800 Subject: [PATCH] Add KVSTORE_FREE_EMPTY_DICTS to cluster mode keys / expires kvstore (#13072) Currently (following #11695, and #12822), keys kvstore and expires kvstore both flag with ON_DEMAND, it means that a cluster node will only allocate a dict when the slot is assigned to it and populated, but on the other hand, when the slot is unassigned, the dict will remain allocated. We considered releasing the dict when the slot is unassigned, but it causes complications on replicas. On the other hand, from benchmarks we conducted, it looks like the performance impact of releasing the dict when it becomes empty and re-allocate it when a key is added again, isn't huge. This PR add KVSTORE_FREE_EMPTY_DICTS to cluster mode keys / expires kvstore. The impact is about about 2% performance drop, for this hopefully uncommon scenario. --------- Co-authored-by: Oran Agra --- src/server.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/server.c b/src/server.c index 92e8c3f33..cff9db24c 100644 --- a/src/server.c +++ b/src/server.c @@ -2657,10 +2657,15 @@ void initServer(void) { server.db = zmalloc(sizeof(redisDb)*server.dbnum); /* Create the Redis databases, and initialize other internal state. */ - int slot_count_bits = (server.cluster_enabled) ? CLUSTER_SLOT_MASK_BITS : 0; + int slot_count_bits = 0; + int flags = KVSTORE_ALLOCATE_DICTS_ON_DEMAND; + if (server.cluster_enabled) { + slot_count_bits = CLUSTER_SLOT_MASK_BITS; + flags |= KVSTORE_FREE_EMPTY_DICTS; + } for (j = 0; j < server.dbnum; j++) { - server.db[j].keys = kvstoreCreate(&dbDictType, slot_count_bits, KVSTORE_ALLOCATE_DICTS_ON_DEMAND); - server.db[j].expires = kvstoreCreate(&dbExpiresDictType, slot_count_bits, KVSTORE_ALLOCATE_DICTS_ON_DEMAND); + server.db[j].keys = kvstoreCreate(&dbDictType, slot_count_bits, flags); + server.db[j].expires = kvstoreCreate(&dbExpiresDictType, slot_count_bits, flags); server.db[j].expires_cursor = 0; server.db[j].blocking_keys = dictCreate(&keylistDictType); server.db[j].blocking_keys_unblock_on_nokey = dictCreate(&objectKeyPointerValueDictType);