Paper/Spigot-Server-Patches/0077-Speedup-BlockPos-by-fixing-inlining.patch
Aikar 9ff01b16ab Add MCUtils helper
This will be used by my next commit. But trying to get the build going
since CI blew up
2016-03-28 21:01:42 -04:00

224 lines
8.8 KiB
Diff

From 88cefc5d4dc82eeb95719ceee5d40341d2434b69 Mon Sep 17 00:00:00 2001
From: Techcable <Techcable@outlook.com>
Date: Mon, 7 Mar 2016 12:51:01 -0700
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 fa95e8f..f0908a2 100644
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
@@ -5,9 +5,11 @@ import com.google.common.base.Objects;
public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
public static final BaseBlockPosition ZERO = new BaseBlockPosition(0, 0, 0);
- private final int a;
- private final int c;
- private final int d;
+ // Paper start - make mutable and protected for MutableBlockPos and PooledBlockPos
+ protected int a;
+ protected int c;
+ protected int d;
+ // Paper end
public BaseBlockPosition(int i, int j, int k) {
this.a = i;
@@ -39,17 +41,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 one implementation of these methods (make final)
+ public final int getX() {
return this.a;
}
- public int getY() {
+ public final int getY() {
return this.c;
}
- public int getZ() {
+ public final int getZ() {
return this.d;
}
+ // 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 e7a95f3..2d56f02 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -207,9 +207,10 @@ public class BlockPosition extends BaseBlockPosition {
++k;
}
- this.b.c = i;
- this.b.d = j;
- this.b.e = k;
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this.b).a = i;
+ ((BaseBlockPosition) this.b).c = j;
+ ((BaseBlockPosition) this.b).d = k;
return this.b;
}
}
@@ -228,17 +229,23 @@ public class BlockPosition extends BaseBlockPosition {
public static final class PooledBlockPosition extends BlockPosition {
+ // Paper start - remove variables
+ /*
private int c;
private int d;
private int e;
+ */
+ // Paper end
private boolean f;
private static final List<BlockPosition.PooledBlockPosition> g = Lists.newArrayList();
private PooledBlockPosition(int i, int j, int k) {
super(0, 0, 0);
- this.c = i;
- this.d = j;
- this.e = k;
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).c = j;
+ ((BaseBlockPosition) this).d = k;
+ // Paper end
}
public static BlockPosition.PooledBlockPosition s() {
@@ -279,6 +286,8 @@ public class BlockPosition extends BaseBlockPosition {
}
}
+ // Paper start - use superclass methods
+ /*
public int getX() {
return this.c;
}
@@ -290,6 +299,8 @@ public class BlockPosition extends BaseBlockPosition {
public int getZ() {
return this.e;
}
+ */
+ // Paper end
public BlockPosition.PooledBlockPosition d(int i, int j, int k) {
if (this.f) {
@@ -297,9 +308,11 @@ public class BlockPosition extends BaseBlockPosition {
this.f = false;
}
- this.c = i;
- this.d = j;
- this.e = k;
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).c = j;
+ ((BaseBlockPosition) this).d = k;
+ // Paper end
return this;
}
@@ -312,7 +325,7 @@ public class BlockPosition extends BaseBlockPosition {
}
public BlockPosition.PooledBlockPosition c(EnumDirection enumdirection) {
- return this.d(this.c + enumdirection.getAdjacentX(), this.d + enumdirection.getAdjacentY(), this.e + enumdirection.getAdjacentZ());
+ return this.d(this.getX() + enumdirection.getAdjacentX(), this.getY() + enumdirection.getAdjacentY(), this.getZ() + enumdirection.getAdjacentZ()); // Paper - use getters
}
public BaseBlockPosition d(BaseBlockPosition baseblockposition) {
@@ -322,9 +335,13 @@ public class BlockPosition extends BaseBlockPosition {
public static final class MutableBlockPosition extends BlockPosition {
+ // Paper start - remove variables
+ /*
private int c;
private int d;
private int e;
+ */
+ // Paper end
public MutableBlockPosition() {
this(0, 0, 0);
@@ -336,11 +353,15 @@ public class BlockPosition extends BaseBlockPosition {
public MutableBlockPosition(int i, int j, int k) {
super(0, 0, 0);
- this.c = i;
- this.d = j;
- this.e = k;
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).c = j;
+ ((BaseBlockPosition) this).d = k;
+ // Paper end
}
+ // Paper start - use superclass methods
+ /*
public int getX() {
return this.c;
}
@@ -352,22 +373,28 @@ public class BlockPosition extends BaseBlockPosition {
public int getZ() {
return this.e;
}
+ */
+ // Paper end
public BlockPosition.MutableBlockPosition c(int i, int j, int k) {
- this.c = i;
- this.d = j;
- this.e = k;
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).c = j;
+ ((BaseBlockPosition) this).d = k;
+ // Paper end
return this;
}
public void c(EnumDirection enumdirection) {
- this.c += enumdirection.getAdjacentX();
- this.d += enumdirection.getAdjacentY();
- this.e += enumdirection.getAdjacentZ();
+ // Paper start - modify base position variables
+ ((BaseBlockPosition) this).a += enumdirection.getAdjacentX();
+ ((BaseBlockPosition) this).c += enumdirection.getAdjacentY();
+ ((BaseBlockPosition) this).d += enumdirection.getAdjacentZ();
+ // Paper end
}
public void p(int i) {
- this.d = i;
+ ((BaseBlockPosition) this).c = i; // Paper - modify base variable
}
public BlockPosition h() {
--
2.7.4