diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/BlockModelShaperMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/BlockModelShaperMixin.java new file mode 100644 index 00000000..9da0fed8 --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/parallelize_model_loading/BlockModelShaperMixin.java @@ -0,0 +1,26 @@ +package org.embeddedt.modernfix.mixin.perf.parallelize_model_loading; + +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import net.minecraft.client.renderer.block.BlockModelShaper; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.Property; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@Mixin(BlockModelShaper.class) +public class BlockModelShaperMixin { + private static Map stateToPropertiesCache = new ConcurrentHashMap<>(); + + @Redirect(method = "stateToModelLocation(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/world/level/block/state/BlockState;)Lnet/minecraft/client/resources/model/ModelResourceLocation;", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/block/BlockModelShaper;statePropertiesToString(Ljava/util/Map;)Ljava/lang/String;")) + private static String getCachedProperty(Map, Comparable> values, ResourceLocation location, BlockState state) { + /* We intentionally don't use the values parameter inside the lambda to avoid an allocation */ + return stateToPropertiesCache.computeIfAbsent(state, s -> BlockModelShaper.statePropertiesToString(s.getValues())); + } +} diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index 63666d7b..34c4569b 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -65,6 +65,7 @@ "bugfix.concurrency.RenderTypeMixin", "bugfix.concurrency.MinecraftMixin", "bugfix.concurrency.StaticTagHelperMixin", + "perf.parallelize_model_loading.BlockModelShaperMixin", "perf.parallelize_model_loading.ModelBakeryMixin", "perf.parallelize_model_loading.OBJLoaderMixin", "perf.parallelize_model_loading.multipart.MultipartMixin",