papermc/patches/server/0254-Optimize-CraftBlockData-Creation.patch

50 lines
2.6 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00:00
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
2021-06-17 19:37:37 +00:00
index 505731735126e81a4cc768311dce337385e5503f..6a3b6611954e07760d586e2dc8c2015cfae7c0a5 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java
+++ b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java
2021-06-12 23:45:00 +00:00
@@ -637,6 +637,14 @@ public abstract class BlockBehaviour {
2021-06-11 12:02:28 +00:00
this.hasPostProcess = blockbase_info.hasPostProcess;
this.emissiveRendering = blockbase_info.emissiveRendering;
}
+ // 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() {
2021-06-17 19:37:37 +00:00
+ if (cachedCraftBlockData == null) cachedCraftBlockData = org.bukkit.craftbukkit.block.data.CraftBlockData.createData(asState());
2021-06-11 12:02:28 +00:00
+ return (org.bukkit.craftbukkit.block.data.CraftBlockData) cachedCraftBlockData.clone();
+ }
+ // Paper end
public void initCache() {
if (!this.getBlock().hasDynamicShape()) {
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
2021-06-12 23:45:00 +00:00
index c6a9ce2a67591205cbeb436b5043e737331c3527..3594f432a25b580173e8577bf324be954f5eddd1 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
2021-06-12 23:45:00 +00:00
@@ -527,7 +527,17 @@ public class CraftBlockData implements BlockData {
2021-06-11 12:02:28 +00:00
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
2021-06-12 23:45:00 +00:00
return CraftBlockData.MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
2021-06-11 12:02:28 +00:00
}