From 4521115b1767081f17acf67d154c69e1a589c0bb Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 28 Feb 2013 15:11:05 +0100 Subject: [PATCH] Cluster: new field in cluster node structure, "numslots". Before a relatively slow popcount() operation was needed every time we needed to get the number of slots served by a given cluster node. Now we just need to check an integer that is taken in sync with the bitmap. --- src/cluster.c | 5 +++++ src/redis.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index 3670f4821..584d837ce 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -330,6 +330,7 @@ clusterNode *createClusterNode(char *nodename, int flags) { getRandomHexChars(node->name, REDIS_CLUSTER_NAMELEN); node->flags = flags; memset(node->slots,0,sizeof(node->slots)); + node->numslots = 0; node->numslaves = 0; node->slaves = NULL; node->slaveof = NULL; @@ -884,6 +885,8 @@ int clusterProcessPacket(clusterLink *link) { } } } + sender->numslots = + popcount(sender->slots,sizeof(sender->slots)); } } @@ -1332,6 +1335,7 @@ int clusterNodeSetSlotBit(clusterNode *n, int slot) { int bit = slot&7; int old = (n->slots[byte] & (1<slots[byte] |= 1<numslots++; return old; } @@ -1341,6 +1345,7 @@ int clusterNodeClearSlotBit(clusterNode *n, int slot) { int bit = slot&7; int old = (n->slots[byte] & (1<slots[byte] &= ~(1<numslots--; return old; } diff --git a/src/redis.h b/src/redis.h index 6a6d72d92..14d26f44c 100644 --- a/src/redis.h +++ b/src/redis.h @@ -553,6 +553,7 @@ struct clusterNode { char name[REDIS_CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */ int flags; /* REDIS_NODE_... */ unsigned char slots[REDIS_CLUSTER_SLOTS/8]; /* 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 */ struct clusterNode **slaves; /* pointers to slave nodes */ struct clusterNode *slaveof; /* pointer to the master node */