aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0332-Optimize-CraftBlockData-Creation.patch
blob: 23e1fe5641f0f1283a0970ca911a6be549657f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
From 7f9d6eaeea4c3a7fd2d75d413016e01b7b2d30d8 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/BlockData.java b/src/main/java/net/minecraft/server/BlockData.java
index 4dd6c32761..1b226a77e0 100644
--- a/src/main/java/net/minecraft/server/BlockData.java
+++ b/src/main/java/net/minecraft/server/BlockData.java
@@ -1,6 +1,7 @@
 package net.minecraft.server;
 
 import com.google.common.collect.ImmutableMap;
+import org.bukkit.craftbukkit.block.data.CraftBlockData;
 
 public class BlockData extends BlockDataAbstract<Block, IBlockData> implements IBlockData {
 
@@ -11,4 +12,14 @@ public class BlockData extends BlockDataAbstract<Block, IBlockData> implements I
     public Block getBlock() {
         return (Block) this.e_;
     }
+
+    // Paper start - impl cached craft block data, lazy load to fix issue with loading at the wrong time
+    private CraftBlockData cachedCraftBlockData;
+
+    @Override
+    public CraftBlockData createCraftBlockData() {
+        if(cachedCraftBlockData == null) cachedCraftBlockData = CraftBlockData.createData(this);
+        return (CraftBlockData) cachedCraftBlockData.clone();
+    }
+    // Paper end
 }
diff --git a/src/main/java/net/minecraft/server/IBlockData.java b/src/main/java/net/minecraft/server/IBlockData.java
index e4e4c55130..08a5acb0ac 100644
--- a/src/main/java/net/minecraft/server/IBlockData.java
+++ b/src/main/java/net/minecraft/server/IBlockData.java
@@ -27,6 +27,8 @@ public interface IBlockData extends IBlockDataHolder<IBlockData> {
 
     Block getBlock();
 
+    org.bukkit.craftbukkit.block.data.CraftBlockData createCraftBlockData(); // Paper - property for converting IBlockData to CraftBlockData, should only be used by CraftBlockData#fromData and should return a clone
+
     default Material getMaterial() {
         return this.getBlock().n(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
index 7755e4f7a6..4bb43e360d 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -521,7 +521,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.21.0