You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/03/17 00:59:59 UTC

[GitHub] [kafka] jsancio commented on a change in pull request #10334: MINOR: Fix BaseHashTable sizing

jsancio commented on a change in pull request #10334:
URL: https://github.com/apache/kafka/pull/10334#discussion_r595640017



##########
File path: metadata/src/main/java/org/apache/kafka/timeline/BaseHashTable.java
##########
@@ -56,12 +58,30 @@
         this.elements = new Object[expectedSizeToCapacity(expectedSize)];
     }
 
+    /**
+     * Calculate the capacity we should provision, given the expected size.
+     *
+     * Our capacity must always be a power of 2, and never less than 2.
+     */
     static int expectedSizeToCapacity(int expectedSize) {
-        if (expectedSize <= 1) {
-            return 2;
+        if (expectedSize >= MAX_CAPACITY / 2) {
+            return MAX_CAPACITY;
+        }
+        return Math.max(MIN_CAPACITY, roundUpToPowerOfTwo(expectedSize * 2));
+    }
+
+    private static int roundUpToPowerOfTwo(int i) {
+        if (i < 0) {
+            return 0;
         }
-        double sizeToFit = expectedSize / MAX_LOAD_FACTOR;
-        return (int) Math.min(MAX_CAPACITY, Math.ceil(Math.log(sizeToFit) / LN_2));
+        i = i - 1;
+        i |= i >> 1;
+        i |= i >> 2;
+        i |= i >> 4;
+        i |= i >> 8;
+        i |= i >> 16;
+        i = i + 1;
+        return i < 0 ? MAX_CAPACITY : i;

Review comment:
       Or https://github.com/google/guava/blob/master/guava/src/com/google/common/math/IntMath.java#L56-L72
   
   It is safe to look as it is Apache License 2.0.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org