From 2c53c01cbc5c0df87c49a5d1ac81a2b115200eb8 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 15 Jul 2023 17:10:09 -0400 Subject: [PATCH 1/3] Improve patchlist formatting --- scripts/gen-markdown-patchlist.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/gen-markdown-patchlist.py b/scripts/gen-markdown-patchlist.py index bf95acb9..37ac1e0b 100644 --- a/scripts/gen-markdown-patchlist.py +++ b/scripts/gen-markdown-patchlist.py @@ -15,12 +15,11 @@ with open('doc/generated/' + branch_name + '-Summary-of-Patches.md', 'w') as out if key.startswith("modernfix.option.mixin."): option_names.append(key.replace("modernfix.option.", "")) option_names.sort() - print("# Summary of Patches - " + branch_name) print() for option in option_names: option_description = lang_obj.get("modernfix.option." + option) option_friendly_name = lang_obj.get("modernfix.option.name." + option) - print(f"# `{option}`") + print(f"### `{option}`") print() if option_description is not None: print(option_description) From c63a8fa21ecfa78bbc053bbcd37ee6dd2d36f913 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 15 Jul 2023 20:22:22 -0400 Subject: [PATCH 2/3] Deduplicate wall block shapes --- .../WallBlockMixin.java | 54 +++++++++++++++++++ .../core/config/ModernFixEarlyConfig.java | 2 + 2 files changed, 56 insertions(+) create mode 100644 common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java new file mode 100644 index 00000000..dda1ae77 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java @@ -0,0 +1,54 @@ +package org.embeddedt.modernfix.common.mixin.perf.deduplicate_wall_shapes; + +import com.google.common.collect.ImmutableMap; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.WallBlock; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.Property; +import net.minecraft.world.phys.shapes.VoxelShape; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@Mixin(WallBlock.class) +public abstract class WallBlockMixin extends Block { + private static Map, Comparable>, VoxelShape> CACHE_BY_PROPERTIES = new HashMap<>(); + private static StateDefinition CACHED_DEFINITION = null; + private static float[] CACHED_FLOATS = null; + + public WallBlockMixin(Properties properties) { + super(properties); + } + + @Inject(method = "makeShapes", at = @At("HEAD"), cancellable = true) + private synchronized void useCachedShapeMap(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable> cir) { + if(CACHED_DEFINITION != null) { + // check if this state container's properties exactly match the one we used for the cache + if(CACHED_DEFINITION.getProperties().equals(this.stateDefinition.getProperties()) && Arrays.equals(CACHED_FLOATS, new float[] { f1, f2, f3, f4, f5, f6 })) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + for(BlockState state : this.stateDefinition.getPossibleStates()) { + builder.put(state, CACHE_BY_PROPERTIES.get(state.getValues())); + } + cir.setReturnValue(builder.build()); + } + } + } + + @Inject(method = "makeShapes", at = @At("RETURN")) + private synchronized void storeCachedShapesByProperty(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable> cir) { + if(CACHE_BY_PROPERTIES.size() == 0) { + Map shapeMap = cir.getReturnValue(); + for(Map.Entry entry : shapeMap.entrySet()) { + CACHE_BY_PROPERTIES.put(entry.getKey().getValues(), entry.getValue()); + } + CACHED_FLOATS = new float[] { f1, f2, f3, f4, f5, f6 }; + CACHED_DEFINITION = this.stateDefinition; + } + } +} diff --git a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index e48486de..e3413561 100644 --- a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -226,6 +226,8 @@ public class ModernFixEarlyConfig { disableIfModPresent("mixin.bugfix.remove_block_chunkloading", "performant"); disableIfModPresent("mixin.bugfix.paper_chunk_patches", "c2me"); disableIfModPresent("mixin.perf.cache_strongholds", "littletiles"); + // content overlap + disableIfModPresent("mixin.perf.deduplicate_wall_shapes", "dashloader"); disableIfModPresent("mixin.perf.nbt_memory_usage", "c2me"); // DimThread makes changes to the server chunk manager (understandably), C2ME probably does the same disableIfModPresent("mixin.bugfix.chunk_deadlock", "c2me", "dimthread"); From ad948f0ec1d479f29ab57ff4c3b0cd5406bc47f7 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 15 Jul 2023 20:36:56 -0400 Subject: [PATCH 3/3] Rework wall block caching to cache collision shapes as well --- .../WallBlockMixin.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java index dda1ae77..169852fe 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/deduplicate_wall_shapes/WallBlockMixin.java @@ -1,6 +1,8 @@ package org.embeddedt.modernfix.common.mixin.perf.deduplicate_wall_shapes; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.mojang.datafixers.util.Pair; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.WallBlock; import net.minecraft.world.level.block.state.BlockState; @@ -12,15 +14,18 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; +/** + * Most wall blocks use the default set of vanilla properties, and the default sizes for their shapes. This means + * there is no need to reconstruct a separate VoxelShape instance for each wall, we can just repurpose the + * same shape instances. To do this we can cache a mapping between a state (represented only as its prop->value map) + * and the desired shape, and generate the BlockState->VoxelShape map from this for each block. + */ @Mixin(WallBlock.class) public abstract class WallBlockMixin extends Block { - private static Map, Comparable>, VoxelShape> CACHE_BY_PROPERTIES = new HashMap<>(); - private static StateDefinition CACHED_DEFINITION = null; - private static float[] CACHED_FLOATS = null; + private static Map, Pair, Comparable>, VoxelShape>, StateDefinition>> CACHE_BY_SHAPE_VALS = new HashMap<>(); public WallBlockMixin(Properties properties) { super(properties); @@ -28,27 +33,28 @@ public abstract class WallBlockMixin extends Block { @Inject(method = "makeShapes", at = @At("HEAD"), cancellable = true) private synchronized void useCachedShapeMap(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable> cir) { - if(CACHED_DEFINITION != null) { - // check if this state container's properties exactly match the one we used for the cache - if(CACHED_DEFINITION.getProperties().equals(this.stateDefinition.getProperties()) && Arrays.equals(CACHED_FLOATS, new float[] { f1, f2, f3, f4, f5, f6 })) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - for(BlockState state : this.stateDefinition.getPossibleStates()) { - builder.put(state, CACHE_BY_PROPERTIES.get(state.getValues())); - } - cir.setReturnValue(builder.build()); - } + ImmutableList key = ImmutableList.of(f1, f2, f3, f4, f5, f6); + Pair, Comparable>, VoxelShape>, StateDefinition> cache = CACHE_BY_SHAPE_VALS.get(key); + // require the properties to be identical + if(cache == null || !cache.getSecond().getProperties().equals(this.stateDefinition.getProperties())) + return; + ImmutableMap.Builder builder = ImmutableMap.builder(); + for(BlockState state : this.stateDefinition.getPossibleStates()) { + builder.put(state, cache.getFirst().get(state.getValues())); } + cir.setReturnValue(builder.build()); } @Inject(method = "makeShapes", at = @At("RETURN")) private synchronized void storeCachedShapesByProperty(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable> cir) { - if(CACHE_BY_PROPERTIES.size() == 0) { + ImmutableList key = ImmutableList.of(f1, f2, f3, f4, f5, f6); + if(!CACHE_BY_SHAPE_VALS.containsKey(key)) { + Map, Comparable>, VoxelShape> cacheByProperties = new HashMap<>(); Map shapeMap = cir.getReturnValue(); for(Map.Entry entry : shapeMap.entrySet()) { - CACHE_BY_PROPERTIES.put(entry.getKey().getValues(), entry.getValue()); + cacheByProperties.put(entry.getKey().getValues(), entry.getValue()); } - CACHED_FLOATS = new float[] { f1, f2, f3, f4, f5, f6 }; - CACHED_DEFINITION = this.stateDefinition; + CACHE_BY_SHAPE_VALS.put(key, Pair.of(cacheByProperties, this.stateDefinition)); } } }