Unverified Commit ee17e7af authored by 王恒's avatar 王恒 Committed by GitHub
Browse files

improve malloc efficiency: reduce call times of zrealloc (#10533)



* improve malloc efficiency: reduce call times of zrealloc
Co-authored-by: default avatarMadelyn Olson <madelyneolson@gmail.com>
parent 625bdaf3
...@@ -960,7 +960,6 @@ clusterNode *createClusterNode(char *nodename, int flags) { ...@@ -960,7 +960,6 @@ clusterNode *createClusterNode(char *nodename, int flags) {
memset(node->slots,0,sizeof(node->slots)); memset(node->slots,0,sizeof(node->slots));
node->slot_info_pairs = NULL; node->slot_info_pairs = NULL;
node->slot_info_pairs_count = 0; node->slot_info_pairs_count = 0;
node->slot_info_pairs_alloc = 0;
node->numslots = 0; node->numslots = 0;
node->numslaves = 0; node->numslaves = 0;
node->slaves = NULL; node->slaves = NULL;
...@@ -4726,13 +4725,10 @@ void clusterGenNodesSlotsInfo(int filter) { ...@@ -4726,13 +4725,10 @@ void clusterGenNodesSlotsInfo(int filter) {
* or end of slot. */ * or end of slot. */
if (i == CLUSTER_SLOTS || n != server.cluster->slots[i]) { if (i == CLUSTER_SLOTS || n != server.cluster->slots[i]) {
if (!(n->flags & filter)) { if (!(n->flags & filter)) {
if (n->slot_info_pairs_count+2 > n->slot_info_pairs_alloc) { if (!n->slot_info_pairs) {
if (n->slot_info_pairs_alloc == 0) n->slot_info_pairs = zmalloc(2 * n->numslots * sizeof(uint16_t));
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));
} }
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++] = start;
n->slot_info_pairs[n->slot_info_pairs_count++] = i-1; n->slot_info_pairs[n->slot_info_pairs_count++] = i-1;
} }
...@@ -4747,7 +4743,6 @@ void clusterFreeNodesSlotsInfo(clusterNode *n) { ...@@ -4747,7 +4743,6 @@ void clusterFreeNodesSlotsInfo(clusterNode *n) {
zfree(n->slot_info_pairs); zfree(n->slot_info_pairs);
n->slot_info_pairs = NULL; n->slot_info_pairs = NULL;
n->slot_info_pairs_count = 0; n->slot_info_pairs_count = 0;
n->slot_info_pairs_alloc = 0;
} }
/* Generate a csv-alike representation of the nodes we are aware of, /* Generate a csv-alike representation of the nodes we are aware of,
......
...@@ -120,7 +120,6 @@ typedef struct clusterNode { ...@@ -120,7 +120,6 @@ typedef struct clusterNode {
unsigned char slots[CLUSTER_SLOTS/8]; /* slots handled by this node */ 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). */ 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_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 numslots; /* Number of slots handled by this node */
int numslaves; /* Number of slave nodes, if this is a master */ int numslaves; /* Number of slave nodes, if this is a master */
struct clusterNode **slaves; /* pointers to slave nodes */ struct clusterNode **slaves; /* pointers to slave nodes */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment