Optimize CraftBlockData Creation

Avoids a hashmap lookup by cacheing a reference to the CraftBlockData
and cloning it when one is needed.
This commit is contained in:
miclebrick 2018-08-23 11:45:32 -04:00
parent 28ea7bb560
commit 68e76b43b8
2 changed files with 29 additions and 3 deletions

View file

@ -50,7 +50,23 @@
}
state.spawnAfterBreak(world, pos, ItemStack.EMPTY, flag);
@@ -873,12 +887,14 @@
@@ -851,7 +865,15 @@
this.spawnTerrainParticles = blockbase_info.spawnTerrainParticles;
this.instrument = blockbase_info.instrument;
this.replaceable = blockbase_info.replaceable;
+ }
+ // Paper start - Perf: impl cached craft block data, lazy load to fix issue with loading at the wrong time
+ private org.bukkit.craftbukkit.block.data.CraftBlockData cachedCraftBlockData;
+
+ public org.bukkit.craftbukkit.block.data.CraftBlockData createCraftBlockData() {
+ if (cachedCraftBlockData == null) cachedCraftBlockData = org.bukkit.craftbukkit.block.data.CraftBlockData.createData(asState());
+ return (org.bukkit.craftbukkit.block.data.CraftBlockData) cachedCraftBlockData.clone();
}
+ // Paper end - Perf: impl cached craft block data, lazy load to fix issue with loading at the wrong time
private boolean calculateSolid() {
if (((Block) this.owner).properties.forceSolidOn) {
@@ -873,12 +895,14 @@
}
}
@ -65,7 +81,7 @@
this.legacySolid = this.calculateSolid();
this.occlusionShape = this.canOcclude ? ((Block) this.owner).getOcclusionShape(this.asState()) : Shapes.empty();
@@ -945,8 +961,8 @@
@@ -945,8 +969,8 @@
return this.occlusionShape;
}
@ -76,7 +92,7 @@
}
public boolean useShapeForLightOcclusion() {
@@ -1125,9 +1141,15 @@
@@ -1125,9 +1149,15 @@
}
public void onPlace(Level world, BlockPos pos, BlockState state, boolean notify) {

View file

@ -579,7 +579,17 @@ public class CraftBlockData implements BlockData {
return craft;
}
// Paper start - optimize creating BlockData to not need a map lookup
static {
// Initialize cached data for all IBlockData instances after registration
Block.BLOCK_STATE_REGISTRY.iterator().forEachRemaining(net.minecraft.world.level.block.state.BlockState::createCraftBlockData);
}
public static CraftBlockData fromData(net.minecraft.world.level.block.state.BlockState data) {
return data.createCraftBlockData();
}
public static CraftBlockData createData(net.minecraft.world.level.block.state.BlockState data) {
// Paper end
return CraftBlockData.MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
}