From 463476933c47541c307eca47fd85e0ed4cb4f3e9 Mon Sep 17 00:00:00 2001 From: Binbin Date: Wed, 22 Nov 2023 17:33:26 +0800 Subject: [PATCH] Optimize dict expand check, move allow check after the thresholds check (#12789) dictExpandAllowed (for the main db dict and the expire dict) seems to involve a few function calls and memory accesses, and we can do it only after the thresholds checks and can get some performance improvements. A simple benchmark test: there are 11032768 fixed keys in the database, start a redis-server with `--maxmemory big_number --save ""`, start a redis-benchmark with `-P 100 -r 1000000000 -t set -n 5000000`, collect `throughput summary: n requests per second` result. After five rounds of testing: ``` unstable this PR 848032.56 897988.56 854408.69 913408.88 858663.94 914076.81 871839.56 916758.31 882612.56 920640.75 ``` We can see a 5% performance improvement in general condition. But note we'll still hit the degradation when we over the thresholds. --- src/dict.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dict.c b/src/dict.c index 1946e2492..79988d768 100644 --- a/src/dict.c +++ b/src/dict.c @@ -1410,13 +1410,13 @@ static int _dictExpandIfNeeded(dict *d) * table (global setting) or we should avoid it but the ratio between * elements/buckets is over the "safe" threshold, we resize doubling * the number of buckets. */ - if (!dictTypeExpandAllowed(d)) - return DICT_OK; if ((dict_can_resize == DICT_RESIZE_ENABLE && d->ht_used[0] >= DICTHT_SIZE(d->ht_size_exp[0])) || (dict_can_resize != DICT_RESIZE_FORBID && d->ht_used[0] / DICTHT_SIZE(d->ht_size_exp[0]) > dict_force_resize_ratio)) { + if (!dictTypeExpandAllowed(d)) + return DICT_OK; return dictExpand(d, d->ht_used[0] + 1); } return DICT_OK;