Paper/patches/server/0244-Optimize-CraftBlockData-Creation.patch
Jake Potrebic a27262375c
some work
2023-09-21 15:01:00 -07:00

50 lines
2.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: miclebrick <miclebrick@outlook.com>
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 6c171199dcc30f56a6d0ab7ecf398b505d145067..916d7b35145345cb3bf5ca46df38eae0512c4e95 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
@@ -809,6 +809,14 @@ public abstract class BlockBehaviour implements FeatureElement {
this.replaceable = blockbase_info.replaceable;
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
private boolean calculateSolid() {
if (((Block) this.owner).properties.forceSolidOn) {
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 745d2c589df8c59db327d7482024096473318812..c80aaa37b7d63acfb283641a4804dc2a32156eda 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -560,7 +560,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);
}