-
Bug
-
Resolution: Fixed
-
P5
-
None
-
b09
-
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."
Currently, the map is created as
// Initial capacity of # values is sufficient to avoid resizes
// for the smallest table size (32)
methodNameToAccessMode = new HashMap<>(AccessMode.values().length);
Even though the comment suggests that no resizes of the table should occur, in fact the threshold is calculated as 32 * 0.75 = 24, so when 24th element is inserted then the internal storage is reallocated and the content of the table is reinserted.
If the initial capacity were (AccessMode.values().length / 0.75 + 1) then no reallocation would be necessary.
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."
Currently, the map is created as
// Initial capacity of # values is sufficient to avoid resizes
// for the smallest table size (32)
methodNameToAccessMode = new HashMap<>(AccessMode.values().length);
Even though the comment suggests that no resizes of the table should occur, in fact the threshold is calculated as 32 * 0.75 = 24, so when 24th element is inserted then the internal storage is reallocated and the content of the table is reinserted.
If the initial capacity were (AccessMode.values().length / 0.75 + 1) then no reallocation would be necessary.
- relates to
-
JDK-8186958 Need method to create pre-sized HashMap
- Resolved