papermc/Spigot-Server-Patches/0180-Speedup-BlockPos-by-fixing-inlining.patch

182 lines
8.2 KiB
Diff
Raw Normal View History

From cb3aa8961d8f87ea76901499ce0a13a3a28103f8 Mon Sep 17 00:00:00 2001
2016-12-01 02:57:02 +00:00
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
2016-12-20 22:34:27 +00:00
index f772c7c..f5dc144 100644
2016-12-01 02:57:02 +00:00
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
2016-12-20 22:34:27 +00:00
@@ -7,11 +7,12 @@ import javax.annotation.concurrent.Immutable;
2016-12-01 02:57:02 +00:00
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
2016-12-20 22:34:27 +00:00
+ protected int a;
+ protected int b;
+ protected int c;
2016-12-01 02:57:02 +00:00
+
+ public final boolean isValidLocation() {
2016-12-20 22:34:27 +00:00
return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
2016-12-01 02:57:02 +00:00
}
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() {
2016-12-20 22:34:27 +00:00
return this.a;
2016-12-01 02:57:02 +00:00
}
- public int getY() {
+ public final int getY() {
2016-12-20 22:34:27 +00:00
return this.b;
2016-12-01 02:57:02 +00:00
}
- public int getZ() {
+ public final int getZ() {
2016-12-20 22:34:27 +00:00
return this.c;
2016-12-01 02:57:02 +00:00
}
+ // 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 b3c1f55..6a0b3a6 100644
2016-12-01 02:57:02 +00:00
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
2016-12-20 22:34:27 +00:00
@@ -211,19 +211,21 @@ public class BlockPosition extends BaseBlockPosition {
if (this.b == null) {
this.b = new BlockPosition.MutableBlockPosition(i, j, k);
return this.b;
- } else if (this.b.b == l && this.b.c == i1 && this.b.d == j1) {
+ // Paper start - b, c, d, refer to x, y, z, and as such, a, b, c of BaseBlockPosition
+ } else if (((BaseBlockPosition)this.b).a == l && ((BaseBlockPosition)this.b).b == i1 && ((BaseBlockPosition)this.b).c == j1) {
return (BlockPosition.MutableBlockPosition) this.endOfData();
} else {
- if (this.b.b < l) {
- ++this.b.b;
- } else if (this.b.c < i1) {
- this.b.b = i;
- ++this.b.c;
- } else if (this.b.d < j1) {
- this.b.b = i;
- this.b.c = j;
- ++this.b.d;
+ if (((BaseBlockPosition) this.b).a < l) {
+ ++((BaseBlockPosition) this.b).a;
+ } else if (((BaseBlockPosition) this.b).b < i1) {
+ ((BaseBlockPosition) this.b).a = i;
+ ++((BaseBlockPosition) this.b).b;
+ } else if (((BaseBlockPosition) this.b).c < j1) {
2016-12-20 22:34:27 +00:00
+ ((BaseBlockPosition) this.b).a = i;
+ ((BaseBlockPosition) this.b).b = j;
+ ++((BaseBlockPosition) this.b).c;
2016-12-01 02:57:02 +00:00
}
2016-12-20 22:34:27 +00:00
+ // Paper end
2016-12-01 02:57:02 +00:00
return this.b;
}
2016-12-20 22:34:27 +00:00
@@ -338,6 +340,8 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
public static class MutableBlockPosition extends BlockPosition {
+ // Paper start - Remove variables
+ /*
protected int b;
protected int c;
protected int d;
2016-12-20 22:34:27 +00:00
@@ -350,6 +354,7 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
public boolean isInvalidYLocation() {
return c < 0 || c >= 256;
}
+ */
// Paper end
public MutableBlockPosition() {
2016-12-20 22:34:27 +00:00
@@ -362,9 +367,11 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
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
2016-12-20 22:34:27 +00:00
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).b = j;
+ ((BaseBlockPosition) this).c = k;
2016-12-01 02:57:02 +00:00
+ // Paper end
}
public BlockPosition a(double d0, double d1, double d2) {
2016-12-20 22:34:27 +00:00
@@ -383,6 +390,8 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
return super.a(enumblockrotation).h();
}
+ // Paper start - Use superclass methods
+ /*
public int getX() {
return this.b;
}
2016-12-20 22:34:27 +00:00
@@ -394,12 +403,16 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
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
2016-12-20 22:34:27 +00:00
+ ((BaseBlockPosition) this).a = i;
+ ((BaseBlockPosition) this).b = j;
+ ((BaseBlockPosition) this).c = k;
2016-12-01 02:57:02 +00:00
+ // Paper end
return this;
}
2016-12-20 22:34:27 +00:00
@@ -416,11 +429,11 @@ public class BlockPosition extends BaseBlockPosition {
2016-12-01 02:57:02 +00:00
}
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;
2016-12-20 22:34:27 +00:00
+ ((BaseBlockPosition) this).b = i; // Paper - Modify base variable
2016-12-01 02:57:02 +00:00
}
public BlockPosition h() {
--
2016-12-10 23:50:31 +00:00
2.9.3
2016-12-01 02:57:02 +00:00