From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 3 Mar 2016 02:07:55 -0600
Subject: [PATCH] Optimize isValidLocation, getType and getBlockData for
 inlining

Hot methods, so reduce # of instructions for the method.

Move is valid location test to the BlockPosition class so that it can access local variables.

Replace all calls to the new place to the unnecessary forward.

Optimize getType and getBlockData to manually inline and optimize the calls

diff --git a/src/main/java/net/minecraft/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
@@ -0,0 +0,0 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
     private int b;
     private int e;
 
+    // Paper start
+    public boolean isValidLocation() {
+        return getX() >= -30000000 && getZ() >= -30000000 && getX() < 30000000 && getZ() < 30000000 && getY() >= 0 && getY() < 256;
+    }
+    public boolean isInvalidYLocation() {
+        return b < 0 || b >= 256;
+    }
+    // Paper end
+
     public BaseBlockPosition(int i, int j, int k) {
         this.a = i;
         this.b = j;
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -0,0 +0,0 @@ public class Chunk implements IChunkAccess {
         return this.sections;
     }
 
-    @Override
+    // Paper start - Optimize getBlockData to reduce instructions
+    public final IBlockData getBlockData(BlockPosition pos) { return getBlockData(pos.getX(), pos.getY(), pos.getZ()); } // Paper
     public IBlockData getType(BlockPosition blockposition) {
-        int i = blockposition.getX();
-        int j = blockposition.getY();
-        int k = blockposition.getZ();
+        return this.getBlockData(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+    }
+
+    public IBlockData getType(final int x, final int y, final int z) {
+        return getBlockData(x, y, z);
+    }
+    public final IBlockData getBlockData(final int x, final int y, final int z) {
+        // Method body / logic copied from below
+        final int i = y >> 4;
+        if (y < 0 || i >= this.sections.length || this.sections[i] == null || this.sections[i].nonEmptyBlockCount == 0) {
+            return Blocks.AIR.getBlockData();
+        }
+        // Inlined ChunkSection.getType() and DataPaletteBlock.a(int,int,int)
+        return this.sections[i].blockIds.a((y & 15) << 8 | (z & 15) << 4 | x & 15);
+    }
 
+    public IBlockData getBlockData_unused(int i, int j, int k) {
+        // Paper end
         if (this.world.isDebugWorld()) {
             IBlockData iblockdata = null;
 
diff --git a/src/main/java/net/minecraft/server/ChunkEmpty.java b/src/main/java/net/minecraft/server/ChunkEmpty.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ChunkEmpty.java
+++ b/src/main/java/net/minecraft/server/ChunkEmpty.java
@@ -0,0 +0,0 @@ import javax.annotation.Nullable;
 
 public class ChunkEmpty extends Chunk {
 
-    private static final BiomeBase[] b = (BiomeBase[]) SystemUtils.a((Object) (new BiomeBase[BiomeStorage.a]), (abiomebase) -> {
+    private static final BiomeBase[] b = (BiomeBase[]) SystemUtils.a((new BiomeBase[BiomeStorage.a]), (abiomebase) -> { // Paper - decompile error
         Arrays.fill(abiomebase, Biomes.PLAINS);
     });
 
@@ -0,0 +0,0 @@ public class ChunkEmpty extends Chunk {
         super(world, chunkcoordintpair, new BiomeStorage(ChunkEmpty.b));
     }
 
+    // Paper start
+    @Override public IBlockData getType(int x, int y, int z) {
+        return Blocks.VOID_AIR.getBlockData();
+    }
+    // Paper end
     @Override
     public IBlockData getType(BlockPosition blockposition) {
         return Blocks.VOID_AIR.getBlockData();
diff --git a/src/main/java/net/minecraft/server/ChunkSection.java b/src/main/java/net/minecraft/server/ChunkSection.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ChunkSection.java
+++ b/src/main/java/net/minecraft/server/ChunkSection.java
@@ -0,0 +0,0 @@ public class ChunkSection {
 
     public static final DataPalette<IBlockData> GLOBAL_PALETTE = new DataPaletteGlobal<>(Block.REGISTRY_ID, Blocks.AIR.getBlockData());
     private final int yPos;
-    private short nonEmptyBlockCount;
+    short nonEmptyBlockCount; // Paper - package-private
     private short tickingBlockCount;
     private short e;
-    private final DataPaletteBlock<IBlockData> blockIds;
+    final DataPaletteBlock<IBlockData> blockIds;
 
     public ChunkSection(int i) {
         this(i, (short) 0, (short) 0, (short) 0);
@@ -0,0 +0,0 @@ public class ChunkSection {
         this.blockIds = new DataPaletteBlock<>(ChunkSection.GLOBAL_PALETTE, Block.REGISTRY_ID, GameProfileSerializer::c, GameProfileSerializer::a, Blocks.AIR.getBlockData());
     }
 
-    public IBlockData getType(int i, int j, int k) {
-        return (IBlockData) this.blockIds.a(i, j, k);
+    public final IBlockData getType(int i, int j, int k) { // Paper
+        return this.blockIds.a(j << 8 | k << 4 | i); // Paper - inline
     }
 
     public Fluid b(int i, int j, int k) {
diff --git a/src/main/java/net/minecraft/server/DataPaletteBlock.java b/src/main/java/net/minecraft/server/DataPaletteBlock.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/DataPaletteBlock.java
+++ b/src/main/java/net/minecraft/server/DataPaletteBlock.java
@@ -0,0 +0,0 @@ public class DataPaletteBlock<T> implements DataPaletteExpandable<T> {
     }
 
     public T a(int i, int j, int k) {
-        return this.a(b(i, j, k));
+        return this.a(j << 8 | k << 4 | i); // Paper - inline
     }
 
     protected T a(int i) {
diff --git a/src/main/java/net/minecraft/server/IChunkAccess.java b/src/main/java/net/minecraft/server/IChunkAccess.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/IChunkAccess.java
+++ b/src/main/java/net/minecraft/server/IChunkAccess.java
@@ -0,0 +0,0 @@ import org.apache.logging.log4j.LogManager;
 
 public interface IChunkAccess extends IBlockAccess, IStructureAccess {
 
+    IBlockData getType(final int x, final int y, final int z); // Paper
     @Nullable
     IBlockData setType(BlockPosition blockposition, IBlockData iblockdata, boolean flag);
 
diff --git a/src/main/java/net/minecraft/server/ProtoChunk.java b/src/main/java/net/minecraft/server/ProtoChunk.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ProtoChunk.java
+++ b/src/main/java/net/minecraft/server/ProtoChunk.java
@@ -0,0 +0,0 @@ public class ProtoChunk implements IChunkAccess {
 
     @Override
     public IBlockData getType(BlockPosition blockposition) {
-        int i = blockposition.getY();
-
-        if (World.b(i)) {
+        return getType(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+    }
+    // Paper start
+    public IBlockData getType(final int x, final int y, final int z) {
+        if (y < 0 || y >= 256) {
             return Blocks.VOID_AIR.getBlockData();
         } else {
-            ChunkSection chunksection = this.getSections()[i >> 4];
-
-            return ChunkSection.a(chunksection) ? Blocks.AIR.getBlockData() : chunksection.getType(blockposition.getX() & 15, i & 15, blockposition.getZ() & 15);
+            ChunkSection chunksection = this.getSections()[y >> 4];
+            return chunksection == Chunk.EMPTY_CHUNK_SECTION || chunksection.c() ? Blocks.AIR.getBlockData() : chunksection.getType(x & 15, y & 15, z & 15);
         }
     }
+    // Paper end
 
     @Override
     public Fluid getFluid(BlockPosition blockposition) {
diff --git a/src/main/java/net/minecraft/server/ProtoChunkExtension.java b/src/main/java/net/minecraft/server/ProtoChunkExtension.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ProtoChunkExtension.java
+++ b/src/main/java/net/minecraft/server/ProtoChunkExtension.java
@@ -0,0 +0,0 @@ public class ProtoChunkExtension extends ProtoChunk {
     public IBlockData getType(BlockPosition blockposition) {
         return this.a.getType(blockposition);
     }
+    // Paper start
+    public final IBlockData getType(final int x, final int y, final int z) {
+        return this.a.getBlockData(x, y, z);
+    }
+    // Paper end
 
     @Override
     public Fluid getFluid(BlockPosition blockposition) {
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -0,0 +0,0 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
     }
 
     public static boolean isValidLocation(BlockPosition blockposition) {
-        return !isOutsideWorld(blockposition) && e(blockposition);
+        return blockposition.isValidLocation();
     }
 
     public static boolean k(BlockPosition blockposition) {
@@ -0,0 +0,0 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
     }
 
     public static boolean isOutsideWorld(BlockPosition blockposition) {
-        return b(blockposition.getY());
+        return blockposition.isInvalidYLocation();
     }
 
     public static boolean b(int i) {
