From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Techcable 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 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 @@ import com.google.common.base.Objects; public class BaseBlockPosition implements Comparable { 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; @@ -0,0 +0,0 @@ public class BaseBlockPosition implements Comparable { 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/server/BlockPosition.java +++ b/src/main/java/net/minecraft/server/BlockPosition.java @@ -0,0 +0,0 @@ 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; } } @@ -0,0 +0,0 @@ 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 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() { @@ -0,0 +0,0 @@ public class BlockPosition extends BaseBlockPosition { } } + // Paper start - use superclass methods + /* public int getX() { return this.c; } @@ -0,0 +0,0 @@ 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) { @@ -0,0 +0,0 @@ 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; } @@ -0,0 +0,0 @@ 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) { @@ -0,0 +0,0 @@ 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); @@ -0,0 +0,0 @@ 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; } @@ -0,0 +0,0 @@ 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() { --