Skip to content

Commit e0f0357

Browse files
chenshi5012c00603587
and
c00603587
authored
Improve cluster slots slotNodes using arrays (#3373)
* Performance improvement cluster slots slotNodes cache switch hashmap to arrays Signed-off-by: c00603587 <[email protected]> Co-authored-by: c00603587 <[email protected]>
1 parent b2c5acf commit e0f0357

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/main/java/redis/clients/jedis/JedisClusterInfoCache.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis.clients.jedis;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.Collections;
56
import java.util.HashMap;
67
import java.util.HashSet;
@@ -21,8 +22,8 @@
2122
public class JedisClusterInfoCache {
2223

2324
private final Map<String, ConnectionPool> nodes = new HashMap<>();
24-
private final Map<Integer, ConnectionPool> slots = new HashMap<>();
25-
private final Map<Integer, HostAndPort> slotNodes = new HashMap<>();
25+
private final ConnectionPool[] slots = new ConnectionPool[Protocol.CLUSTER_HASHSLOTS];
26+
private final HostAndPort[] slotNodes = new HostAndPort[Protocol.CLUSTER_HASHSLOTS];
2627

2728
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
2829
private final Lock r = rwl.readLock();
@@ -148,8 +149,8 @@ private void discoverClusterSlots(Connection jedis) {
148149
}
149150
w.lock();
150151
try {
151-
this.slots.clear();
152-
this.slotNodes.clear();
152+
Arrays.fill(slots, null);
153+
Arrays.fill(slotNodes, null);
153154
Set<String> hostAndPortKeys = new HashSet<>();
154155

155156
for (Object slotInfoObj : slotsInfo) {
@@ -224,8 +225,8 @@ public void assignSlotToNode(int slot, HostAndPort targetNode) {
224225
w.lock();
225226
try {
226227
ConnectionPool targetPool = setupNodeIfNotExist(targetNode);
227-
slots.put(slot, targetPool);
228-
slotNodes.put(slot, targetNode);
228+
slots[slot] = targetPool;
229+
slotNodes[slot] = targetNode;
229230
} finally {
230231
w.unlock();
231232
}
@@ -236,8 +237,8 @@ public void assignSlotsToNode(List<Integer> targetSlots, HostAndPort targetNode)
236237
try {
237238
ConnectionPool targetPool = setupNodeIfNotExist(targetNode);
238239
for (Integer slot : targetSlots) {
239-
slots.put(slot, targetPool);
240-
slotNodes.put(slot, targetNode);
240+
slots[slot] = targetPool;
241+
slotNodes[slot] = targetNode;
241242
}
242243
} finally {
243244
w.unlock();
@@ -260,7 +261,7 @@ public ConnectionPool getNode(HostAndPort node) {
260261
public ConnectionPool getSlotPool(int slot) {
261262
r.lock();
262263
try {
263-
return slots.get(slot);
264+
return slots[slot];
264265
} finally {
265266
r.unlock();
266267
}
@@ -269,7 +270,7 @@ public ConnectionPool getSlotPool(int slot) {
269270
public HostAndPort getSlotNode(int slot) {
270271
r.lock();
271272
try {
272-
return slotNodes.get(slot);
273+
return slotNodes[slot];
273274
} finally {
274275
r.unlock();
275276
}
@@ -311,8 +312,8 @@ public void reset() {
311312
}
312313
}
313314
nodes.clear();
314-
slots.clear();
315-
slotNodes.clear();
315+
Arrays.fill(slots, null);
316+
Arrays.fill(slotNodes, null);
316317
} finally {
317318
w.unlock();
318319
}

0 commit comments

Comments
 (0)