Paper/Spigot-Server-Patches/0289-Optimize-CraftBlockData-Creation.patch

63 lines
2.5 KiB
Diff
Raw Normal View History

2019-07-20 06:01:24 +02:00
From 0dc7fe61b11f27da089ec80669932ea398a42d97 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/server/IBlockData.java b/src/main/java/net/minecraft/server/IBlockData.java
2019-07-20 06:01:24 +02:00
index 367b02800a..9fd1b4915f 100644
--- a/src/main/java/net/minecraft/server/IBlockData.java
+++ b/src/main/java/net/minecraft/server/IBlockData.java
2019-05-05 10:33:44 +02:00
@@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.DynamicOps;
import com.mojang.datafixers.util.Pair;
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
+
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
2019-05-06 01:24:37 +02:00
@@ -38,6 +40,15 @@ public class IBlockData extends BlockDataAbstract<Block, IBlockData> implements
2019-05-05 10:33:44 +02:00
return (Block) this.a;
}
2019-05-05 10:33:44 +02:00
+ // Paper start - impl cached craft block data, lazy load to fix issue with loading at the wrong time
+ private CraftBlockData cachedCraftBlockData;
+
+ public CraftBlockData createCraftBlockData() {
+ if(cachedCraftBlockData == null) cachedCraftBlockData = CraftBlockData.createData(this);
+ return (CraftBlockData) cachedCraftBlockData.clone();
+ }
+ // Paper end
+
2019-05-05 10:33:44 +02:00
public Material getMaterial() {
return this.getBlock().l(this);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
2019-07-20 06:01:24 +02:00
index 99c361dc69..839e645b69 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -548,7 +548,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.REGISTRY_ID.iterator().forEachRemaining(IBlockData::createCraftBlockData);
+ }
public static CraftBlockData fromData(IBlockData data) {
+ return data.createCraftBlockData();
+ }
+
+ public static CraftBlockData createData(IBlockData data) {
+ // Paper end
return MAP.getOrDefault(data.getBlock().getClass(), CraftBlockData::new).apply(data);
}
}
--
2.22.0