8322149: ConcurrentHashMap smarter presizing for copy constructor and putAll

Reviewed-by: shade, simonis
This commit is contained in:
Joshua Cao 2024-01-24 18:52:38 +00:00 committed by Volker Simonis
parent fb822e49f2
commit c432dc008b
2 changed files with 26 additions and 3 deletions

View file

@ -848,7 +848,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* @param m the map
*/
public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
this.sizeCtl = DEFAULT_CAPACITY;
this(m.size());
putAll(m);
}
@ -1084,7 +1084,9 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* @param m mappings to be stored in this map
*/
public void putAll(Map<? extends K, ? extends V> m) {
tryPresize(m.size());
if (table != null) {
tryPresize(size() + m.size());
}
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
putVal(e.getKey(), e.getValue(), false);
}