Skip to content

Commit

Permalink
Use a thread-local for the block model cache map
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Mar 31, 2024
1 parent 0ce683e commit b8fb8e8
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,15 +11,13 @@
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;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Map;
import java.util.concurrent.ExecutionException;

@Mixin(BlockModelShapes.class)
@ClientOnlyMixin
Expand All @@ -35,7 +29,7 @@ public abstract class MixinBlockModelShapes implements IBlockModelShapes {
@Final
@Mutable
private Map<IBlockState, IBakedModel> bakedModelStore;
private final DynamicModelCache<IBlockState> vintage$modelCache = new DynamicModelCache<>(this::getModelForStateSlow, false);
private ThreadLocal<Reference2ReferenceLinkedOpenHashMap<IBlockState, IBakedModel>> vintage$modelCache = ThreadLocal.withInitial(Reference2ReferenceLinkedOpenHashMap::new);

private static StateMapperHandler stateMapperHandler;

Expand All @@ -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();
}

Expand All @@ -68,7 +62,19 @@ private IBakedModel getModelForStateSlow(IBlockState state) {
**/
@Overwrite
public IBakedModel getModelForState(IBlockState state) {
return this.vintage$modelCache.get(state);
Reference2ReferenceLinkedOpenHashMap<IBlockState, IBakedModel> 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
Expand Down

0 comments on commit b8fb8e8

Please sign in to comment.