# HG changeset patch # User igerasim # Date 1522911272 25200 # Wed Apr 04 23:54:32 2018 -0700 # Node ID 13eef7f0467ec2f3e1bc76e5e3993101c32ff767 # Parent 3930c4d4f80594d2cfee88d94abb24837338fc6f [mq]: 8200788-Optimal-initial-capacity-of-java-lang-VarHandle-AccessMode-methodNameToAccessMode diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandle.java b/src/java.base/share/classes/java/lang/invoke/VarHandle.java --- a/src/java.base/share/classes/java/lang/invoke/VarHandle.java +++ b/src/java.base/share/classes/java/lang/invoke/VarHandle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1788,9 +1788,10 @@ static final Map methodNameToAccessMode; static { - // Initial capacity of # values is sufficient to avoid resizes - // for the smallest table size (32) - methodNameToAccessMode = new HashMap<>(AccessMode.values().length); + // Initial capacity of # values divided by the load factor is sufficient + // to avoid resizes for the smallest table size (64) + int initialCapacity = (AccessMode.values().length / 0.75f) + 1; + methodNameToAccessMode = new HashMap<>(initialCapacity); for (AccessMode am : AccessMode.values()) { methodNameToAccessMode.put(am.methodName, am); } diff --git a/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java b/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java --- a/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java +++ b/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java @@ -27,6 +27,7 @@ * @summary Initial capacity of Class.enumConstantDirectory is not optimal * @library /lib/testlibrary * @modules java.base/java.lang:open + * java.base/java.util:open * @build jdk.testlibrary.OptimalCapacity * @run main ConstantDirectoryOptimalCapacity */ diff --git a/test/jdk/java/lang/invoke/VarHandles/AccessMode/OptimalMapSize.java b/test/jdk/java/lang/invoke/VarHandles/AccessMode/OptimalMapSize.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/lang/invoke/VarHandles/AccessMode/OptimalMapSize.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * 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. + */ + +/** + * @test + * @bug 8200788 + * @summary Optimal initial capacity of AccessMode.methodNameToAccessMode + * @library /lib/testlibrary + * @modules java.base/java.lang.invoke:open + * java.base/java.util:open + * @build jdk.testlibrary.OptimalCapacity + * @run main OptimalMapSize + */ + +import java.lang.invoke.VarHandle.AccessMode; +import jdk.testlibrary.OptimalCapacity; + +public class OptimalMapSize { + public static void main(String[] args) throws Throwable { + int initialCapacity = (int)(AccessMode.values().length / 0.75f) + 1; + OptimalCapacity.ofHashMap(AccessMode.class, "methodNameToAccessMode", + initialCapacity); + } +}