8200788: Optimal initial capacity of java.lang.VarHandle.AccessMode.methodNameToAccessMode

Reviewed-by: redestad
This commit is contained in:
Ivan Gerasimov 2018-04-07 17:07:13 -07:00
parent 999296bab2
commit 9fe989cc39
3 changed files with 52 additions and 5 deletions
src/java.base/share/classes/java/lang/invoke
test/jdk/java/lang

@ -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,10 +1788,12 @@ public abstract class VarHandle {
static final Map<String, AccessMode> methodNameToAccessMode;
static {
// Initial capacity of # values is sufficient to avoid resizes
// for the smallest table size (32)
methodNameToAccessMode = new HashMap<>(AccessMode.values().length);
for (AccessMode am : AccessMode.values()) {
AccessMode[] values = AccessMode.values();
// Initial capacity of # values divided by the load factor is sufficient
// to avoid resizes for the smallest table size (64)
int initialCapacity = (int)(values.length / 0.75f) + 1;
methodNameToAccessMode = new HashMap<>(initialCapacity);
for (AccessMode am : values) {
methodNameToAccessMode.put(am.methodName, am);
}
}

@ -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
*/

@ -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);
}
}