From b8fb8e8473123dac08ace07aca2692181147a556 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sun, 31 Mar 2024 14:41:41 -0400 Subject: [PATCH] Use a thread-local for the block model cache map --- .../MixinBlockModelShapes.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/embeddedt/vintagefix/mixin/dynamic_resources/MixinBlockModelShapes.java b/src/main/java/org/embeddedt/vintagefix/mixin/dynamic_resources/MixinBlockModelShapes.java index 595424c..7759781 100644 --- a/src/main/java/org/embeddedt/vintagefix/mixin/dynamic_resources/MixinBlockModelShapes.java +++ b/src/main/java/org/embeddedt/vintagefix/mixin/dynamic_resources/MixinBlockModelShapes.java @@ -1,10 +1,6 @@ package org.embeddedt.vintagefix.mixin.dynamic_resources; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import com.google.common.collect.ImmutableMap; -import net.minecraft.block.Block; +import it.unimi.dsi.fastutil.objects.Reference2ReferenceLinkedOpenHashMap; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.BlockModelShapes; import net.minecraft.client.renderer.block.model.IBakedModel; @@ -15,7 +11,6 @@ import org.embeddedt.vintagefix.dynamicresources.IBlockModelShapes; import org.embeddedt.vintagefix.dynamicresources.StateMapperHandler; import org.embeddedt.vintagefix.dynamicresources.model.DynamicBakedModelProvider; -import org.embeddedt.vintagefix.dynamicresources.model.DynamicModelCache; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -23,7 +18,6 @@ import org.spongepowered.asm.mixin.Shadow; import java.util.Map; -import java.util.concurrent.ExecutionException; @Mixin(BlockModelShapes.class) @ClientOnlyMixin @@ -35,7 +29,7 @@ public abstract class MixinBlockModelShapes implements IBlockModelShapes { @Final @Mutable private Map bakedModelStore; - private final DynamicModelCache vintage$modelCache = new DynamicModelCache<>(this::getModelForStateSlow, false); + private ThreadLocal> vintage$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new); private static StateMapperHandler stateMapperHandler; @@ -49,7 +43,7 @@ public void reloadModels() { if(stateMapperHandler == null) { stateMapperHandler = new StateMapperHandler(this.blockStateMapper); } - this.vintage$modelCache.clear(); + this.vintage$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new); this.bakedModelStore = DynamicBakedModelProvider.instance.getBakedModelStore(); } @@ -68,7 +62,19 @@ private IBakedModel getModelForStateSlow(IBlockState state) { **/ @Overwrite public IBakedModel getModelForState(IBlockState state) { - return this.vintage$modelCache.get(state); + Reference2ReferenceLinkedOpenHashMap map = this.vintage$modelCache.get(); + IBakedModel model = map.get(state); + + if(model != null) { + return model; + } + + model = this.getModelForStateSlow(state); + map.putAndMoveToFirst(state, model); + if(map.size() > 500) { + map.removeLast(); + } + return model; } @Override