8200696: Optimal initial capacity of java.lang.Class.enumConstantDirectory jdk-11+8
authorigerasim
Wed, 04 Apr 2018 14:09:31 -0700
changeset 49521 755e1b55a4df
parent 49520 7a64b48586d8
child 49522 3930c4d4f805
8200696: Optimal initial capacity of java.lang.Class.enumConstantDirectory Reviewed-by: dholmes, redestad
src/java.base/share/classes/java/lang/Class.java
test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java
test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java
--- a/src/java.base/share/classes/java/lang/Class.java	Wed Apr 04 11:26:10 2018 -0400
+++ b/src/java.base/share/classes/java/lang/Class.java	Wed Apr 04 14:09:31 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 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
@@ -3529,7 +3529,7 @@
             if (universe == null)
                 throw new IllegalArgumentException(
                     getName() + " is not an enum type");
-            directory = new HashMap<>(2 * universe.length);
+            directory = new HashMap<>((int)(universe.length / 0.75f) + 1);
             for (T constant : universe) {
                 directory.put(((Enum<?>)constant).name(), constant);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java	Wed Apr 04 14:09:31 2018 -0700
@@ -0,0 +1,113 @@
+/*
+ * 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 8200696
+ * @summary Initial capacity of Class.enumConstantDirectory is not optimal
+ * @library /lib/testlibrary
+ * @modules java.base/java.lang:open
+ * @build jdk.testlibrary.OptimalCapacity
+ * @run main ConstantDirectoryOptimalCapacity
+ */
+
+import jdk.testlibrary.OptimalCapacity;
+
+public class ConstantDirectoryOptimalCapacity {
+
+    public static void main(String[] args) throws Throwable {
+        test(E1.class);
+        test(E2.class);
+        test(E3.class);
+        test(E4.class);
+        test(E5.class);
+        test(E6.class);
+        test(E7.class);
+        test(E8.class);
+        test(E9.class);
+        test(E10.class);
+        test(E11.class);
+        test(E12.class);
+        test(E13.class);
+        test(E14.class);
+        test(E15.class);
+        test(E16.class);
+        test(E17.class);
+        test(E18.class);
+        test(E19.class);
+        test(E20.class);
+        test(E21.class);
+        test(E22.class);
+        test(E23.class);
+        test(E24.class);
+        test(E25.class);
+        test(E26.class);
+    }
+
+    private static void test(Class<? extends Enum> e) {
+        Enum.valueOf(e, "V0"); // trigger init of enumConstantDirectory
+
+        int initialCapacity = (int)(e.getEnumConstants().length / 0.75f) + 1;
+        OptimalCapacity.ofHashMap(e.getClass(), e, "enumConstantDirectory",
+            initialCapacity);
+    }
+
+    enum E1 { V0 }
+    enum E2 { V0, V1 }
+    enum E3 { V0, V1, V2 }
+    enum E4 { V0, V1, V2, V3 }
+    enum E5 { V0, V1, V2, V3, V4 }
+    enum E6 { V0, V1, V2, V3, V4, V5 }
+    enum E7 { V0, V1, V2, V3, V4, V5, V6 }
+    enum E8 { V0, V1, V2, V3, V4, V5, V6, V7 }
+    enum E9 { V0, V1, V2, V3, V4, V5, V6, V7, V8 }
+    enum E10 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9 }
+    enum E11 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10 }
+    enum E12 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11 }
+    enum E13 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12 }
+    enum E14 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13 }
+    enum E15 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14 }
+    enum E16 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15 }
+    enum E17 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16 }
+    enum E18 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17 }
+    enum E19 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18 }
+    enum E20 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19 }
+    enum E21 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20 }
+    enum E22 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20, V21 }
+    enum E23 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20, V21, V22 }
+    enum E24 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20, V21, V22, V23 }
+    enum E25 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24 }
+    enum E26 { V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
+               V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25 }
+}
--- a/test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java	Wed Apr 04 11:26:10 2018 -0400
+++ b/test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java	Wed Apr 04 14:09:31 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -102,10 +102,34 @@
     public static void ofHashMap(Class<?> clazz, String fieldName,
             int initialCapacity)
     {
+        ofHashMap(clazz, null, fieldName, initialCapacity);
+    }
+
+    /**
+     * Checks adequacy of the initial capacity of a non-static field
+     * of type {@code HashMap}.
+     *
+     * Having
+     * <pre>
+     * class XClass {
+     *     HashMap theMap = new HashMap(N);
+     * }
+     * XClass instance = ...
+     * </pre>
+     *
+     * you should call from the test
+     *
+     * <pre>
+     * OptimalCapacity.ofHashMap(XClass.class, instance, "theMap", N);
+     * </pre>
+     */
+    public static void ofHashMap(Class<?> clazz, Object instance,
+            String fieldName, int initialCapacity)
+    {
         try {
             Field field = clazz.getDeclaredField(fieldName);
             field.setAccessible(true);
-            Object obj = field.get(null);
+            Object obj = field.get(instance);
             if (!HashMap.class.equals(obj.getClass())) {
                 throw new RuntimeException(field +
                     " expected to be of type HashMap");