changeset 16337:ab164f8b8569

8171373: Reduce copying during initialization of ModuleHashes Reviewed-by: alanb, mchung, chegar
author redestad
date Sat, 17 Dec 2016 12:59:15 +0100
parents 9ce6fade25f2
children 90fbc6050458
files src/java.base/share/classes/jdk/internal/module/ModuleHashes.java src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java
diffstat 3 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java	Fri Dec 16 21:43:29 2016 -0800
+++ b/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java	Sat Dec 17 12:59:15 2016 +0100
@@ -149,9 +149,10 @@
      */
     public static class Builder {
         final String algorithm;
-        Map<String, byte[]> nameToHash;
+        final Map<String, byte[]> nameToHash;
 
-        Builder(String algorithm) {
+        Builder(String algorithm, int initialCapacity) {
+            this.nameToHash = new HashMap<>(initialCapacity);
             this.algorithm =  Objects.requireNonNull(algorithm);
         }
 
@@ -159,9 +160,6 @@
          * Sets the module hash for the given module name
          */
         public Builder hashForModule(String mn, byte[] hash) {
-            if (nameToHash == null)
-                nameToHash = new HashMap<>();
-
             nameToHash.put(mn, hash);
             return this;
         }
@@ -170,7 +168,7 @@
          * Builds a {@code ModuleHashes}.
          */
         public ModuleHashes build() {
-            if (nameToHash != null) {
+            if (!nameToHash.isEmpty()) {
                 return new ModuleHashes(algorithm, nameToHash);
             } else {
                 return null;
--- a/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java	Fri Dec 16 21:43:29 2016 -0800
+++ b/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java	Sat Dec 17 12:59:15 2016 +0100
@@ -124,7 +124,7 @@
     private final Map<String, ModuleReference> nameToModule;
 
     // module name to hashes
-    private final Map<String, byte[]> hashes = new HashMap<>();
+    private final Map<String, byte[]> hashes;
 
     private SystemModuleFinder() {
         String[] names = moduleNames();
@@ -162,12 +162,24 @@
             }
         }
 
+        Map<String, byte[]> hashes = null;
+        boolean secondSeen = false;
         // record the hashes to build HashSupplier
         for (ModuleHashes mh : recordedHashes) {
             if (mh != null) {
-                hashes.putAll(mh.hashes());
+                // if only one module contain ModuleHashes, use it
+                if (hashes == null) {
+                    hashes = mh.hashes();
+                } else {
+                    if (!secondSeen) {
+                        hashes = new HashMap<>(hashes);
+                        secondSeen = true;
+                    }
+                    hashes.putAll(mh.hashes());
+                }
             }
         }
+        this.hashes = (hashes == null) ? Map.of() : hashes;
 
         ModuleReference[] mods = new ModuleReference[n];
 
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java	Fri Dec 16 21:43:29 2016 -0800
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java	Sat Dec 17 12:59:15 2016 +0100
@@ -967,8 +967,9 @@
                 hmv.visitTypeInsn(NEW, MODULE_HASHES_BUILDER);
                 hmv.visitInsn(DUP);
                 hmv.visitLdcInsn(recordedHashes.algorithm());
+                pushInt(hmv, ((4 * recordedHashes.names().size()) / 3) + 1);
                 hmv.visitMethodInsn(INVOKESPECIAL, MODULE_HASHES_BUILDER,
-                    "<init>", "(Ljava/lang/String;)V", false);
+                    "<init>", "(Ljava/lang/String;I)V", false);
                 hmv.visitVarInsn(ASTORE, BUILDER_VAR);
                 hmv.visitVarInsn(ALOAD, BUILDER_VAR);
             }