8200788: Optimal initial capacity of java.lang.VarHandle.AccessMode.methodNameToAccessMode
authorigerasim
Sat, 07 Apr 2018 17:07:13 -0700
changeset 49546 05c1e4d50f9a
parent 49545 5556e9c1e681
child 49547 2f3c0bd6b987
8200788: Optimal initial capacity of java.lang.VarHandle.AccessMode.methodNameToAccessMode Reviewed-by: redestad
src/java.base/share/classes/java/lang/invoke/VarHandle.java
test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java
test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java
--- a/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Fri Apr 06 08:58:22 2018 -0700
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandle.java	Sat Apr 07 17:07:13 2018 -0700
@@ -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 @@
 
         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);
             }
         }
--- a/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java	Fri Apr 06 08:58:22 2018 -0700
+++ b/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java	Sat Apr 07 17:07:13 2018 -0700
@@ -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
  */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java	Sat Apr 07 17:07:13 2018 -0700
@@ -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);
+    }
+}