From afba75b6cb57ebcffc2b6f1b724f77221fb65686 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Thu, 21 Feb 2013 00:40:28 -0600 Subject: [PATCH] Optimize getTileEntities Avoid traversing tile entities for every loaded chunk --- ...Optimize-getTileEntities-performance.patch | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 CraftBukkit-Patches/0029-Optimize-getTileEntities-performance.patch diff --git a/CraftBukkit-Patches/0029-Optimize-getTileEntities-performance.patch b/CraftBukkit-Patches/0029-Optimize-getTileEntities-performance.patch new file mode 100644 index 0000000000..fd957f671c --- /dev/null +++ b/CraftBukkit-Patches/0029-Optimize-getTileEntities-performance.patch @@ -0,0 +1,47 @@ +From 57cb706d68fe541313ce19b05256d6530fe2c0ec Mon Sep 17 00:00:00 2001 +From: Mike Primm +Date: Wed, 20 Feb 2013 23:07:53 -0500 +Subject: [PATCH] Optimize getTileEntities performance + +Avoid traversing tile entities for every loaded chunk +--- + .../java/net/minecraft/server/WorldServer.java | 21 +++++++++++--------- + 1 file changed, 12 insertions(+), 9 deletions(-) + +diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java +index 6cb3e24..8f0050a 100644 +--- a/src/main/java/net/minecraft/server/WorldServer.java ++++ b/src/main/java/net/minecraft/server/WorldServer.java +@@ -616,17 +616,20 @@ public class WorldServer extends World implements org.bukkit.BlockChangeDelegate + + public List getTileEntities(int i, int j, int k, int l, int i1, int j1) { + ArrayList arraylist = new ArrayList(); +- // CraftBukkit start - use iterator +- Iterator iterator = this.tileEntityList.iterator(); +- +- while (iterator.hasNext()) { +- TileEntity tileentity = (TileEntity) iterator.next(); +- // CraftBukkit end +- +- if (tileentity.x >= i && tileentity.y >= j && tileentity.z >= k && tileentity.x < l && tileentity.y < i1 && tileentity.z < j1) { +- arraylist.add(tileentity); ++ // Spigot start - check in chunks: usually just from one ++ for (int cx = (i >> 4); cx <= ((l - 1) >> 4); cx++) { ++ for (int cz = (k >> 4); cz <= ((j1 - 1) >> 4); cz++) { ++ Chunk c = getChunkAt(cx, cz); ++ if (c == null) continue; ++ for (Object te : c.tileEntities.values()) { ++ TileEntity tileentity = (TileEntity) te; ++ if ((tileentity.x >= i) && (tileentity.y >= j) && (tileentity.z >= k) && (tileentity.x < l) && (tileentity.y < i1) && (tileentity.z < j1)) { ++ arraylist.add(tileentity); ++ } ++ } + } + } ++ // Spigot end + + return arraylist; + } +-- +1.7.10.1 +