2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 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
2020-05-12 01:18:11 +00:00
Subject: [PATCH] Improve BlockPosition inlining
2016-12-01 02:57:02 +00:00
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.
2020-05-09 14:59:29 +00:00
This is based upon conclusions drawn from inspecting the assenmbly generated bythe JIT compiler on my microbenchmarks.
2016-12-01 02:57:02 +00:00
They had 'callq' (invoke) instead of 'mov' (get from memory) instructions.
2020-06-27 14:56:32 +00:00
In addition, manually inline PooledBlockPosition#d(int, int, int) as it is a hot method that doesn't get inlined due to
invokespecial instructions.
2016-12-01 02:57:02 +00:00
diff --git a/src/main/java/net/minecraft/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
Optimize Villager Pathfinding - Massive Villager Improvement
This change reimplements the entire BehaviorFindPosition method to
get rid of all of the streams, and implement the logic in a more sane way.
We keep vanilla behavior 100% the same with this change, just wrote more
optimal, as we can abort iterating POI's as soon as we find a match....
One slight change is that Minecraft adds a random delay before a POI is
attempted again. I've increased the amount of that delay based on the distance
to said POI, so farther POI's will not be attempted as often.
Additionally, we spiral out, so we favor local POI's before we ever favor farther POI's.
We also try to pathfind 1 POI at a time instead of collecting multiple POI's then tossing them
all to the pathfinder, so that once we get a match we can return before even looking at other
POI's.
This benefits us in that ideally, a villager will constantly find the near POI's and
not even try to pathfind to the farther POI. Trying to pathfind to distant POI's is
what causes significant lag.
Other improvements here is to stop spamming the POI manager with empty nullables.
Vanilla used them to represent if they needed to load POI data off disk or not.
Well, we load POI data async on chunk load, so we have it, and we surely do not ever
want to load POI data sync either for unloaded chunks!
So this massively reduces object count in the POI hashmaps, resulting in less hash collions,
and also less memory use.
Additionally, unemployed villagers were using significant time due to major ineffeciency in
the code rebuilding data that is static every single invocation for every POI type...
So we cache that and only rebuild it if professions change, which should be never unless
a plugin manipulates and adds custom professions, which it will handle by rebuilding.
2020-05-27 05:23:19 +00:00
index 7b05bb9edcd059a134cef12cc9fea570217bc601..a0450a7ddf21659c5636b3f298e6bf4f0a93fc4d 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
2020-05-12 01:18:11 +00:00
@@ -7,32 +7,30 @@ import javax.annotation.concurrent.Immutable;
2016-12-01 02:57:02 +00:00
public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
2019-01-01 03:15:55 +00:00
2016-12-01 02:57:02 +00:00
public static final BaseBlockPosition ZERO = new BaseBlockPosition(0, 0, 0);
2019-12-11 23:43:22 +00:00
- @Deprecated
2016-12-01 02:57:02 +00:00
- private final int a;
2019-12-11 23:43:22 +00:00
- @Deprecated
2016-12-01 02:57:02 +00:00
- private final int b;
2019-12-11 23:43:22 +00:00
- @Deprecated
2016-12-01 02:57:02 +00:00
- private final int c;
2018-07-22 22:46:13 +00:00
// Paper start
2020-05-09 14:59:29 +00:00
- public boolean isValidLocation() {
- return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
2020-05-12 01:18:11 +00:00
+ protected int x;
2018-07-22 22:46:13 +00:00
+ protected int y;
+ protected int z;
2020-05-09 14:59:29 +00:00
+
+ public final boolean isValidLocation() {
2018-07-22 22:46:13 +00:00
+ return x >= -30000000 && z >= -30000000 && x < 30000000 && z < 30000000 && y >= 0 && y < 256;
2016-12-01 02:57:02 +00:00
}
2020-05-09 14:59:29 +00:00
- public boolean isInvalidYLocation() {
2018-07-22 22:46:13 +00:00
- return b < 0 || b >= 256;
2020-05-09 14:59:29 +00:00
+ public final boolean isInvalidYLocation() {
2018-07-22 22:46:13 +00:00
+ return y < 0 || y >= 256;
}
// Paper end
public BaseBlockPosition(int i, int j, int k) {
- this.a = i;
- this.b = j;
- this.c = k;
+ this.x = i;
+ this.y = j;
+ this.z = k;
}
public BaseBlockPosition(double d0, double d1, double d2) {
2020-05-09 14:59:29 +00:00
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)) {
2020-05-12 01:18:11 +00:00
@@ -40,29 +38,31 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
2020-05-09 14:59:29 +00:00
} else {
BaseBlockPosition baseblockposition = (BaseBlockPosition) object;
- return this.getX() != baseblockposition.getX() ? false : (this.getY() != baseblockposition.getY() ? false : this.getZ() == baseblockposition.getZ());
2020-05-12 01:18:11 +00:00
+ return x == baseblockposition.x && z == baseblockposition.z && y == baseblockposition.y; // Paper
2020-05-09 14:59:29 +00:00
}
}
- public int hashCode() {
- return (this.getY() + this.getZ() * 31) * 31 + this.getX();
2020-05-12 01:18:11 +00:00
+ public final int hashCode() { // Paper
+ return (this.y + this.z * 31) * 31 + this.x; // Paper
2020-05-09 14:59:29 +00:00
}
public int compareTo(BaseBlockPosition baseblockposition) {
2019-01-01 03:15:55 +00:00
return this.getY() == baseblockposition.getY() ? (this.getZ() == baseblockposition.getZ() ? this.getX() - baseblockposition.getX() : this.getZ() - baseblockposition.getZ()) : this.getY() - baseblockposition.getY();
2016-12-01 02:57:02 +00:00
}
- public int getX() {
2018-07-22 22:46:13 +00:00
- return this.a;
+ // Paper start
2016-12-01 02:57:02 +00:00
+ public final int getX() {
2018-07-22 22:46:13 +00:00
+ return this.x;
2016-12-01 02:57:02 +00:00
}
2020-05-09 14:59:29 +00:00
- public int getY() {
2018-07-22 22:46:13 +00:00
- return this.b;
2020-05-09 14:59:29 +00:00
+ public final int getY() {
2018-07-22 22:46:13 +00:00
+ return this.y;
2016-12-01 02:57:02 +00:00
}
2020-05-09 14:59:29 +00:00
- public int getZ() {
2018-07-22 22:46:13 +00:00
- return this.c;
2020-05-09 14:59:29 +00:00
+ public final int getZ() {
2018-07-22 22:46:13 +00:00
+ return this.z;
2016-12-01 02:57:02 +00:00
}
2020-05-09 14:59:29 +00:00
+ // Paper end
2019-12-11 23:43:22 +00:00
public BaseBlockPosition down() {
2020-05-09 14:59:29 +00:00
return this.down(1);
2020-05-12 01:18:11 +00:00
@@ -75,13 +75,14 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
2019-12-11 23:43:22 +00:00
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);
}
2016-12-01 02:57:02 +00:00
+ // Paper end
2019-01-01 03:15:55 +00:00
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());
2019-04-30 01:20:24 +00:00
}
public boolean a(BaseBlockPosition baseblockposition, double d0) {
2019-12-11 23:43:22 +00:00
- return this.distanceSquared((double) baseblockposition.getX(), (double) baseblockposition.getY(), (double) baseblockposition.getZ(), false) < d0 * d0;
2019-04-30 01:20:24 +00:00
+ return this.distanceSquared((double) baseblockposition.x, (double) baseblockposition.y, (double) baseblockposition.z, false) < d0 * d0; // Paper
}
public boolean a(IPosition iposition, double d0) {
Optimize Villager Pathfinding - Massive Villager Improvement
This change reimplements the entire BehaviorFindPosition method to
get rid of all of the streams, and implement the logic in a more sane way.
We keep vanilla behavior 100% the same with this change, just wrote more
optimal, as we can abort iterating POI's as soon as we find a match....
One slight change is that Minecraft adds a random delay before a POI is
attempted again. I've increased the amount of that delay based on the distance
to said POI, so farther POI's will not be attempted as often.
Additionally, we spiral out, so we favor local POI's before we ever favor farther POI's.
We also try to pathfind 1 POI at a time instead of collecting multiple POI's then tossing them
all to the pathfinder, so that once we get a match we can return before even looking at other
POI's.
This benefits us in that ideally, a villager will constantly find the near POI's and
not even try to pathfind to the farther POI. Trying to pathfind to distant POI's is
what causes significant lag.
Other improvements here is to stop spamming the POI manager with empty nullables.
Vanilla used them to represent if they needed to load POI data off disk or not.
Well, we load POI data async on chunk load, so we have it, and we surely do not ever
want to load POI data sync either for unloaded chunks!
So this massively reduces object count in the POI hashmaps, resulting in less hash collions,
and also less memory use.
Additionally, unemployed villagers were using significant time due to major ineffeciency in
the code rebuilding data that is static every single invocation for every POI type...
So we cache that and only rebuild it if professions change, which should be never unless
a plugin manipulates and adds custom professions, which it will handle by rebuilding.
2020-05-27 05:23:19 +00:00
@@ -107,9 +108,9 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
2019-04-30 01:20:24 +00:00
}
public int n(BaseBlockPosition baseblockposition) {
2019-12-11 23:43:22 +00:00
- float f = (float) Math.abs(baseblockposition.getX() - this.getX());
- float f1 = (float) Math.abs(baseblockposition.getY() - this.getY());
- float f2 = (float) Math.abs(baseblockposition.getZ() - this.getZ());
2019-04-30 01:20:24 +00:00
+ float f = (float) Math.abs(baseblockposition.getX() - this.x); // Paper
+ float f1 = (float) Math.abs(baseblockposition.getY() - this.y); // Paper
+ float f2 = (float) Math.abs(baseblockposition.getZ() - this.z); // Paper
return (int) (f + f1 + f2);
}
2016-12-01 02:57:02 +00:00
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
2020-06-27 14:56:32 +00:00
index f8ac39e1b019b0918996f745d99f6ed09db0fd11..9e523157f9daf8930afd96ab8701e500eb078f42 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
2020-05-09 14:59:29 +00:00
@@ -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;
2020-05-12 01:18:11 +00:00
@@ -113,7 +114,7 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2020-05-09 14:59:29 +00:00
}
2020-05-12 01:18:11 +00:00
public long asLong() {
- return a(this.getX(), this.getY(), this.getZ());
+ return a(this.x, this.y, this.z); // Paper
2020-05-09 14:59:29 +00:00
}
2020-05-12 01:18:11 +00:00
public BlockPosition a(double d0, double d1, double d2) {
2020-06-27 14:56:32 +00:00
@@ -298,7 +299,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
@Override
public BlockPosition.PooledBlockPosition d(int i, int j, int k) {
- return (BlockPosition.PooledBlockPosition) super.d(i, j, k);
+ // Paper start - manually inline this hot method
+ //return (BlockPosition.PooledBlockPosition) super.d(i, j, k);
+ this.x = i;
+ this.y = j;
+ this.z = k;
+ return this;
+ // Paper end
}
@Override
@@ -343,11 +350,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2019-01-01 03:15:55 +00:00
}
2016-12-01 02:57:02 +00:00
public static class MutableBlockPosition extends BlockPosition {
2019-01-01 03:15:55 +00:00
-
2018-07-22 22:46:13 +00:00
+ // Paper start - comment out
2016-12-01 02:57:02 +00:00
+ /*
protected int b;
protected int c;
protected int d;
2019-12-11 23:43:22 +00:00
-
2016-12-01 02:57:02 +00:00
+ */
2019-12-11 23:43:22 +00:00
+ // Paper end
2016-12-01 02:57:02 +00:00
public MutableBlockPosition() {
2019-12-11 23:43:22 +00:00
this(0, 0, 0);
}
2020-06-27 14:56:32 +00:00
@@ -357,10 +366,13 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2018-07-22 22:46:13 +00:00
}
2016-12-01 02:57:02 +00:00
public MutableBlockPosition(int i, int j, int k) {
2018-07-22 22:46:13 +00:00
- super(0, 0, 0);
+ // Paper start
+ super(i, j, k);
+ /*
this.b = i;
this.c = j;
2016-12-01 02:57:02 +00:00
- this.d = k;
2018-07-22 22:46:13 +00:00
+ this.d = k;*/
2016-12-01 02:57:02 +00:00
+ // Paper end
}
2019-04-30 01:20:24 +00:00
public MutableBlockPosition(double d0, double d1, double d2) {
2020-06-27 14:56:32 +00:00
@@ -391,6 +403,9 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2019-04-30 01:20:24 +00:00
return super.a(enumblockrotation).immutableCopy();
2016-12-01 02:57:02 +00:00
}
2019-04-30 01:20:24 +00:00
+
2016-12-01 02:57:02 +00:00
+ /*
2018-07-22 22:46:13 +00:00
+ // Paper start - use parent getters
2019-04-30 01:20:24 +00:00
@Override
2016-12-01 02:57:02 +00:00
public int getX() {
return this.b;
2020-06-27 14:56:32 +00:00
@@ -404,13 +419,16 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2019-04-30 01:20:24 +00:00
@Override
2016-12-01 02:57:02 +00:00
public int getZ() {
return this.d;
2018-07-22 22:46:13 +00:00
- }
+ }*/
2016-12-01 02:57:02 +00:00
+ // Paper end
2019-04-30 01:20:24 +00:00
public BlockPosition.MutableBlockPosition setValues(int i, int j, int k) { return d(i, j, k);} // Paper - OBFHELPER
public BlockPosition.MutableBlockPosition d(int i, int j, int k) {
2016-12-01 02:57:02 +00:00
- this.b = i;
- this.c = j;
- this.d = k;
2018-07-22 22:46:13 +00:00
+ // Paper start - use xyz
+ this.x = i;
+ this.y = j;
+ this.z = k;
2016-12-01 02:57:02 +00:00
+ // Paper end
return this;
}
2020-06-27 14:56:32 +00:00
@@ -440,26 +458,26 @@ public class BlockPosition extends BaseBlockPosition implements MinecraftSeriali
2016-12-01 02:57:02 +00:00
}
public BlockPosition.MutableBlockPosition c(EnumDirection enumdirection, int i) {
2019-04-30 01:20:24 +00:00
- return this.d(this.b + enumdirection.getAdjacentX() * i, this.c + enumdirection.getAdjacentY() * i, this.d + enumdirection.getAdjacentZ() * i);
+ return this.d(this.x + enumdirection.getAdjacentX() * i, this.y + enumdirection.getAdjacentY() * i, this.z + enumdirection.getAdjacentZ() * i);
2018-07-22 22:46:13 +00:00
}
2019-04-30 01:20:24 +00:00
public BlockPosition.MutableBlockPosition e(int i, int j, int k) {
- return this.d(this.b + i, this.c + j, this.d + k);
+ return this.d(this.x + i, this.y + j, this.z + k);
2016-12-01 02:57:02 +00:00
}
2020-01-28 00:16:53 +00:00
public final void setX(final int x) { this.o(x); } // Paper - OBFHELPER
2019-06-25 01:47:58 +00:00
public void o(int i) {
- this.b = i;
+ this.x = i; // Paper change to x
}
2020-01-28 00:16:53 +00:00
public final void setY(final int y) { this.p(y); } // Paper - OBFHELPER
2016-12-01 02:57:02 +00:00
public void p(int i) {
- this.c = i;
2018-07-22 22:46:13 +00:00
+ this.y = i; // Paper change to y
2016-12-01 02:57:02 +00:00
}
2020-01-28 00:16:53 +00:00
public final void setZ(final int z) { this.q(z); } // Paper - OBFHELPER
2019-06-25 01:47:58 +00:00
public void q(int i) {
- this.d = i;
+ this.z = i; // Paper change to z
}
2019-04-30 01:20:24 +00:00
@Override