groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [groovy] danielsun1106 commented on a change in pull request #903: GROOVY-9060: Make the initial capacity of LinkedHashMap be power of 2…
Date Sun, 31 Mar 2019 12:29:10 GMT
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_r270664227
 
 

 ##########
 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:
   I will tweak the current implementation according to your suggestion, which will be more
efficient 😄

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

Mime
View raw message