blackdrag 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_r270636444
########## 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: How about return 2<<Integer.highestOneBit(initialEntryCnt)+1; ? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services