From 33e453fcfdc4b9e2cb2541d7fa2eb9040faf6e3a Mon Sep 17 00:00:00 2001
From: Techcable <Techcable@outlook.com>
Date: Wed, 30 Nov 2016 20:57:02 -0600
Subject: [PATCH] Speedup BlockPos by fixing inlining

---
 ...Location-getType-and-getBlockData-fo.patch |   4 +-
 .../0150-Make-entities-look-for-hoppers.patch |   6 +-
 ...-Speedup-BlockPos-by-fixing-inlining.patch | 162 ++++++++++++++++++
 3 files changed, 167 insertions(+), 5 deletions(-)
 create mode 100644 Spigot-Server-Patches/0184-Speedup-BlockPos-by-fixing-inlining.patch

diff --git a/Spigot-Server-Patches/0092-Optimize-isValidLocation-getType-and-getBlockData-fo.patch b/Spigot-Server-Patches/0092-Optimize-isValidLocation-getType-and-getBlockData-fo.patch
index b73b1ec40d..d998c6a44d 100644
--- a/Spigot-Server-Patches/0092-Optimize-isValidLocation-getType-and-getBlockData-fo.patch
+++ b/Spigot-Server-Patches/0092-Optimize-isValidLocation-getType-and-getBlockData-fo.patch
@@ -1,4 +1,4 @@
-From 0c1d747df007846e209ed5f0531dd51d59fad7fc Mon Sep 17 00:00:00 2001
+From 9b31029333fb85ecd0627b4c69982c9b562987ec 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 inling
@@ -98,7 +98,7 @@ index 3d784d0..afdc4a7 100644
      private NibbleArray skyLight;
  
 diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
-index 223250f..14ac6d2 100644
+index ecd2d26..224e4c9 100644
 --- a/src/main/java/net/minecraft/server/World.java
 +++ b/src/main/java/net/minecraft/server/World.java
 @@ -273,12 +273,12 @@ public abstract class World implements IBlockAccess {
diff --git a/Spigot-Server-Patches/0150-Make-entities-look-for-hoppers.patch b/Spigot-Server-Patches/0150-Make-entities-look-for-hoppers.patch
index caba9a3506..9b08bba10f 100644
--- a/Spigot-Server-Patches/0150-Make-entities-look-for-hoppers.patch
+++ b/Spigot-Server-Patches/0150-Make-entities-look-for-hoppers.patch
@@ -1,4 +1,4 @@
-From 0d78e997b13f2330eb8721d24bde7a39dd0182a9 Mon Sep 17 00:00:00 2001
+From 56aca6078929415387fb88359401d9259e3180de Mon Sep 17 00:00:00 2001
 From: Techcable <Techcable@outlook.com>
 Date: Sat, 18 Jun 2016 01:01:37 -0500
 Subject: [PATCH] Make entities look for hoppers
@@ -261,7 +261,7 @@ index 804215a..e830d83 100644
 +    double G(); default double getZ() { return G(); } // Paper - OBFHELPER
  }
 diff --git a/src/main/java/net/minecraft/server/TileEntityHopper.java b/src/main/java/net/minecraft/server/TileEntityHopper.java
-index 4ebcf47..41cc568 100644
+index 7154776..f8339ad 100644
 --- a/src/main/java/net/minecraft/server/TileEntityHopper.java
 +++ b/src/main/java/net/minecraft/server/TileEntityHopper.java
 @@ -122,6 +122,7 @@ public class TileEntityHopper extends TileEntityLootable implements IHopper, ITi
@@ -390,5 +390,5 @@ index 4ebcf47..41cc568 100644
  
              if (!list.isEmpty()) {
 -- 
-2.10.2.windows.1
+2.10.2
 
diff --git a/Spigot-Server-Patches/0184-Speedup-BlockPos-by-fixing-inlining.patch b/Spigot-Server-Patches/0184-Speedup-BlockPos-by-fixing-inlining.patch
new file mode 100644
index 0000000000..40dc71fd2d
--- /dev/null
+++ b/Spigot-Server-Patches/0184-Speedup-BlockPos-by-fixing-inlining.patch
@@ -0,0 +1,162 @@
+From 3e498c8197d48f4f8cdb709c7f7e980a54ae9af1 Mon Sep 17 00:00:00 2001
+From: Techcable <Techcable@outlook.com>
+Date: Wed, 30 Nov 2016 20:56:58 -0600
+Subject: [PATCH] Speedup BlockPos by fixing inlining
+
+Normally the JVM can inline virtual getters by having two sets of code, one is the 'optimized' code and the other is the 'deoptimized' code.
+If a single type is used 99% of the time, then its worth it to inline, and to revert to 'deoptimized' the 1% of the time we encounter other types.
+But if two types are encountered commonly, then the JVM can't inline them both, and the call overhead remains.
+
+This scenario also occurs with BlockPos and MutableBlockPos.
+The variables in BlockPos are final, so MutableBlockPos can't modify them.
+MutableBlockPos fixes this by adding custom mutable variables, and overriding the getters to access them.
+
+This approach with utility methods that operate on MutableBlockPos and BlockPos.
+Specific examples are BlockPosition.up(), and World.isValidLocation().
+It makes these simple methods much slower than they need to be.
+
+This should result in an across the board speedup in anything that accesses blocks or does logic with positions.
+
+This is based upon conclusions drawn from inspecting the assenmbly generated bythe JIT compiler on my mircorbenchmarks.
+They had 'callq' (invoke) instead of 'mov' (get from memory) instructions.
+
+diff --git a/src/main/java/net/minecraft/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
+index f772c7c..f5dc144 100644
+--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
++++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
+@@ -7,11 +7,12 @@ import javax.annotation.concurrent.Immutable;
+ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
+ 
+     public static final BaseBlockPosition ZERO = new BaseBlockPosition(0, 0, 0);
+-    private final int a;
+-    private final int b;
+-    private final int c;
+-    // Paper start
+-    public boolean isValidLocation() {
++    // Paper start - Make mutable and protected for MutableBlockPos and PooledBlockPos
++    protected int a;
++    protected int b;
++    protected int c;
++
++    public final boolean isValidLocation() {
+         return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
+     }
+     public boolean isInvalidYLocation() {
+@@ -49,17 +50,19 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
+         return this.getY() == baseblockposition.getY() ? (this.getZ() == baseblockposition.getZ() ? this.getX() - baseblockposition.getX() : this.getZ() - baseblockposition.getZ()) : this.getY() - baseblockposition.getY();
+     }
+ 
+-    public int getX() {
++    // Paper start - Only allow a single implementation
++    public final int getX() {
+         return this.a;
+     }
+ 
+-    public int getY() {
++    public final int getY() {
+         return this.b;
+     }
+ 
+-    public int getZ() {
++    public final int getZ() {
+         return this.c;
+     }
++    // Paper end
+ 
+     public BaseBlockPosition d(BaseBlockPosition baseblockposition) {
+         return new BaseBlockPosition(this.getY() * baseblockposition.getZ() - this.getZ() * baseblockposition.getY(), this.getZ() * baseblockposition.getX() - this.getX() * baseblockposition.getZ(), this.getX() * baseblockposition.getY() - this.getY() * baseblockposition.getX());
+diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
+index 6c14061..c258331 100644
+--- a/src/main/java/net/minecraft/server/BlockPosition.java
++++ b/src/main/java/net/minecraft/server/BlockPosition.java
+@@ -254,9 +254,9 @@ public class BlockPosition extends BaseBlockPosition {
+                             }
+                             // Paper end
+ 
+-                            this.b.b = i;
+-                            this.b.c = j;
+-                            this.b.d = k;
++                            ((BaseBlockPosition) this.b).a = i;
++                            ((BaseBlockPosition) this.b).b = j;
++                            ((BaseBlockPosition) this.b).c = k;
+                             return this.b;
+                         }
+                     }
+@@ -370,6 +370,8 @@ public class BlockPosition extends BaseBlockPosition {
+ 
+     public static class MutableBlockPosition extends BlockPosition {
+ 
++        // Paper start - Remove variables
++        /*
+         protected int b;
+         protected int c;
+         protected int d;
+@@ -382,6 +384,7 @@ public class BlockPosition extends BaseBlockPosition {
+         public boolean isInvalidYLocation() {
+             return c < 0 || c >= 256;
+         }
++        */
+         // Paper end
+ 
+         public MutableBlockPosition() {
+@@ -394,9 +397,11 @@ public class BlockPosition extends BaseBlockPosition {
+ 
+         public MutableBlockPosition(int i, int j, int k) {
+             super(0, 0, 0);
+-            this.b = i;
+-            this.c = j;
+-            this.d = k;
++            // Paper start - Modify base position variables
++            ((BaseBlockPosition) this).a = i;
++            ((BaseBlockPosition) this).b = j;
++            ((BaseBlockPosition) this).c = k;
++            // Paper end
+         }
+ 
+         public BlockPosition a(double d0, double d1, double d2) {
+@@ -415,6 +420,8 @@ public class BlockPosition extends BaseBlockPosition {
+             return super.a(enumblockrotation).h();
+         }
+ 
++        // Paper start - Use superclass methods
++        /*
+         public int getX() {
+             return this.b;
+         }
+@@ -426,12 +433,16 @@ public class BlockPosition extends BaseBlockPosition {
+         public int getZ() {
+             return this.d;
+         }
++        */
++        // Paper end
+ 
+         public void setValues(int x, int y, int z) { c(x, y, z); } // Paper - OBFHELPER
+         public BlockPosition.MutableBlockPosition c(int i, int j, int k) {
+-            this.b = i;
+-            this.c = j;
+-            this.d = k;
++            // Paper start - Modify base position variables
++            ((BaseBlockPosition) this).a = i;
++            ((BaseBlockPosition) this).b = j;
++            ((BaseBlockPosition) this).c = k;
++            // Paper end
+             return this;
+         }
+ 
+@@ -448,11 +459,11 @@ public class BlockPosition extends BaseBlockPosition {
+         }
+ 
+         public BlockPosition.MutableBlockPosition c(EnumDirection enumdirection, int i) {
+-            return this.c(this.b + enumdirection.getAdjacentX() * i, this.c + enumdirection.getAdjacentY() * i, this.d + enumdirection.getAdjacentZ() * i);
++            return this.c(this.getX() + enumdirection.getAdjacentX() * i, this.getY() + enumdirection.getAdjacentY() * i, this.getZ() + enumdirection.getAdjacentZ() * i); // Paper - USE THE BLEEPING GETTERS
+         }
+ 
+         public void p(int i) {
+-            this.c = i;
++            ((BaseBlockPosition) this).b = i; // Paper - Modify base variable
+         }
+ 
+         public BlockPosition h() {
+-- 
+2.10.2
+