From a45783647fc199d4b08c9cc6575cce314790ad6a Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:37:50 -0500 Subject: [PATCH] Make the block model cache thread-local instead of using a lock --- .../BlockModelShaperMixin.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockModelShaperMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockModelShaperMixin.java index bed4cb16..11f99989 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockModelShaperMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/BlockModelShaperMixin.java @@ -1,12 +1,12 @@ package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; +import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap; import net.minecraft.client.renderer.block.BlockModelShaper; import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.ModelManager; import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.world.level.block.state.BlockState; import org.embeddedt.modernfix.annotation.ClientOnlyMixin; -import org.embeddedt.modernfix.dynamicresources.DynamicModelCache; import org.embeddedt.modernfix.dynamicresources.ModelLocationCache; import org.embeddedt.modernfix.util.DynamicOverridableMap; import org.spongepowered.asm.mixin.*; @@ -25,7 +25,7 @@ public class BlockModelShaperMixin { @Shadow @Final @Mutable private Map modelByStateCache; - private final DynamicModelCache mfix$modelCache = new DynamicModelCache<>(k -> this.cacheBlockModel((BlockState)k), false); + private ThreadLocal> mfix$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new); @Inject(method = "", at = @At("RETURN")) private void replaceModelMap(CallbackInfo ci) { @@ -39,7 +39,7 @@ public class BlockModelShaperMixin { */ @Overwrite public void rebuildCache() { - this.mfix$modelCache.clear(); + this.mfix$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new); } private BakedModel cacheBlockModel(BlockState state) { @@ -59,6 +59,18 @@ public class BlockModelShaperMixin { */ @Overwrite public BakedModel getBlockModel(BlockState state) { - return this.mfix$modelCache.get(state); + Reference2ReferenceLinkedOpenHashMap map = this.mfix$modelCache.get(); + BakedModel model = map.get(state); + + if(model != null) { + return model; + } + + model = this.cacheBlockModel(state); + map.putAndMoveToFirst(state, model); + if(map.size() > 500) { + map.removeLast(); + } + return model; } }