< prev index next >

src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java

Print this page
rev 16336 : 8171373: Reduce copying during initialization of ModuleHashes
Reviewed-by: alanb, mchung, chegar

@@ -122,11 +122,11 @@
 
     // maps module name to module reference
     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();
         int n = names.length;
         moduleCount.add(n);

@@ -160,16 +160,28 @@
                 recordedHashes[i] = attrs.recordedHashes();
                 moduleResolutions[i] = attrs.moduleResolution();
             }
         }
 
+        Map<String, byte[]> hashes = null;
+        boolean secondSeen = false;
         // record the hashes to build HashSupplier
         for (ModuleHashes mh : recordedHashes) {
             if (mh != null) {
+                // 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];
 
         @SuppressWarnings(value = {"rawtypes", "unchecked"})
         Entry<String, ModuleReference>[] map
< prev index next >