Cache Hashcode for BlockPosition

This commit is contained in:
Sotr
2020-04-15 04:28:58 +07:00
parent 78713a1039
commit 1f992c63e9

View File

@@ -0,0 +1,73 @@
From 64251523cd583604d4806817b6115ad35919d726 Mon Sep 17 00:00:00 2001
From: Sotr <i@omc.hk>
Date: Wed, 15 Apr 2020 04:28:25 +0700
Subject: [PATCH] Cache Hashcode for BlockPosition
diff --git a/src/main/java/net/minecraft/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
index c439a8d01..808f2eb6c 100644
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
@@ -18,6 +18,7 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
return y < 0 || y >= 256;
}
// Paper end
+ protected int hash; // Akarin - cache hashcode
public BaseBlockPosition(int i, int j, int k) {
this.x = i;
@@ -42,8 +43,18 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
}
public int hashCode() {
- return (this.getY() + this.getZ() * 31) * 31 + this.getX();
+ // Akarin start - cache hashcode
+ if (hash == 0) {
+ hash = (this.getY() + this.getZ() * 31) * 31 + this.getX();
+ }
+ return hash;
+ // return (this.getY() + this.getZ() * 31) * 31 + this.getX();
+ }
+
+ public void recalcHashCode() {
+ hash = (this.getY() + this.getZ() * 31) * 31 + this.getX();
}
+ // Akarin 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();
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index 2d07d350d..43e7e5c27 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
@@ -439,6 +439,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
this.y = j;
this.z = k;
// Paper end
+ this.recalcHashCode(); // Akarin - cache hashcode
return this;
}
@@ -479,16 +480,19 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
public final void setX(final int x) { this.o(x); } // Paper - OBFHELPER
public void o(int i) {
this.x = i; // Paper change to x
+ this.recalcHashCode(); // Akarin - cache hashcode
}
public final void setY(final int y) { this.p(y); } // Paper - OBFHELPER
public void p(int i) {
this.y = i; // Paper change to y
+ this.recalcHashCode(); // Akarin - cache hashcode
}
public final void setZ(final int z) { this.q(z); } // Paper - OBFHELPER
public void q(int i) {
this.z = i; // Paper change to z
+ this.recalcHashCode(); // Akarin - cache hashcode
}
@Override
--
2.25.1.windows.1