From ee17e7af8da0c6e482bc24ad1e2197d94cba1e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=81=92?= <33808231+f20500909@users.noreply.github.com> Date: Sun, 10 Apr 2022 10:52:36 +0800 Subject: [PATCH] improve malloc efficiency: reduce call times of zrealloc (#10533) * improve malloc efficiency: reduce call times of zrealloc Co-authored-by: Madelyn Olson --- src/cluster.c | 11 +++-------- src/cluster.h | 1 - 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 701871b36..a757172e7 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -960,7 +960,6 @@ clusterNode *createClusterNode(char *nodename, int flags) { memset(node->slots,0,sizeof(node->slots)); node->slot_info_pairs = NULL; node->slot_info_pairs_count = 0; - node->slot_info_pairs_alloc = 0; node->numslots = 0; node->numslaves = 0; node->slaves = NULL; @@ -4726,13 +4725,10 @@ void clusterGenNodesSlotsInfo(int filter) { * or end of slot. */ if (i == CLUSTER_SLOTS || n != server.cluster->slots[i]) { if (!(n->flags & filter)) { - if (n->slot_info_pairs_count+2 > n->slot_info_pairs_alloc) { - if (n->slot_info_pairs_alloc == 0) - n->slot_info_pairs_alloc = 8; - else - n->slot_info_pairs_alloc *= 2; - n->slot_info_pairs = zrealloc(n->slot_info_pairs, n->slot_info_pairs_alloc * sizeof(uint16_t)); + if (!n->slot_info_pairs) { + n->slot_info_pairs = zmalloc(2 * n->numslots * sizeof(uint16_t)); } + serverAssert((n->slot_info_pairs_count + 1) < (2 * n->numslots)); n->slot_info_pairs[n->slot_info_pairs_count++] = start; n->slot_info_pairs[n->slot_info_pairs_count++] = i-1; } @@ -4747,7 +4743,6 @@ void clusterFreeNodesSlotsInfo(clusterNode *n) { zfree(n->slot_info_pairs); n->slot_info_pairs = NULL; n->slot_info_pairs_count = 0; - n->slot_info_pairs_alloc = 0; } /* Generate a csv-alike representation of the nodes we are aware of, diff --git a/src/cluster.h b/src/cluster.h index 27e9e7770..90b775ca2 100644 --- a/src/cluster.h +++ b/src/cluster.h @@ -120,7 +120,6 @@ typedef struct clusterNode { unsigned char slots[CLUSTER_SLOTS/8]; /* slots handled by this node */ uint16_t *slot_info_pairs; /* Slots info represented as (start/end) pair (consecutive index). */ int slot_info_pairs_count; /* Used number of slots in slot_info_pairs */ - int slot_info_pairs_alloc; /* Allocated number of slots in slot_info_pairs */ int numslots; /* Number of slots handled by this node */ int numslaves; /* Number of slave nodes, if this is a master */ struct clusterNode **slaves; /* pointers to slave nodes */