From e0f035785ad4f35ae41a216e4a7ecd03cb08761b Mon Sep 17 00:00:00 2001 From: chenshi Date: Thu, 27 Apr 2023 12:48:28 +0800 Subject: [PATCH] Improve cluster slots slotNodes using arrays (#3373) * Performance improvement cluster slots slotNodes cache switch hashmap to arrays Signed-off-by: c00603587 Co-authored-by: c00603587 --- .../clients/jedis/JedisClusterInfoCache.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 324e059079..4bd9408ae8 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -21,8 +22,8 @@ public class JedisClusterInfoCache { private final Map nodes = new HashMap<>(); - private final Map slots = new HashMap<>(); - private final Map slotNodes = new HashMap<>(); + private final ConnectionPool[] slots = new ConnectionPool[Protocol.CLUSTER_HASHSLOTS]; + private final HostAndPort[] slotNodes = new HostAndPort[Protocol.CLUSTER_HASHSLOTS]; private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); @@ -148,8 +149,8 @@ private void discoverClusterSlots(Connection jedis) { } w.lock(); try { - this.slots.clear(); - this.slotNodes.clear(); + Arrays.fill(slots, null); + Arrays.fill(slotNodes, null); Set hostAndPortKeys = new HashSet<>(); for (Object slotInfoObj : slotsInfo) { @@ -224,8 +225,8 @@ public void assignSlotToNode(int slot, HostAndPort targetNode) { w.lock(); try { ConnectionPool targetPool = setupNodeIfNotExist(targetNode); - slots.put(slot, targetPool); - slotNodes.put(slot, targetNode); + slots[slot] = targetPool; + slotNodes[slot] = targetNode; } finally { w.unlock(); } @@ -236,8 +237,8 @@ public void assignSlotsToNode(List targetSlots, HostAndPort targetNode) try { ConnectionPool targetPool = setupNodeIfNotExist(targetNode); for (Integer slot : targetSlots) { - slots.put(slot, targetPool); - slotNodes.put(slot, targetNode); + slots[slot] = targetPool; + slotNodes[slot] = targetNode; } } finally { w.unlock(); @@ -260,7 +261,7 @@ public ConnectionPool getNode(HostAndPort node) { public ConnectionPool getSlotPool(int slot) { r.lock(); try { - return slots.get(slot); + return slots[slot]; } finally { r.unlock(); } @@ -269,7 +270,7 @@ public ConnectionPool getSlotPool(int slot) { public HostAndPort getSlotNode(int slot) { r.lock(); try { - return slotNodes.get(slot); + return slotNodes[slot]; } finally { r.unlock(); } @@ -311,8 +312,8 @@ public void reset() { } } nodes.clear(); - slots.clear(); - slotNodes.clear(); + Arrays.fill(slots, null); + Arrays.fill(slotNodes, null); } finally { w.unlock(); }