Reset EncoderCache after each server resource reload

This commit is contained in:
embeddedt 2025-12-24 21:02:43 -05:00
parent 9b7a174af8
commit b4024f696d
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
3 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package org.embeddedt.modernfix.common.mixin.perf.encoder_cache_leak;
import net.minecraft.core.component.DataComponents;
import net.minecraft.util.EncoderCache;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(DataComponents.class)
public interface DataComponentsAccessor {
@Accessor("ENCODER_CACHE")
static EncoderCache mfix$getCache() {
throw new AssertionError();
}
}

View File

@ -0,0 +1,12 @@
package org.embeddedt.modernfix.common.mixin.perf.encoder_cache_leak;
import com.google.common.cache.LoadingCache;
import net.minecraft.util.EncoderCache;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(EncoderCache.class)
public interface EncoderCacheAccessor {
@Accessor("cache")
LoadingCache<?, ?> mfix$getCache();
}

View File

@ -0,0 +1,27 @@
package org.embeddedt.modernfix.common.mixin.perf.encoder_cache_leak;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.ReloadableServerResources;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import java.util.concurrent.CompletableFuture;
@Mixin(ReloadableServerResources.class)
public class ReloadableServerResourcesMixin {
/**
* @author embeddedt
* @reason Some mods (e.g. KubeJS) may provide a custom DynamicOps instance during resource reload. This instance
* can end up being strongly retained by an EncoderCache.Key entry even after the reload finishes. The simplest
* fix is to invalidate all entries of the encoder cache after a server-side resource reload, which should not break
* mods, as the cache is not guaranteed to persist entries for any length of time due to using both a maximum size
* & soft values.
*/
@ModifyReturnValue(method = "loadResources", at = @At("RETURN"))
private static CompletableFuture<ReloadableServerResources> resetEncoderCache(CompletableFuture<ReloadableServerResources> future) {
return future.whenComplete((r, t) -> {
((EncoderCacheAccessor)DataComponentsAccessor.mfix$getCache()).mfix$getCache().invalidateAll();
});
}
}