Improve BlockPosition hashCode/equals

Actually showed up in profiling as decent time spent here...

Noticed y/z was missing its final that it use to have, when x had it. some how
must of got messed up on some update. though people suggest this shouldn't of
mattered anyways, but lets put it back for safety.

Added cache of hashcode, as well as optimized the hash code using larger primes.
Also stored the long value of the x/y/z so that for equals we can compare a single long,
as well as have that long value cached for .asLong()
This commit is contained in:
Aikar 2020-05-09 10:59:29 -04:00
parent 2572bd017c
commit 2d401d2dfb
No known key found for this signature in database
GPG key ID: 401ADFC9891FAAFE
4 changed files with 100 additions and 25 deletions

View file

@ -1,7 +1,7 @@
From 0000000000000000000000000000000000000000 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
Subject: [PATCH] Improve BlockPosition inline and hashCode/equals
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.
@ -17,14 +17,18 @@ 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.
This is based upon conclusions drawn from inspecting the assenmbly generated bythe JIT compiler on my microbenchmarks.
They had 'callq' (invoke) instead of 'mov' (get from memory) instructions.
Co-Authored-By: Aikar <aikar@aikar.co>
for:
Also cache the hashCode and long values to use it for speeding up hashmap usage
diff --git a/src/main/java/net/minecraft/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640ca13e5e7c 100644
index 71089442c189336fc0061852a661581784a64013..334a6ab0df1978c42fdf8ad454e96f60e70c76dc 100644
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
@@ -7,25 +7,22 @@ import javax.annotation.concurrent.Immutable;
@@ -7,32 +7,32 @@ import javax.annotation.concurrent.Immutable;
public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
public static final BaseBlockPosition ZERO = new BaseBlockPosition(0, 0, 0);
@ -35,15 +39,20 @@ index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640c
- @Deprecated
- private final int c;
// Paper start
+ protected int x;
- public boolean isValidLocation() {
- return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
+ protected int x; // If these ever become non final, review this entire patch, look for places need to rehash on write
+ protected int y;
+ protected int z;
public boolean isValidLocation() {
- return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
+ private int hashCodeCached = 0;
+ private long cachedLong = Long.MAX_VALUE;
+
+ public final boolean isValidLocation() {
+ return x >= -30000000 && z >= -30000000 && x < 30000000 && z < 30000000 && y >= 0 && y < 256;
}
public boolean isInvalidYLocation() {
- public boolean isInvalidYLocation() {
- return b < 0 || b >= 256;
+ public final boolean isInvalidYLocation() {
+ return y < 0 || y >= 256;
}
// Paper end
@ -58,7 +67,48 @@ index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640c
}
public BaseBlockPosition(double d0, double d1, double d2) {
@@ -52,16 +49,17 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
this(MathHelper.floor(d0), MathHelper.floor(d1), MathHelper.floor(d2));
}
- public boolean equals(Object object) {
+ public final boolean equals(Object object) { // Paper
if (this == object) {
return true;
} else if (!(object instanceof BaseBlockPosition)) {
@@ -40,29 +40,49 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
} else {
BaseBlockPosition baseblockposition = (BaseBlockPosition) object;
- return this.getX() != baseblockposition.getX() ? false : (this.getY() != baseblockposition.getY() ? false : this.getZ() == baseblockposition.getZ());
+ return this.asLong() == baseblockposition.asLong(); // Paper
}
}
- public int hashCode() {
- return (this.getY() + this.getZ() * 31) * 31 + this.getX();
+ public final int hashCode() {
+ // Paper start
+ if (this.cachedLong == Long.MAX_VALUE) {
+ rebuildCache();
+ }
+ return this.hashCodeCached;
+ }
+ private void rebuildCache() {
+ this.cachedLong = BlockPosition.asLong(getX(), getY(), getZ());
+ this.hashCodeCached = (this.getY() + this.getZ() * 1031) * 1031 + this.getX();
+ }
+ public final long asLong() {
+ if (this.cachedLong == Long.MAX_VALUE) {
+ rebuildCache();
+ }
+ return this.cachedLong;
+ }
+ public final void rehash() {
+ this.cachedLong = Long.MAX_VALUE;
+ // Paper end
}
public int compareTo(BaseBlockPosition baseblockposition) {
return this.getY() == baseblockposition.getY() ? (this.getZ() == baseblockposition.getZ() ? this.getX() - baseblockposition.getX() : this.getZ() - baseblockposition.getZ()) : this.getY() - baseblockposition.getY();
}
@ -69,18 +119,22 @@ index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640c
+ return this.x;
}
public int getY() {
- public int getY() {
- return this.b;
+ public final int getY() {
+ return this.y;
}
public int getZ() {
- public int getZ() {
- return this.c;
+ public final int getZ() {
+ return this.z;
}
+ // Paper end
public BaseBlockPosition down() {
@@ -75,13 +73,14 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
return this.down(1);
@@ -75,13 +95,14 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
public BaseBlockPosition shift(EnumDirection enumdirection, int i) {
return i == 0 ? this : new BaseBlockPosition(this.getX() + enumdirection.getAdjacentX() * i, this.getY() + enumdirection.getAdjacentY() * i, this.getZ() + enumdirection.getAdjacentZ() * i);
}
@ -96,7 +150,7 @@ index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640c
}
public boolean a(IPosition iposition, double d0) {
@@ -106,9 +105,9 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
@@ -106,9 +127,9 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
}
public int n(BaseBlockPosition baseblockposition) {
@ -110,10 +164,27 @@ index 71089442c189336fc0061852a661581784a64013..c439a8d0191c8667c881b2111b8c640c
return (int) (f + f1 + f2);
}
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b1120d3f5 100644
index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ce39e34c1d60ec0fc9d4a0e1b802d8c96177b4b 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -343,11 +343,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -99,6 +99,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
return new BlockPosition(b(i), c(i), d(i));
}
+ public static long asLong(int x, int y, int z) { return a(x, y, z); } // Paper - OBFHELPER
public static long a(int i, int j, int k) {
long l = 0L;
@@ -112,7 +113,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
return i & -16L;
}
- public long asLong() {
+ public long asLong_unused() { // Paper - moved to parent
return a(this.getX(), this.getY(), this.getZ());
}
@@ -343,11 +344,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
}
public static class MutableBlockPosition extends BlockPosition {
@ -129,7 +200,7 @@ index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b
public MutableBlockPosition() {
this(0, 0, 0);
}
@@ -357,10 +359,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -357,10 +360,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
}
public MutableBlockPosition(int i, int j, int k) {
@ -145,7 +216,7 @@ index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b
}
public MutableBlockPosition(double d0, double d1, double d2) {
@@ -391,6 +396,9 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -391,6 +397,9 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
return super.a(enumblockrotation).immutableCopy();
}
@ -155,7 +226,7 @@ index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b
@Override
public int getX() {
return this.b;
@@ -404,13 +412,16 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -404,13 +413,17 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@Override
public int getZ() {
return this.d;
@ -172,11 +243,12 @@ index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b
+ this.x = i;
+ this.y = j;
+ this.z = k;
+ rehash();
+ // Paper end
return this;
}
@@ -440,26 +451,26 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -440,26 +453,29 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
}
public BlockPosition.MutableBlockPosition c(EnumDirection enumdirection, int i) {
@ -193,18 +265,21 @@ index f8ac39e1b019b0918996f745d99f6ed09db0fd11..0ab2b23440346a0faa22ffb4d585662b
public void o(int i) {
- this.b = i;
+ this.x = i; // Paper change to x
+ rehash(); // Paper
}
public final void setY(final int y) { this.p(y); } // Paper - OBFHELPER
public void p(int i) {
- this.c = i;
+ this.y = i; // Paper change to y
+ rehash(); // Paper
}
public final void setZ(final int z) { this.q(z); } // Paper - OBFHELPER
public void q(int i) {
- this.d = i;
+ this.z = i; // Paper change to z
+ rehash(); // Paper
}
@Override

View file

@ -6,10 +6,10 @@ Subject: [PATCH] Optimize BlockPosition helper methods
Resolves #1338
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index 0ab2b23440346a0faa22ffb4d585662b1120d3f5..db7ba12fd4fa4f30b94471f1efbec5426b99a5db 100644
index 0ce39e34c1d60ec0fc9d4a0e1b802d8c96177b4b..df93e32242bb91ddb1c2da8965956d38ee381dc2 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -134,57 +134,74 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -135,57 +135,74 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
}
public BlockPosition up() {

View file

@ -99,10 +99,10 @@ index 6d351f0979ecfa8e500edf8dd03b4a455fd5d180..a44f65f40d2080b63069602a454266ee
@Override
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index db7ba12fd4fa4f30b94471f1efbec5426b99a5db..9010359fbdc393e43ab2a0702cfc27427458b1ff 100644
index df93e32242bb91ddb1c2da8965956d38ee381dc2..063f8eb08635aaa44803f2a67d118805294ae938 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -451,6 +451,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -453,6 +453,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
return this.d(MathHelper.floor(d0), MathHelper.floor(d1), MathHelper.floor(d2));
}

View file

@ -883,10 +883,10 @@ index 0000000000000000000000000000000000000000..118988c39e58f28e8a2851792b9c014f
+ }
+}
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index 9010359fbdc393e43ab2a0702cfc27427458b1ff..07813c55cdc85e08697d4126973f829ae564cf41 100644
index 063f8eb08635aaa44803f2a67d118805294ae938..58ba1209155e05e802171800f4420e3ae419950f 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -125,6 +125,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@@ -126,6 +126,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
return i == 0 && j == 0 && k == 0 ? this : new BlockPosition(this.getX() + i, this.getY() + j, this.getZ() + k);
}