thetumbled commented on code in PR #18390:
URL: https://github.com/apache/pulsar/pull/18390#discussion_r1311505247
##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongHashMap.java:
##########
@@ -333,25 +334,23 @@ V get(long key, int keyHash) {
if (!acquiredLock) {
stamp = readLock();
acquiredLock = true;
+
+ // update local variable
+ keys = this.keys;
+ values = this.values;
+ bucket = signSafeMod(keyHash, capacity);
Review Comment:
At this time, `capacity` must be equal to `values.length`.
If we rehash or update the map, the `capacity` will be updated too.
https://github.com/apache/pulsar/blob/0ab1800a3024ed1325d822c4dc6a3267927eca90/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMap.java#L594
Indeed, it would be more friendly if we use `values.length`.
##########
pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMapTest.java:
##########
@@ -173,6 +174,62 @@ public void testExpandAndShrink() {
assertEquals(map.capacity(), 8);
}
+ @Test
+ public void testExpandAndShrinkAndGet() throws Throwable {
+ ConcurrentLongLongPairHashMap map =
ConcurrentLongLongPairHashMap.newBuilder()
Review Comment:
ok.
##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMap.java:
##########
@@ -322,7 +322,10 @@ private static final class Section extends StampedLock {
LongPair get(long key1, long key2, int keyHash) {
long stamp = tryOptimisticRead();
boolean acquiredLock = false;
- int bucket = signSafeMod(keyHash, capacity);
+ // add local variable here, so OutOfBound won't happen
+ long[] table = this.table;
+ // calculate table.length / 4 as capacity to avoid rehash changing
capacity
+ int bucket = signSafeMod(keyHash, table.length / 4);
Review Comment:
For each util class, an item corresponds to multiple array member.
Take `ConcurrentLongLongPairHashMap` for example:
https://github.com/apache/pulsar/blob/0ab1800a3024ed1325d822c4dc6a3267927eca90/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMap.java#L309
Each item corresponds to 4 array member, that is `table.length==capacity*4`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]