Deduplicate empty byte arrays
This commit is contained in:
parent
41eef0b6ab
commit
ac99791d3a
|
|
@ -41,6 +41,8 @@ public class ModernFixCachingClassTransformer extends ClassTransformer {
|
||||||
|
|
||||||
private ConcurrentHashMap<String, Pair<List<byte[]>, byte[]>> transformationCache;
|
private ConcurrentHashMap<String, Pair<List<byte[]>, byte[]>> transformationCache;
|
||||||
|
|
||||||
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
||||||
|
|
||||||
private static File childFile(File file) {
|
private static File childFile(File file) {
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
return file;
|
return file;
|
||||||
|
|
@ -85,6 +87,19 @@ public class ModernFixCachingClassTransformer extends ClassTransformer {
|
||||||
this.transformationCache = new ConcurrentHashMap<>();
|
this.transformationCache = new ConcurrentHashMap<>();
|
||||||
try(ObjectInputStream inStream = new ObjectInputStream(new FileInputStream(CLASS_CACHE_DAT))) {
|
try(ObjectInputStream inStream = new ObjectInputStream(new FileInputStream(CLASS_CACHE_DAT))) {
|
||||||
this.transformationCache = (ConcurrentHashMap<String, Pair<List<byte[]>,byte[]>>)inStream.readObject();
|
this.transformationCache = (ConcurrentHashMap<String, Pair<List<byte[]>,byte[]>>)inStream.readObject();
|
||||||
|
/* Deduplicate any empty byte arrays to minimize impact of empty classes */
|
||||||
|
List<String> keys = new ArrayList<>(this.transformationCache.keySet());
|
||||||
|
for(String key : keys) {
|
||||||
|
Pair<List<byte[]>,byte[]> pair = this.transformationCache.get(key);
|
||||||
|
for(int i = 0; i < pair.getLeft().size(); i++) {
|
||||||
|
if(pair.getLeft().get(i).length == 0) {
|
||||||
|
pair.getLeft().set(i, EMPTY_BYTE_ARRAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(pair.getRight().length == 0) {
|
||||||
|
this.transformationCache.put(key, Pair.of(pair.getLeft(), EMPTY_BYTE_ARRAY));
|
||||||
|
}
|
||||||
|
}
|
||||||
int size = 0;
|
int size = 0;
|
||||||
/* Approximate the size in bytes */
|
/* Approximate the size in bytes */
|
||||||
for(Map.Entry<String, Pair<List<byte[]>,byte[]>> entry : this.transformationCache.entrySet()) {
|
for(Map.Entry<String, Pair<List<byte[]>,byte[]>> entry : this.transformationCache.entrySet()) {
|
||||||
|
|
@ -172,6 +187,8 @@ public class ModernFixCachingClassTransformer extends ClassTransformer {
|
||||||
LOGGER.warn("Hashes have changed, discarding cached version of " + name);
|
LOGGER.warn("Hashes have changed, discarding cached version of " + name);
|
||||||
}
|
}
|
||||||
byte[] transformed = super.transform(inputClass, name, reason);
|
byte[] transformed = super.transform(inputClass, name, reason);
|
||||||
|
if(transformed.length == 0)
|
||||||
|
transformed = EMPTY_BYTE_ARRAY; /* deduplicate */
|
||||||
return Pair.of(hashList, transformed);
|
return Pair.of(hashList, transformed);
|
||||||
}
|
}
|
||||||
}).getRight();
|
}).getRight();
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package org.embeddedt.modernfix.classloading.hashers;
|
||||||
|
|
||||||
import com.google.common.collect.HashMultimap;
|
import com.google.common.collect.HashMultimap;
|
||||||
import com.google.common.io.Resources;
|
import com.google.common.io.Resources;
|
||||||
|
import cpw.mods.modlauncher.ModernFixCachingClassTransformer;
|
||||||
import org.spongepowered.asm.launch.IClassProcessor;
|
import org.spongepowered.asm.launch.IClassProcessor;
|
||||||
import org.spongepowered.asm.launch.MixinLaunchPluginLegacy;
|
import org.spongepowered.asm.launch.MixinLaunchPluginLegacy;
|
||||||
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
|
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
|
||||||
|
|
@ -92,6 +93,6 @@ public class MixinTransformerHasher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hashesByClass.getOrDefault(className, new byte[0]);
|
return hashesByClass.getOrDefault(className, ModernFixCachingClassTransformer.EMPTY_BYTE_ARRAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user