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

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 final Map<String, AccessMode> methodNameToAccessMode;
static { static {
// Initial capacity of # values is sufficient to avoid resizes AccessMode[] values = AccessMode.values();
// for the smallest table size (32) // Initial capacity of # values divided by the load factor is sufficient
methodNameToAccessMode = new HashMap<>(AccessMode.values().length); // to avoid resizes for the smallest table size (64)
for (AccessMode am : AccessMode.values()) { int initialCapacity = (int)(values.length / 0.75f) + 1;
methodNameToAccessMode = new HashMap<>(initialCapacity);
for (AccessMode am : values) {
methodNameToAccessMode.put(am.methodName, am); methodNameToAccessMode.put(am.methodName, am);
} }
} }

View file

@ -27,6 +27,7 @@
* @summary Initial capacity of Class.enumConstantDirectory is not optimal * @summary Initial capacity of Class.enumConstantDirectory is not optimal
* @library /lib/testlibrary * @library /lib/testlibrary
* @modules java.base/java.lang:open * @modules java.base/java.lang:open
* java.base/java.util:open
* @build jdk.testlibrary.OptimalCapacity * @build jdk.testlibrary.OptimalCapacity
* @run main ConstantDirectoryOptimalCapacity * @run main ConstantDirectoryOptimalCapacity
*/ */

View file

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