--- old/src/java.base/share/classes/java/util/HashMap.java 2018-12-17 07:25:13.770953982 +0100 +++ new/src/java.base/share/classes/java/util/HashMap.java 2018-12-17 07:25:13.331952967 +0100 @@ -501,9 +501,14 @@ (int)ft : MAXIMUM_CAPACITY); if (t > threshold) threshold = tableSizeFor(t); + } else { + // Because of linked-list buckets constraints, we cannot + // expand all at once, but can reduce total resize + // effort by repeated doubling now vs later + while (table.length < MAXIMUM_CAPACITY && s > threshold) { + resize(); + } } - else if (s > threshold) - resize(); for (Map.Entry e : m.entrySet()) { K key = e.getKey(); V value = e.getValue(); --- old/test/jdk/java/util/concurrent/tck/JSR166TestCase.java 2018-12-17 07:25:14.886956561 +0100 +++ new/test/jdk/java/util/concurrent/tck/JSR166TestCase.java 2018-12-17 07:25:14.425955496 +0100 @@ -577,6 +577,7 @@ "HashMapTest", "LinkedBlockingDeque8Test", "LinkedBlockingQueue8Test", + "LinkedHashMapTest", "LongAccumulatorTest", "LongAdderTest", "SplittableRandomTest", --- old/test/jdk/java/util/concurrent/tck/MapTest.java 2018-12-17 07:25:15.960959043 +0100 +++ new/test/jdk/java/util/concurrent/tck/MapTest.java 2018-12-17 07:25:15.524958035 +0100 @@ -166,6 +166,20 @@ assertEquals(1, m.size()); } + /** + * "Missing" test found while investigating JDK-8210280. + * See discussion on mailing list. + * TODO: randomize + */ + public void testBug8210280() { + Map m = impl.emptyMap(); + for (int i = 0; i < 4; i++) m.put(7 + i * 16, 0); + Map more = impl.emptyMap(); + for (int i = 0; i < 128; i++) more.put(-i, 42); + m.putAll(more); + for (int i = 0; i < 4; i++) assertEquals(0, m.get(7 + i * 16)); + } + // public void testFailsIntentionallyForDebugging() { // fail(impl.klazz().getSimpleName()); // } --- /dev/null 2018-12-17 06:09:15.256999270 +0100 +++ new/test/jdk/java/util/HashMap/WhiteBoxResizeTest.java 2018-12-17 07:25:16.508960309 +0100 @@ -0,0 +1,91 @@ +/* + * 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.Map; +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 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.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.put(1, 1); + map.putAll(IntStream.range(0, 64).boxed().collect(toMap(i -> i, i -> i))); + + int capacity = ((Object[]) TABLE.get(map)).length; + assertEquals(capacity, 128); + } +} --- /dev/null 2018-12-17 06:09:15.256999270 +0100 +++ new/test/jdk/java/util/concurrent/tck/LinkedHashMapTest.java 2018-12-17 07:25:17.181961864 +0100 @@ -0,0 +1,60 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * 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. + */ + +/* + * This file is available under and governed by the GNU General Public + * License version 2 only, as published by the Free Software Foundation. + * However, the following notice accompanied the original version of this + * file: + * + * Written by Doug Lea and Martin Buchholz with assistance from + * members of JCP JSR-166 Expert Group and released to the public + * domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +import java.util.LinkedHashMap; +import java.util.Map; + +import junit.framework.Test; + +public class LinkedHashMapTest extends JSR166TestCase { + public static void main(String[] args) { + main(suite(), args); + } + + public static Test suite() { + class Implementation implements MapImplementation { + public Class klazz() { return LinkedHashMap.class; } + public Map emptyMap() { return new LinkedHashMap(); } + public Object makeKey(int i) { return i; } + public Object makeValue(int i) { return i; } + public boolean isConcurrent() { return false; } + public boolean permitsNullKeys() { return true; } + public boolean permitsNullValues() { return true; } + public boolean supportsSetValue() { return true; } + } + return newTestSuite( + // LinkedHashMapTest.class, + MapTest.testSuite(new Implementation())); + } +} --- /dev/null 2018-12-17 06:09:15.256999270 +0100 +++ new/test/micro/org/openjdk/bench/java/util/HashMapBench.java 2018-12-17 07:25:17.808963313 +0100 @@ -0,0 +1,77 @@ +/* + * 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.Random; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import static java.util.stream.Collectors.toMap; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class HashMapBench { + private Map bigMapToAdd; + private Random r; + + @Setup + public void setup() { + this.r = new Random(); + this.bigMapToAdd = IntStream.range(0, 1_000_000).boxed().collect(toMap(i -> 7 + i * 128, i -> r.nextInt())); + } + + @Benchmark + public int putAllWithBigMapToNonEmptyMap() { + var map = new HashMap(); + map.put(-1, -1); + map.putAll(bigMapToAdd); + return map.size(); + } + + @Benchmark + public int putAllWithBigMapToEmptyMap() { + var map = new HashMap(); + map.putAll(bigMapToAdd); + return map.size(); + } + + @Benchmark + public int put() { + var map = new HashMap(); + for (int k : bigMapToAdd.keySet()) { + map.put(k, bigMapToAdd.get(k)); + } + return map.size(); + } +}