Add eviction to some DFU caches

This commit is contained in:
embeddedt 2023-04-16 20:14:42 -04:00
parent 1602d3352f
commit ad7bc8829b
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
3 changed files with 53 additions and 0 deletions

View File

@ -112,9 +112,11 @@ tasks.withType(JavaCompile) {
// JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used.
// We'll use that if it's available, but otherwise we'll use the older option.
def targetVersion = 8
/*
if (JavaVersion.current().isJava9Compatible()) {
options.release = targetVersion
}
*/
}
java {

View File

@ -13,6 +13,7 @@ import org.embeddedt.modernfix.classloading.FastAccessTransformerList;
import org.embeddedt.modernfix.classloading.ModernFixResourceFinder;
import org.embeddedt.modernfix.core.config.ModernFixEarlyConfig;
import org.embeddedt.modernfix.core.config.Option;
import org.embeddedt.modernfix.dfu.DFUBlaster;
import org.embeddedt.modernfix.load.ModWorkManagerQueue;
import org.embeddedt.modernfix.util.DummyList;
import org.objectweb.asm.Opcodes;
@ -85,6 +86,7 @@ public class ModernFixMixinPlugin implements IMixinConfigPlugin {
FastAccessTransformerList.attemptReplace();
ModWorkManagerQueue.replace();
DFUBlaster.blastMaps();
/* https://github.com/FabricMC/Mixin/pull/99 */
try {

View File

@ -0,0 +1,49 @@
package org.embeddedt.modernfix.dfu;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.mojang.datafixers.RewriteResult;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.functions.PointFreeRule;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.util.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.embeddedt.modernfix.ModernFix;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.IntFunction;
public class DFUBlaster {
public static void blastMaps() {
Cache<Pair<IntFunction<RewriteResult<?, ?>>, Integer>, RewriteResult<?, ?>> hmapApplyCache = CacheBuilder.newBuilder()
.maximumSize(200) /* should mean approximately 50MB used max */
.expireAfterAccess(3, TimeUnit.MINUTES)
.build();
Cache<Triple<Type<?>, TypeRewriteRule, PointFreeRule>, Optional<? extends RewriteResult<?, ?>>> rewriteCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(3, TimeUnit.MINUTES)
.build();
try {
Class<?> FOLD_CLASS = Class.forName("com.mojang.datafixers.functions.Fold");
Field hmapField = FOLD_CLASS.getDeclaredField("HMAP_APPLY_CACHE");
hmapField.setAccessible(true);
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe)theUnsafe.get(null);
Object base = unsafe.staticFieldBase(hmapField);
long offset = unsafe.staticFieldOffset(hmapField);
unsafe.putObject(base, offset, hmapApplyCache.asMap());
Field rewriteCacheField = Type.class.getDeclaredField("REWRITE_CACHE");
rewriteCacheField.setAccessible(true);
base = unsafe.staticFieldBase(rewriteCacheField);
offset = unsafe.staticFieldOffset(rewriteCacheField);
unsafe.putObject(base, offset, rewriteCache.asMap());
} catch(Throwable e) {
ModernFix.LOGGER.error("Could not replace DFU map", e);
}
}
}