-
Bug
-
Resolution: Fixed
-
P5
-
None
-
b08
-
Verified
The optimal initial capacity of a HashMap depends on the expected number of elements in the map and the load factor.
From the documentation:
https://docs.oracle.com/javase/10/docs/api/java/util/HashMap.html
"If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur."
java.lang.Class.enumConstantDirectory is currently created as
new HashMap<>(2 * universe.length);
which is not optimal for some values of universe.length.
For example, when an enum has 5 elements, the underlying storage created is of size 16, while size 8 would be enough (8 * 0.75 == 6 > 5).
To achieve optimal initial capacity (i.e. such that will be not too low to avoid reallocation and not too high to avoid wasting memory) the formula initialCapacity = (expectedSize / 0.75 + 1) can be used.
From the documentation:
https://docs.oracle.com/javase/10/docs/api/java/util/HashMap.html
"If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur."
java.lang.Class.enumConstantDirectory is currently created as
new HashMap<>(2 * universe.length);
which is not optimal for some values of universe.length.
For example, when an enum has 5 elements, the underlying storage created is of size 16, while size 8 would be enough (8 * 0.75 == 6 > 5).
To achieve optimal initial capacity (i.e. such that will be not too low to avoid reallocation and not too high to avoid wasting memory) the formula initialCapacity = (expectedSize / 0.75 + 1) can be used.
- relates to
-
JDK-8186958 Need method to create pre-sized HashMap
- Resolved