You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by GitBox <gi...@apache.org> on 2019/03/31 06:27:07 UTC

[GitHub] [groovy] danielsun1106 commented on a change in pull request #903: GROOVY-9060: Make the initial capacity of LinkedHashMap be power of 2…

danielsun1106 commented on a change in pull request #903: GROOVY-9060: Make the initial capacity of LinkedHashMap be power of 2…
URL: https://github.com/apache/groovy/pull/903#discussion_r270651854
 
 

 ##########
 File path: src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
 ##########
 @@ -396,10 +402,33 @@ public static List createList(Object[] values) {
         return answer;
     }
 
+    /**
+     * Get the initial capacity of hash map, which is the closest power of 2 to the entry count.
+     * (SEE https://stackoverflow.com/questions/8352378/why-does-hashmap-require-that-the-initial-capacity-be-a-power-of-two)
+     *
+     * e.g.
+     * 1: 1
+     * 2: 2
+     * 3: 4
+     * 4: 4
+     * 5: 8
+     * 6: 8
+     * 7: 8
+     * 8: 8
+     * 9: 16
+     * ...
+     *
+     * @param initialEntryCnt the initial entry count
+     * @return the initial capacity
+     */
+    public static int initialCapacity(int initialEntryCnt) {
+        return (int) pow(2, ceil(log(initialEntryCnt) / LN2));
 
 Review comment:
   `2<<Integer.highestOneBit​(initialEntryCnt)+1; ` increases its result too fast, e.g. 
   When `initialEntryCnt` is 16, the result has already been 262144 😲
   
   Thanks for your suggestion all the same.
   

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


With regards,
Apache Git Services