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

@ -495,9 +495,9 @@ public class HashMap<K,V> extends AbstractMap<K,V>
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
double dt = Math.ceil(s / (double)loadFactor);
int t = ((dt < (double)MAXIMUM_CAPACITY) ?
(int)dt : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
} else {
@ -1527,12 +1527,12 @@ public class HashMap<K,V> extends AbstractMap<K,V>
} else if (mappings == 0) {
// use defaults
} else if (mappings > 0) {
float fc = (float)mappings / lf + 1.0f;
int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
double dc = Math.ceil(mappings / (double)lf);
int cap = ((dc < DEFAULT_INITIAL_CAPACITY) ?
DEFAULT_INITIAL_CAPACITY :
(fc >= MAXIMUM_CAPACITY) ?
(dc >= MAXIMUM_CAPACITY) ?
MAXIMUM_CAPACITY :
tableSizeFor((int)fc));
tableSizeFor((int)dc));
float ft = (float)cap * lf;
threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
(int)ft : Integer.MAX_VALUE);