squah-confluent commented on code in PR #23721:
URL: https://github.com/apache/kafka/pull/23721#discussion_r4193544144
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Utils.java:
##########
@@ -520,4 +521,42 @@ public static long computeTopicHash(String topicName,
CoordinatorMetadataImage m
return hasher.getAsLong();
}
+
+ /**
+ * Constructs a HashMap that can hold the expected number of mappings
without resizing. This is
+ * equivalent to HashMap.newHashMap which is introduced in Java 19.
+ * @param numMappings The expected number of mappings.
+ * @return The newly created map.
+ */
+ public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
+ return new HashMap<>(capacity(numMappings));
+ }
+
+ /**
+ * Constructs a HashSet that can hold the expected number of elements
without resizing. This is
+ * equivalent to HashSet.newHashSet which is introduced in Java 19.
+ * @param numElements The expected number of elements.
+ * @return The newly created set.
+ */
+ public static <E> HashSet<E> newHashSet(int numElements) {
+ return new HashSet<>(capacity(numElements));
+ }
+
+ /**
+ * Constructs a LinkedHashSet that can hold the expected number of
elements without resizing.
+ * This is equivalent to LinkedHashSet.newLinkedHashSet which is
introduced in Java 19.
+ * @param numElements The expected number of elements.
+ * @return The newly created set.
+ */
+ public static <E> LinkedHashSet<E> newLinkedHashSet(int numElements) {
+ return new LinkedHashSet<>(capacity(numElements));
+ }
+
+ /**
+ * @return The smallest initial capacity of a HashMap or HashSet, with the
default load factor
+ * of 0.75, that holds the given number of entries without resizing.
+ */
+ static int capacity(int numEntries) {
Review Comment:
nit: since this is a general utils class, can we be more specific in the
naming?
```suggestion
static int hashCapacity(int numEntries) {
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java:
##########
@@ -327,4 +328,27 @@ void
testAssignmentFromTopicPartitionsWithUnequalEpochLength() {
.count());
}
}
+
+ @Test
+ public void testCapacity() {
+ assertEquals(0, Utils.capacity(0));
+ assertEquals(2, Utils.capacity(1));
+ assertEquals(3, Utils.capacity(2));
+ assertEquals(4, Utils.capacity(3));
+ assertEquals(15, Utils.capacity(11));
+ assertEquals(16, Utils.capacity(12));
+ assertEquals(18, Utils.capacity(13));
+ }
+
+ @Test
+ public void testCapacityIsTheSmallestThatHoldsTheEntriesWithoutResizing() {
+ // A HashMap resizes when its size exceeds its capacity times the load
factor of 0.75.
+ for (int numEntries = 1; numEntries <= 100_000; numEntries++) {
+ int capacity = Utils.capacity(numEntries);
+ assertTrue(capacity * 0.75 >= numEntries,
+ "Capacity " + capacity + " is too small for " + numEntries);
+ assertTrue((capacity - 1) * 0.75 < numEntries,
+ "Capacity " + capacity + " is too large for " + numEntries);
+ }
+ }
Review Comment:
This is really confusing and I couldn't convince myself that it was a
faithful reproduction of the hashmap resize logic at first glance. What
property are we really interested in? I think primarily it's that we can fit
`numEntries` into the map without resizing. That the capacity is the smallest
that accommodates `numEntries` is secondary.
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java:
##########
@@ -327,4 +328,27 @@ void
testAssignmentFromTopicPartitionsWithUnequalEpochLength() {
.count());
}
}
+
+ @Test
+ public void testCapacity() {
+ assertEquals(0, Utils.capacity(0));
+ assertEquals(2, Utils.capacity(1));
+ assertEquals(3, Utils.capacity(2));
+ assertEquals(4, Utils.capacity(3));
+ assertEquals(15, Utils.capacity(11));
+ assertEquals(16, Utils.capacity(12));
+ assertEquals(18, Utils.capacity(13));
+ }
+
+ @Test
+ public void testCapacityIsTheSmallestThatHoldsTheEntriesWithoutResizing() {
+ // A HashMap resizes when its size exceeds its capacity times the load
factor of 0.75.
+ for (int numEntries = 1; numEntries <= 100_000; numEntries++) {
+ int capacity = Utils.capacity(numEntries);
+ assertTrue(capacity * 0.75 >= numEntries,
+ "Capacity " + capacity + " is too small for " + numEntries);
Review Comment:
```suggestion
assertTrue(numEntries <= (int)(capacity * 0.75f),
numEntries + " elements does not fit in capacity " +
capacity);
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java:
##########
@@ -327,4 +328,27 @@ void
testAssignmentFromTopicPartitionsWithUnequalEpochLength() {
.count());
}
}
+
+ @Test
+ public void testCapacity() {
+ assertEquals(0, Utils.capacity(0));
+ assertEquals(2, Utils.capacity(1));
+ assertEquals(3, Utils.capacity(2));
+ assertEquals(4, Utils.capacity(3));
+ assertEquals(15, Utils.capacity(11));
+ assertEquals(16, Utils.capacity(12));
+ assertEquals(18, Utils.capacity(13));
+ }
+
+ @Test
+ public void testCapacityIsTheSmallestThatHoldsTheEntriesWithoutResizing() {
+ // A HashMap resizes when its size exceeds its capacity times the load
factor of 0.75.
Review Comment:
nit: can we communicate the condition less ambiguously?
```suggestion
// A HashMap resizes when its size is strictly greater than its
capacity times the load factor of 0.75, rounded down to the nearest integer.
```
##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/Utils.java:
##########
@@ -520,4 +521,42 @@ public static long computeTopicHash(String topicName,
CoordinatorMetadataImage m
return hasher.getAsLong();
}
+
+ /**
+ * Constructs a HashMap that can hold the expected number of mappings
without resizing. This is
+ * equivalent to HashMap.newHashMap which is introduced in Java 19.
+ * @param numMappings The expected number of mappings.
+ * @return The newly created map.
+ */
+ public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
+ return new HashMap<>(capacity(numMappings));
+ }
+
+ /**
+ * Constructs a HashSet that can hold the expected number of elements
without resizing. This is
+ * equivalent to HashSet.newHashSet which is introduced in Java 19.
+ * @param numElements The expected number of elements.
+ * @return The newly created set.
+ */
+ public static <E> HashSet<E> newHashSet(int numElements) {
+ return new HashSet<>(capacity(numElements));
+ }
+
+ /**
+ * Constructs a LinkedHashSet that can hold the expected number of
elements without resizing.
+ * This is equivalent to LinkedHashSet.newLinkedHashSet which is
introduced in Java 19.
+ * @param numElements The expected number of elements.
+ * @return The newly created set.
+ */
+ public static <E> LinkedHashSet<E> newLinkedHashSet(int numElements) {
+ return new LinkedHashSet<>(capacity(numElements));
+ }
+
+ /**
+ * @return The smallest initial capacity of a HashMap or HashSet, with the
default load factor
+ * of 0.75, that holds the given number of entries without resizing.
+ */
+ static int capacity(int numEntries) {
+ return (int) Math.ceil(numEntries / (double) 0.75f);
Review Comment:
nit: can the cast from a float literal to a double be simplified?
```suggestion
return (int) Math.ceil(numEntries / 0.75f);
```
```suggestion
return (int) Math.ceil(numEntries / 0.75);
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java:
##########
@@ -327,4 +328,27 @@ void
testAssignmentFromTopicPartitionsWithUnequalEpochLength() {
.count());
}
}
+
+ @Test
+ public void testCapacity() {
+ assertEquals(0, Utils.capacity(0));
+ assertEquals(2, Utils.capacity(1));
+ assertEquals(3, Utils.capacity(2));
+ assertEquals(4, Utils.capacity(3));
+ assertEquals(15, Utils.capacity(11));
+ assertEquals(16, Utils.capacity(12));
+ assertEquals(18, Utils.capacity(13));
+ }
+
+ @Test
+ public void testCapacityIsTheSmallestThatHoldsTheEntriesWithoutResizing() {
+ // A HashMap resizes when its size exceeds its capacity times the load
factor of 0.75.
+ for (int numEntries = 1; numEntries <= 100_000; numEntries++) {
+ int capacity = Utils.capacity(numEntries);
+ assertTrue(capacity * 0.75 >= numEntries,
+ "Capacity " + capacity + " is too small for " + numEntries);
+ assertTrue((capacity - 1) * 0.75 < numEntries,
+ "Capacity " + capacity + " is too large for " + numEntries);
Review Comment:
```suggestion
// numEntries no longer fits when shrinking the capacity.
assertFalse(numEntries <= (int)((capacity - 1) * 0.75f),
"Capacity " + capacity + " for " + numEntries + " elements
is oversized");
```
##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/UtilsTest.java:
##########
@@ -327,4 +328,27 @@ void
testAssignmentFromTopicPartitionsWithUnequalEpochLength() {
.count());
}
}
+
+ @Test
+ public void testCapacity() {
+ assertEquals(0, Utils.capacity(0));
+ assertEquals(2, Utils.capacity(1));
+ assertEquals(3, Utils.capacity(2));
+ assertEquals(4, Utils.capacity(3));
+ assertEquals(15, Utils.capacity(11));
+ assertEquals(16, Utils.capacity(12));
+ assertEquals(18, Utils.capacity(13));
+ }
+
+ @Test
+ public void testCapacityIsTheSmallestThatHoldsTheEntriesWithoutResizing() {
Review Comment:
```suggestion
public void testCapacityCanHoldEntriesWithoutResizing() {
```
--
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]