Make the block model cache thread-local instead of using a lock

This commit is contained in:
embeddedt 2024-03-04 16:37:50 -05:00
parent ad1e12a3bb
commit a45783647f
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -1,12 +1,12 @@
package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; 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.renderer.block.BlockModelShaper;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelManager; import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.dynamicresources.DynamicModelCache;
import org.embeddedt.modernfix.dynamicresources.ModelLocationCache; import org.embeddedt.modernfix.dynamicresources.ModelLocationCache;
import org.embeddedt.modernfix.util.DynamicOverridableMap; import org.embeddedt.modernfix.util.DynamicOverridableMap;
import org.spongepowered.asm.mixin.*; import org.spongepowered.asm.mixin.*;
@ -25,7 +25,7 @@ public class BlockModelShaperMixin {
@Shadow @Final @Mutable @Shadow @Final @Mutable
private Map<BlockState, BakedModel> modelByStateCache; private Map<BlockState, BakedModel> modelByStateCache;
private final DynamicModelCache<BlockState> mfix$modelCache = new DynamicModelCache<>(k -> this.cacheBlockModel((BlockState)k), false); private ThreadLocal<Reference2ReferenceLinkedOpenHashMap<BlockState, BakedModel>> mfix$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new);
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void replaceModelMap(CallbackInfo ci) { private void replaceModelMap(CallbackInfo ci) {
@ -39,7 +39,7 @@ public class BlockModelShaperMixin {
*/ */
@Overwrite @Overwrite
public void rebuildCache() { public void rebuildCache() {
this.mfix$modelCache.clear(); this.mfix$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new);
} }
private BakedModel cacheBlockModel(BlockState state) { private BakedModel cacheBlockModel(BlockState state) {
@ -59,6 +59,18 @@ public class BlockModelShaperMixin {
*/ */
@Overwrite @Overwrite
public BakedModel getBlockModel(BlockState state) { public BakedModel getBlockModel(BlockState state) {
return this.mfix$modelCache.get(state); Reference2ReferenceLinkedOpenHashMap<BlockState, BakedModel> 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;
} }
} }