From 6d777ade166a5543a293c9353d7e6910b4a52b17 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Thu, 9 Aug 2012 01:10:12 -0500 Subject: [PATCH] [Bleeding] Optimize chunk loading ChunkSection.e() is called once per chunk section loaded and is quite expensive (about 20% of CPU time for loading the chunk). This changes the logic to add a fast path when extended block data is not being used and reorganizes the loops for more optimal array traversal. Overall this saves about 20-30% CPU time in this method. --- .../net/minecraft/server/ChunkSection.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/main/java/net/minecraft/server/ChunkSection.java b/src/main/java/net/minecraft/server/ChunkSection.java index 4d38de8a11..4891baf7d0 100644 --- a/src/main/java/net/minecraft/server/ChunkSection.java +++ b/src/main/java/net/minecraft/server/ChunkSection.java @@ -111,6 +111,63 @@ public class ChunkSection { } public void e() { + // CraftBukkit start - optimize for speed + byte[] dd = this.d; + int cntb = 0; + int cntc = 0; + if (this.e == null) { // No extended block IDs? Don't waste time messing with them + for (int off = 0; off < dd.length; off++) { + int l = dd[off] & 0xFF; + if (l > 0) { + if (Block.byId[l] == null) { + dd[off] = 0; + } else { + ++cntb; + if (Block.byId[l].r()) { + ++cntc; + } + } + } + } + } else { + byte[] ext = this.e.a; + for (int off = 0, off2 = 0; off < dd.length;) { + byte extid = ext[off2]; + int l = (dd[off] & 0xFF) | ((extid & 0xF) << 8); // Even data + if (l > 0) { + if (Block.byId[l] == null) { + dd[off] = 0; + ext[off2] &= 0xF0; + } else { + ++cntb; + if (Block.byId[l].r()) { + ++cntc; + } + } + } + off++; + l = (dd[off] & 0xFF) | ((extid & 0xF0) << 4); // Odd data + if (l > 0) { + if (Block.byId[l] == null) { + dd[off] = 0; + ext[off2] &= 0x0F; + } else { + ++cntb; + if (Block.byId[l].r()) { + ++cntc; + } + } + } + off++; + off2++; + } + } + this.b = cntb; + this.c = cntc; + } + + private void old_e() { + // CraftBukkit end this.b = 0; this.c = 0;