Deduplicate wall block shapes

This commit is contained in:
embeddedt 2023-07-15 20:22:22 -04:00
parent 2c53c01cbc
commit c63a8fa21e
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 56 additions and 0 deletions

View File

@ -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<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape> CACHE_BY_PROPERTIES = new HashMap<>();
private static StateDefinition<Block, BlockState> 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<Map<BlockState, VoxelShape>> 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<BlockState, VoxelShape> 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<Map<BlockState, VoxelShape>> cir) {
if(CACHE_BY_PROPERTIES.size() == 0) {
Map<BlockState, VoxelShape> shapeMap = cir.getReturnValue();
for(Map.Entry<BlockState, VoxelShape> 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;
}
}
}

View File

@ -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");