Skip to content

Commit c63a8fa

Browse files
committed
Deduplicate wall block shapes
1 parent 2c53c01 commit c63a8fa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.deduplicate_wall_shapes;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import net.minecraft.world.level.block.Block;
5+
import net.minecraft.world.level.block.WallBlock;
6+
import net.minecraft.world.level.block.state.BlockState;
7+
import net.minecraft.world.level.block.state.StateDefinition;
8+
import net.minecraft.world.level.block.state.properties.Property;
9+
import net.minecraft.world.phys.shapes.VoxelShape;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Inject;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
14+
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
@Mixin(WallBlock.class)
20+
public abstract class WallBlockMixin extends Block {
21+
private static Map<ImmutableMap<Property<?>, Comparable<?>>, VoxelShape> CACHE_BY_PROPERTIES = new HashMap<>();
22+
private static StateDefinition<Block, BlockState> CACHED_DEFINITION = null;
23+
private static float[] CACHED_FLOATS = null;
24+
25+
public WallBlockMixin(Properties properties) {
26+
super(properties);
27+
}
28+
29+
@Inject(method = "makeShapes", at = @At("HEAD"), cancellable = true)
30+
private synchronized void useCachedShapeMap(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) {
31+
if(CACHED_DEFINITION != null) {
32+
// check if this state container's properties exactly match the one we used for the cache
33+
if(CACHED_DEFINITION.getProperties().equals(this.stateDefinition.getProperties()) && Arrays.equals(CACHED_FLOATS, new float[] { f1, f2, f3, f4, f5, f6 })) {
34+
ImmutableMap.Builder<BlockState, VoxelShape> builder = ImmutableMap.builder();
35+
for(BlockState state : this.stateDefinition.getPossibleStates()) {
36+
builder.put(state, CACHE_BY_PROPERTIES.get(state.getValues()));
37+
}
38+
cir.setReturnValue(builder.build());
39+
}
40+
}
41+
}
42+
43+
@Inject(method = "makeShapes", at = @At("RETURN"))
44+
private synchronized void storeCachedShapesByProperty(float f1, float f2, float f3, float f4, float f5, float f6, CallbackInfoReturnable<Map<BlockState, VoxelShape>> cir) {
45+
if(CACHE_BY_PROPERTIES.size() == 0) {
46+
Map<BlockState, VoxelShape> shapeMap = cir.getReturnValue();
47+
for(Map.Entry<BlockState, VoxelShape> entry : shapeMap.entrySet()) {
48+
CACHE_BY_PROPERTIES.put(entry.getKey().getValues(), entry.getValue());
49+
}
50+
CACHED_FLOATS = new float[] { f1, f2, f3, f4, f5, f6 };
51+
CACHED_DEFINITION = this.stateDefinition;
52+
}
53+
}
54+
}

common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ private ModernFixEarlyConfig(File file) {
226226
disableIfModPresent("mixin.bugfix.remove_block_chunkloading", "performant");
227227
disableIfModPresent("mixin.bugfix.paper_chunk_patches", "c2me");
228228
disableIfModPresent("mixin.perf.cache_strongholds", "littletiles");
229+
// content overlap
230+
disableIfModPresent("mixin.perf.deduplicate_wall_shapes", "dashloader");
229231
disableIfModPresent("mixin.perf.nbt_memory_usage", "c2me");
230232
// DimThread makes changes to the server chunk manager (understandably), C2ME probably does the same
231233
disableIfModPresent("mixin.bugfix.chunk_deadlock", "c2me", "dimthread");

0 commit comments

Comments
 (0)