8171373: Reduce copying during initialization of ModuleHashes

Reviewed-by: alanb, mchung, chegar
This commit is contained in:
Claes Redestad 2016-12-17 12:59:15 +01:00
parent be91309965
commit 1a652df643
3 changed files with 20 additions and 9 deletions

View File

@ -149,9 +149,10 @@ public final class ModuleHashes {
*/
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 @@ 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;

View File

@ -124,7 +124,7 @@ public class SystemModuleFinder implements ModuleFinder {
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 @@ public class SystemModuleFinder implements ModuleFinder {
}
}
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];

View File

@ -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,
"<init>", "(Ljava/lang/String;)V", false);
"<init>", "(Ljava/lang/String;I)V", false);
hmv.visitVarInsn(ASTORE, BUILDER_VAR);
hmv.visitVarInsn(ALOAD, BUILDER_VAR);
}