From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: miclebrick Date: Thu, 23 Aug 2018 11:45:32 -0400 Subject: [PATCH] Optimize CraftBlockData Creation Avoids a hashmap lookup by cacheing a reference to the CraftBlockData and cloning it when one is needed. diff --git a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java index 0eb877c80c6492d03681c70f7866ad705a62454e..7cc6dcb128f2792fc32bb8f50e4c94d600255207 100644 --- a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java +++ b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java @@ -753,6 +753,14 @@ public abstract class BlockBehaviour implements FeatureElement { this.spawnParticlesOnBreak = blockbase_info.spawnParticlesOnBreak; this.conditionallyFullOpaque = this.isOpaque() & this.isTransparentOnSomeFaces(); // Paper } + // Paper start - 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 // Paper start protected boolean shapeExceedsCube = true; diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java index b47fdd7d54ca0fafb8b56b64ae1ab96b6fcf0818..d4a204cdd035d1af2fc5a05036a3374ac89974c5 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java +++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java @@ -554,7 +554,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(BlockState::createCraftBlockData); + } public static CraftBlockData fromData(BlockState data) { + return data.createCraftBlockData(); + } + + public static CraftBlockData createData(BlockState data) { + // Paper end return CraftBlockData.MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data); }