From 1a652df64325acc38783cf8a66cbf53fa706bacb Mon Sep 17 00:00:00 2001 From: Claes Redestad Date: Sat, 17 Dec 2016 12:59:15 +0100 Subject: [PATCH] 8171373: Reduce copying during initialization of ModuleHashes Reviewed-by: alanb, mchung, chegar --- .../jdk/internal/module/ModuleHashes.java | 10 ++++------ .../jdk/internal/module/SystemModuleFinder.java | 16 ++++++++++++++-- .../internal/plugins/SystemModulesPlugin.java | 3 ++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/jdk/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java b/jdk/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java index 641127b894f..f4e5fcb9fe1 100644 --- a/jdk/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java +++ b/jdk/src/java.base/share/classes/jdk/internal/module/ModuleHashes.java @@ -149,9 +149,10 @@ public final class ModuleHashes { */ public static class Builder { final String algorithm; - Map nameToHash; + final Map nameToHash; - Builder(String algorithm) { + Builder(String algorithm, int initialCapacity) { + this.nameToHash = new HashMap<>(initialCapacity); this.algorithm = Objects.requireNonNull(algorithm); } @@ -159,9 +160,6 @@ public final class ModuleHashes { * 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 @@ public final class ModuleHashes { * Builds a {@code ModuleHashes}. */ public ModuleHashes build() { - if (nameToHash != null) { + if (!nameToHash.isEmpty()) { return new ModuleHashes(algorithm, nameToHash); } else { return null; diff --git a/jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java b/jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java index cb7fa0a8d13..5ec0a62ca69 100644 --- a/jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java +++ b/jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java @@ -124,7 +124,7 @@ public class SystemModuleFinder implements ModuleFinder { private final Map nameToModule; // module name to hashes - private final Map hashes = new HashMap<>(); + private final Map hashes; private SystemModuleFinder() { String[] names = moduleNames(); @@ -162,12 +162,24 @@ public class SystemModuleFinder implements ModuleFinder { } } + Map 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]; diff --git a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java index 6adb3d699b5..d7cd59a398e 100644 --- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java +++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java @@ -967,8 +967,9 @@ public final class SystemModulesPlugin implements Plugin { 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, - "", "(Ljava/lang/String;)V", false); + "", "(Ljava/lang/String;I)V", false); hmv.visitVarInsn(ASTORE, BUILDER_VAR); hmv.visitVarInsn(ALOAD, BUILDER_VAR); }