--- old/src/java.base/share/classes/java/util/HashMap.java 2018-12-12 14:30:31.224754209 +0100 +++ new/src/java.base/share/classes/java/util/HashMap.java 2018-12-12 14:30:30.877753733 +0100 @@ -501,9 +501,9 @@ (int)ft : MAXIMUM_CAPACITY); if (t > threshold) threshold = tableSizeFor(t); + } else if (s > threshold) { + resize(s); } - else if (s > threshold) - resize(); for (Map.Entry e : m.entrySet()) { K key = e.getKey(); V value = e.getValue(); @@ -661,6 +661,29 @@ } /** + * Resize the table to the nearest power of two to {@code size}. Moves all items to the new table. + * + * @param size expected number of elements in the new table + * @return the table + */ + final Node[] resize(int size) { + if (size < 0) { + throw new IllegalArgumentException("Negative number of elements does not make sense."); + } + Node[] oldTable = table; + int oldCapacity = (oldTable == null) ? 0 : oldTable.length; + int newCapacity = tableSizeFor(size); + + // need to resize? + if (newCapacity > oldCapacity) { + threshold = (int) ((float) newCapacity * loadFactor); + return createTableAndMoveElements(newCapacity, oldCapacity, oldTable); + } else { + return oldTable; + } + } + + /** * Initializes or doubles table size. If null, allocates in * accord with initial capacity target held in field threshold. * Otherwise, because we are using power-of-two expansion, the @@ -695,36 +718,42 @@ (int)ft : Integer.MAX_VALUE); } threshold = newThr; - @SuppressWarnings({"rawtypes","unchecked"}) - Node[] newTab = (Node[])new Node[newCap]; + + return createTableAndMoveElements(newCap, oldCap, oldTab); + } + + private Node[] createTableAndMoveElements(int newCap, int oldCap, Node[] oldTab) { + @SuppressWarnings({"rawtypes", "unchecked"}) + Node[] newTab = (Node[]) new Node[newCap]; table = newTab; if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node e; if ((e = oldTab[j]) != null) { oldTab[j] = null; - if (e.next == null) + if (e.next == null) { newTab[e.hash & (newCap - 1)] = e; - else if (e instanceof TreeNode) - ((TreeNode)e).split(this, newTab, j, oldCap); - else { // preserve order + } else if (e instanceof TreeNode) { + ((TreeNode) e).split(this, newTab, j, oldCap); + } else { // preserve order Node loHead = null, loTail = null; Node hiHead = null, hiTail = null; Node next; do { next = e.next; if ((e.hash & oldCap) == 0) { - if (loTail == null) + if (loTail == null) { loHead = e; - else + } else { loTail.next = e; + } loTail = e; - } - else { - if (hiTail == null) + } else { + if (hiTail == null) { hiHead = e; - else + } else { hiTail.next = e; + } hiTail = e; } } while ((e = next) != null); --- /dev/null 2018-12-12 12:44:44.997999841 +0100 +++ new/test/jdk/java/util/HashMap/WhiteBoxResizeTest.java 2018-12-12 14:30:31.734754909 +0100 @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2018, Red Hat, Inc. All rights reserved. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import org.testng.annotations.Test; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.invoke.VarHandle; +import java.util.HashMap; +import java.util.stream.IntStream; + +import static java.util.stream.Collectors.toMap; +import static org.testng.Assert.assertEquals; + +/* + * @test + * @bug 8210280 + * @modules java.base/java.util:open + * @summary White box tests for HashMap internals around table resize + * @run testng WhiteBoxResizeTest + */ +public class WhiteBoxResizeTest { + final MethodHandle TABLE_SIZE_FOR; + final MethodHandle RESIZE; + final VarHandle THRESHOLD; + final VarHandle TABLE; + + public WhiteBoxResizeTest() throws ReflectiveOperationException { + Class mClass = HashMap.class; + String nodeClassName = mClass.getName() + "$Node"; + Class nodeArrayClass = Class.forName("[L" + nodeClassName + ";"); + MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup()); + TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass); + this.TABLE_SIZE_FOR = lookup.findStatic( + mClass, "tableSizeFor", + MethodType.methodType(int.class, int.class)); + this.RESIZE = lookup.findVirtual(mClass, "resize", MethodType.methodType(nodeArrayClass, int.class)); + this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class); + } + + int tableSizeFor(int n) { + try { + return (int) TABLE_SIZE_FOR.invoke(n); + } catch (Throwable t) { throw new AssertionError(t); } + } + + @Test + public void testTableSizeFor() { + assertEquals(tableSizeFor(0), 1); + assertEquals(tableSizeFor(1), 1); + assertEquals(tableSizeFor(2), 2); + assertEquals(tableSizeFor(3), 4); + assertEquals(tableSizeFor(15), 16); + assertEquals(tableSizeFor(16), 16); + assertEquals(tableSizeFor(17), 32); + int maxSize = 1 << 30; + assertEquals(tableSizeFor(maxSize - 1), maxSize); + assertEquals(tableSizeFor(maxSize), maxSize); + assertEquals(tableSizeFor(maxSize + 1), maxSize); + assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize); + } + + @Test + public void putAllCapacityTest() { + var map = new HashMap(); + map.putAll(IntStream.range(0, 10).boxed().collect(toMap(i -> i, i -> i))); + + int capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 16); + } + + @Test + public void callResizeWithSizeTest() throws Throwable { + var map = new HashMap(); + + RESIZE.invoke(map, 0); + int capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 1); + + RESIZE.invoke(map, 15); + capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 16); + + RESIZE.invoke(map, 256); + capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 256); + + RESIZE.invoke(map, 257); + capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 512); + } +} --- /dev/null 2018-12-12 12:44:44.997999841 +0100 +++ new/test/micro/org/openjdk/bench/java/util/HashMapBench.java 2018-12-12 14:30:32.518755985 +0100 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018, Red Hat, Inc. All rights reserved. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package org.openjdk.bench.java.util; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class HashMapBench { + private Map bigMapToAdd; + + @Setup + public void setup() { + this.bigMapToAdd = IntStream.range(0, 10_000_000).boxed().collect(Collectors.toMap(i -> i, i -> i)); + } + + @Benchmark + public int putAllWithBigMap() { + var map = new HashMap(); + map.put(-1, -1); + map.putAll(bigMapToAdd); + return map.size(); + } +}