8281631: HashMap copy constructor and putAll can over-allocate table

Reviewed-by: smarks
This commit is contained in:
XenoAmess 2022-03-16 16:48:49 +00:00 committed by Stuart Marks
parent 0cf291bc31
commit 3e393047e1
4 changed files with 265 additions and 79 deletions

View file

@ -213,9 +213,7 @@ public class WeakHashMap<K,V>
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load factor: "+
loadFactor);
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
int capacity = HashMap.tableSizeFor(initialCapacity);
table = newTable(capacity);
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
@ -251,7 +249,7 @@ public class WeakHashMap<K,V>
* @since 1.3
*/
public WeakHashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) ((float)m.size() / DEFAULT_LOAD_FACTOR + 1.0F),
this(Math.max((int) Math.ceil(m.size() / (double)DEFAULT_LOAD_FACTOR),
DEFAULT_INITIAL_CAPACITY),
DEFAULT_LOAD_FACTOR);
putAll(m);
@ -468,7 +466,7 @@ public class WeakHashMap<K,V>
modCount++;
Entry<K,V> e = tab[i];
tab[i] = new Entry<>(k, value, queue, h, e);
if (++size >= threshold)
if (++size > threshold)
resize(tab.length * 2);
return null;
}
@ -557,7 +555,7 @@ public class WeakHashMap<K,V>
* to at most one extra resize.
*/
if (numKeysToBeAdded > threshold) {
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
int targetCapacity = (int)Math.ceil(numKeysToBeAdded / (double)loadFactor);
if (targetCapacity > MAXIMUM_CAPACITY)
targetCapacity = MAXIMUM_CAPACITY;
int newCapacity = table.length;