Skip to content

Commit c432dc0

Browse files
caojoshuasimonis
authored andcommittedJan 24, 2024
8322149: ConcurrentHashMap smarter presizing for copy constructor and putAll
Reviewed-by: shade, simonis
1 parent fb822e4 commit c432dc0

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed
 

‎src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ public ConcurrentHashMap(int initialCapacity) {
848848
* @param m the map
849849
*/
850850
public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
851-
this.sizeCtl = DEFAULT_CAPACITY;
851+
this(m.size());
852852
putAll(m);
853853
}
854854

@@ -1084,7 +1084,9 @@ else if (f instanceof ReservationNode)
10841084
* @param m mappings to be stored in this map
10851085
*/
10861086
public void putAll(Map<? extends K, ? extends V> m) {
1087-
tryPresize(m.size());
1087+
if (table != null) {
1088+
tryPresize(size() + m.size());
1089+
}
10881090
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
10891091
putVal(e.getKey(), e.getValue(), false);
10901092
}

‎test/micro/org/openjdk/bench/java/util/concurrent/Maps.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openjdk.jmh.annotations.Measurement;
2929
import org.openjdk.jmh.annotations.Mode;
3030
import org.openjdk.jmh.annotations.OutputTimeUnit;
31+
import org.openjdk.jmh.annotations.Param;
3132
import org.openjdk.jmh.annotations.Scope;
3233
import org.openjdk.jmh.annotations.Setup;
3334
import org.openjdk.jmh.annotations.State;
@@ -48,27 +49,32 @@
4849
public class Maps {
4950
private SimpleRandom rng;
5051
private Map<Integer, Integer> map;
52+
private Map<Integer, Integer> staticMap;
5153
private Integer[] key;
5254

5355
private int removesPerMaxRandom;
5456
private int insertsPerMaxRandom;
5557
private int total;
5658
private int position;
5759

60+
@Param("10000")
61+
private int nkeys;
62+
5863
@Setup
5964
public void initTest() {
60-
int nkeys = 10000;
6165
int pRemove = 10;
6266
int pInsert = 90;
6367
removesPerMaxRandom = (int) ((pRemove / 100.0 * 0x7FFFFFFFL));
6468
insertsPerMaxRandom = (int) ((pInsert / 100.0 * 0x7FFFFFFFL));
6569

6670
rng = new SimpleRandom();
6771
map = new ConcurrentHashMap<>();
72+
staticMap = new ConcurrentHashMap<>();
6873
total = 0;
6974
key = new Integer[nkeys];
7075
for (int i = 0; i < key.length; ++i) {
7176
key[i] = rng.next();
77+
staticMap.put(rng.next(), rng.next());
7278
}
7379
position = key.length / 2;
7480
}
@@ -106,6 +112,21 @@ public void testConcurrentHashMap() {
106112
position = pos;
107113
}
108114

115+
@Benchmark
116+
public ConcurrentHashMap<Integer, Integer> testConcurrentHashMapCopyConstructor() {
117+
return new ConcurrentHashMap<>(staticMap);
118+
}
119+
120+
@Benchmark
121+
public ConcurrentHashMap<Integer, Integer> testConcurrentHashMapPutAll() {
122+
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(nkeys);
123+
for (int i = 0; i < nkeys; ++i) {
124+
map.put(rng.next(), rng.next());
125+
}
126+
map.putAll(staticMap);
127+
return map;
128+
}
129+
109130
private static class SimpleRandom {
110131
private final static long multiplier = 0x5DEECE66DL;
111132
private final static long addend = 0xBL;

0 commit comments

Comments
 (0)
Please sign in to comment.