Update more feature patches

This commit is contained in:
Nassim Jahnke 2024-12-16 23:46:48 +01:00
parent 57c683647f
commit e7995023f1
No known key found for this signature in database
GPG key ID: EF6771C01F6EF02F
2 changed files with 122 additions and 118 deletions

View file

@ -6,11 +6,12 @@ Subject: [PATCH] Optimize Bit Operations by inlining
Inline bit operations and reduce instruction count to make these hot Inline bit operations and reduce instruction count to make these hot
operations faster operations faster
diff --git a/src/main/java/net/minecraft/core/BlockPos.java b/src/main/java/net/minecraft/core/BlockPos.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 diff --git a/net/minecraft/core/BlockPos.java b/net/minecraft/core/BlockPos.java
--- a/src/main/java/net/minecraft/core/BlockPos.java index 98f0b1cf19d7a035849a9a2fa25e2be3a4c5a980..01ba7bc16e30440e35ce062bc21dae7e59bb6f8d 100644
+++ b/src/main/java/net/minecraft/core/BlockPos.java --- a/net/minecraft/core/BlockPos.java
@@ -0,0 +0,0 @@ public class BlockPos extends Vec3i { +++ b/net/minecraft/core/BlockPos.java
@@ -51,15 +51,17 @@ public class BlockPos extends Vec3i {
}; };
private static final Logger LOGGER = LogUtils.getLogger(); private static final Logger LOGGER = LogUtils.getLogger();
public static final BlockPos ZERO = new BlockPos(0, 0, 0); public static final BlockPos ZERO = new BlockPos(0, 0, 0);
@ -36,18 +37,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
public BlockPos(int x, int y, int z) { public BlockPos(int x, int y, int z) {
super(x, y, z); super(x, y, z);
@@ -0,0 +0,0 @@ public class BlockPos extends Vec3i { @@ -69,28 +71,29 @@ public class BlockPos extends Vec3i {
this(pos.getX(), pos.getY(), pos.getZ()); this(vector.getX(), vector.getY(), vector.getZ());
} }
+ public static long getAdjacent(int baseX, int baseY, int baseZ, Direction enumdirection) { return asLong(baseX + enumdirection.getStepX(), baseY + enumdirection.getStepY(), baseZ + enumdirection.getStepZ()); } // Paper + public static long getAdjacent(int baseX, int baseY, int baseZ, Direction direction) { return asLong(baseX + direction.getStepX(), baseY + direction.getStepY(), baseZ + direction.getStepZ()); } // Paper
public static long offset(long value, Direction direction) { public static long offset(long pos, Direction direction) {
return offset(value, direction.getStepX(), direction.getStepY(), direction.getStepZ()); return offset(pos, direction.getStepX(), direction.getStepY(), direction.getStepZ());
} }
public static long offset(long value, int x, int y, int z) { public static long offset(long pos, int dx, int dy, int dz) {
- return asLong(getX(value) + x, getY(value) + y, getZ(value) + z); - return asLong(getX(pos) + dx, getY(pos) + dy, getZ(pos) + dz);
+ return asLong((int) (value >> 38) + x, (int) ((value << 52) >> 52) + y, (int) ((value << 26) >> 38) + z); // Paper - simplify/inline + return asLong((int) (pos >> 38) + dx, (int) ((pos << 52) >> 52) + dy, (int) ((pos << 26) >> 38) + dz); // Paper - simplify/inline
} }
public static int getX(long packedPos) { public static int getX(long packedPos) {
@ -71,23 +72,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
} }
public static BlockPos containing(double x, double y, double z) { public static BlockPos containing(double x, double y, double z) {
@@ -0,0 +0,0 @@ public class BlockPos extends Vec3i { @@ -114,10 +117,7 @@ public class BlockPos extends Vec3i {
} }
public static long asLong(int x, int y, int z) { public static long asLong(int x, int y, int z) {
- long l = 0L; - long l = 0L;
- l |= ((long)x & PACKED_X_MASK) << X_OFFSET; - l |= (x & PACKED_X_MASK) << X_OFFSET;
- l |= ((long)y & PACKED_Y_MASK) << 0; - l |= (y & PACKED_Y_MASK) << 0;
- return l | ((long)z & PACKED_Z_MASK) << Z_OFFSET; - return l | (z & PACKED_Z_MASK) << Z_OFFSET;
+ return (((long) x & (long) 67108863) << 38) | (((long) y & (long) 4095)) | (((long) z & (long) 67108863) << 12); // Paper - inline constants and simplify + return ((x & 67108863L) << 38) | ((y & 4095L)) | ((z & 67108863L) << 12); // Paper - inline constants and simplify
} }
public static long getFlatIndex(long y) { public static long getFlatIndex(long packedPos) {
diff --git a/src/main/java/net/minecraft/core/SectionPos.java b/src/main/java/net/minecraft/core/SectionPos.java diff --git a/net/minecraft/core/SectionPos.java b/net/minecraft/core/SectionPos.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 1780d8e14cea62971da75e4dcc80d1805434037b..737f853a264d720ce15f6bbffc206e3003fff380 100644
--- a/src/main/java/net/minecraft/core/SectionPos.java --- a/net/minecraft/core/SectionPos.java
+++ b/src/main/java/net/minecraft/core/SectionPos.java +++ b/net/minecraft/core/SectionPos.java
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -38,7 +38,7 @@ public class SectionPos extends Vec3i {
} }
public static SectionPos of(BlockPos pos) { public static SectionPos of(BlockPos pos) {
@ -96,7 +97,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
} }
public static SectionPos of(ChunkPos chunkPos, int y) { public static SectionPos of(ChunkPos chunkPos, int y) {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -54,7 +54,7 @@ public class SectionPos extends Vec3i {
} }
public static SectionPos of(long packed) { public static SectionPos of(long packed) {
@ -105,109 +106,108 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
} }
public static SectionPos bottomOf(ChunkAccess chunk) { public static SectionPos bottomOf(ChunkAccess chunk) {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -65,8 +65,16 @@ public class SectionPos extends Vec3i {
return offset(packed, direction.getStepX(), direction.getStepY(), direction.getStepZ()); return offset(packed, direction.getStepX(), direction.getStepY(), direction.getStepZ());
} }
+ // Paper start + // Paper start
+ public static long getAdjacentFromBlockPos(int x, int y, int z, Direction enumdirection) { + public static long getAdjacentFromBlockPos(int x, int y, int z, Direction direction) {
+ return (((long) ((x >> 4) + enumdirection.getStepX()) & 4194303L) << 42) | (((long) ((y >> 4) + enumdirection.getStepY()) & 1048575L)) | (((long) ((z >> 4) + enumdirection.getStepZ()) & 4194303L) << 20); + return (((long) ((x >> 4) + direction.getStepX()) & 4194303L) << 42) | (((long) ((y >> 4) + direction.getStepY()) & 1048575L)) | (((long) ((z >> 4) + direction.getStepZ()) & 4194303L) << 20);
+ } + }
+ public static long getAdjacentFromSectionPos(int x, int y, int z, Direction enumdirection) { + public static long getAdjacentFromSectionPos(int x, int y, int z, Direction direction) {
+ return (((long) (x + enumdirection.getStepX()) & 4194303L) << 42) | (((long) ((y) + enumdirection.getStepY()) & 1048575L)) | (((long) (z + enumdirection.getStepZ()) & 4194303L) << 20); + return (((long) (x + direction.getStepX()) & 4194303L) << 42) | (((long) ((y) + direction.getStepY()) & 1048575L)) | (((long) (z + direction.getStepZ()) & 4194303L) << 20);
+ } + }
+ // Paper end + // Paper end
public static long offset(long packed, int x, int y, int z) { public static long offset(long packed, int dx, int dy, int dz) {
- return asLong(x(packed) + x, y(packed) + y, z(packed) + z); - return asLong(x(packed) + dx, y(packed) + dy, z(packed) + dz);
+ return (((long) ((int) (packed >> 42) + x) & 4194303L) << 42) | (((long) ((int) (packed << 44 >> 44) + y) & 1048575L)) | (((long) ((int) (packed << 22 >> 42) + z) & 4194303L) << 20); // Simplify to reduce instruction count + return (((long) ((int) (packed >> 42) + dx) & 4194303L) << 42) | (((long) ((int) (packed << 44 >> 44) + dy) & 1048575L)) | (((long) ((int) (packed << 22 >> 42) + dz) & 4194303L) << 20); // Simplify to reduce instruction count
} }
public static int posToSectionCoord(double coord) { public static int posToSectionCoord(double pos) {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -86,10 +94,7 @@ public class SectionPos extends Vec3i {
} }
public static short sectionRelativePos(BlockPos pos) { public static short sectionRelativePos(BlockPos pos) {
- int i = sectionRelative(pos.getX()); - int relativeBlockPosX = sectionRelative(pos.getX());
- int j = sectionRelative(pos.getY()); - int relativeBlockPosY = sectionRelative(pos.getY());
- int k = sectionRelative(pos.getZ()); - int relativeBlockPosZ = sectionRelative(pos.getZ());
- return (short)(i << 8 | k << 4 | j << 0); - return (short)(relativeBlockPosX << 8 | relativeBlockPosZ << 4 | relativeBlockPosY << 0);
+ return (short) ((pos.getX() & 15) << 8 | (pos.getZ() & 15) << 4 | pos.getY() & 15); // Paper - simplify/inline + return (short) ((pos.getX() & 15) << 8 | (pos.getZ() & 15) << 4 | pos.getY() & 15); // Paper - simplify/inline
} }
public static int sectionRelativeX(short packedLocalPos) { public static int sectionRelativeX(short x) {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -152,16 +157,16 @@ public class SectionPos extends Vec3i {
return this.getZ(); return this.getZ();
} }
- public int minBlockX() { - public int minBlockX() {
- return sectionToBlockCoord(this.x()); - return sectionToBlockCoord(this.x());
+ public final int minBlockX() { // Paper - make final + public final int minBlockX() { // Paper - final
+ return this.getX() << 4; // Paper - inline + return this.getX() << 4; // Paper - inline
} }
- public int minBlockY() { - public int minBlockY() {
- return sectionToBlockCoord(this.y()); - return sectionToBlockCoord(this.y());
+ public final int minBlockY() { // Paper - make final + public final int minBlockY() { // Paper - final
+ return this.getY() << 4; // Paper - inline + return this.getY() << 4; // Paper - inline
} }
- public int minBlockZ() { - public int minBlockZ() {
- return sectionToBlockCoord(this.z()); - return sectionToBlockCoord(this.z());
+ public int minBlockZ() { // Paper - make final + public final int minBlockZ() { // Paper - final
+ return this.getZ() << 4; // Paper - inline + return this.getZ() << 4; // Paper - inline
} }
public int maxBlockX() { public int maxBlockX() {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -177,7 +182,7 @@ public class SectionPos extends Vec3i {
} }
public static long blockToSection(long blockPos) { public static long blockToSection(long levelPos) {
- return asLong(blockToSectionCoord(BlockPos.getX(blockPos)), blockToSectionCoord(BlockPos.getY(blockPos)), blockToSectionCoord(BlockPos.getZ(blockPos))); - return asLong(blockToSectionCoord(BlockPos.getX(levelPos)), blockToSectionCoord(BlockPos.getY(levelPos)), blockToSectionCoord(BlockPos.getZ(levelPos)));
+ // b(a(BlockPosition.b(i)), a(BlockPosition.c(i)), a(BlockPosition.d(i))); + return (((long) (int) (levelPos >> 42) & 4194303L) << 42) | (((long) (int) ((levelPos << 52) >> 56) & 1048575L)) | (((long) (int) ((levelPos << 26) >> 42) & 4194303L) << 20); // Simplify to reduce instruction count
+ return (((long) (int) (blockPos >> 42) & 4194303L) << 42) | (((long) (int) ((blockPos << 52) >> 56) & 1048575L)) | (((long) (int) ((blockPos << 26) >> 42) & 4194303L) << 20); // Simplify to reduce instruction count
} }
public static long getZeroNode(int x, int z) { public static long getZeroNode(int x, int z) {
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -205,15 +210,17 @@ public class SectionPos extends Vec3i {
return asLong(blockToSectionCoord(pos.getX()), blockToSectionCoord(pos.getY()), blockToSectionCoord(pos.getZ())); return asLong(blockToSectionCoord(blockPos.getX()), blockToSectionCoord(blockPos.getY()), blockToSectionCoord(blockPos.getZ()));
} }
+ // Paper start + // Paper start
+ public static long blockPosAsSectionLong(int i, int j, int k) { + public static long blockPosAsSectionLong(int x, int y, int z) {
+ return (((long) (i >> 4) & 4194303L) << 42) | (((long) (j >> 4) & 1048575L)) | (((long) (k >> 4) & 4194303L) << 20); + return (((long) (x >> 4) & 4194303L) << 42) | (((long) (y >> 4) & 1048575L)) | (((long) (z >> 4) & 4194303L) << 20);
+ } + }
+ // Paper end + // Paper end
+
public static long asLong(int x, int y, int z) { public static long asLong(int x, int y, int z) {
- long l = 0L; - long l = 0L;
- l |= ((long)x & 4194303L) << 42; - l |= (x & 4194303L) << 42;
- l |= ((long)y & 1048575L) << 0; - l |= (y & 1048575L) << 0;
- return l | ((long)z & 4194303L) << 20; - return l | (z & 4194303L) << 20;
+ return (((long) x & 4194303L) << 42) | (((long) y & 1048575L)) | (((long) z & 4194303L) << 20); // Paper - Simplify to reduce instruction count + return ((x & 4194303L) << 42) | ((y & 1048575L)) | ((z & 4194303L) << 20); // Paper - Simplify to reduce instruction count
} }
public long asLong() { public long asLong() {
- return asLong(this.x(), this.y(), this.z()); - return asLong(this.x(), this.y(), this.z());
+ return (((long) getX() & 4194303L) << 42) | (((long) getY() & 1048575L)) | (((long) getZ() & 4194303L) << 20); // Paper - Simplify to reduce instruction count + return ((this.getX() & 4194303L) << 42) | ((this.getY() & 1048575L)) | ((this.getZ() & 4194303L) << 20); // Paper - Simplify to reduce instruction count
} }
@Override @Override
@@ -0,0 +0,0 @@ public class SectionPos extends Vec3i { @@ -226,16 +233,13 @@ public class SectionPos extends Vec3i {
} }
public static Stream<SectionPos> cube(SectionPos center, int radius) { public static Stream<SectionPos> cube(SectionPos center, int radius) {
- int i = center.x(); - int sectionX = center.x();
- int j = center.y(); - int sectionY = center.y();
- int k = center.z(); - int sectionZ = center.z();
- return betweenClosedStream(i - radius, j - radius, k - radius, i + radius, j + radius, k + radius); - return betweenClosedStream(sectionX - radius, sectionY - radius, sectionZ - radius, sectionX + radius, sectionY + radius, sectionZ + radius);
+ return betweenClosedStream(center.getX() - radius, center.getY() - radius, center.getZ() - radius, center.getX() + radius, center.getY() + radius, center.getZ() + radius); // Paper - simplify/inline + return betweenClosedStream(center.getX() - radius, center.getY() - radius, center.getZ() - radius, center.getX() + radius, center.getY() + radius, center.getZ() + radius); // Paper - simplify/inline
} }
public static Stream<SectionPos> aroundChunk(ChunkPos center, int radius, int minY, int maxY) { - public static Stream<SectionPos> aroundChunk(ChunkPos chunkPos, int x, int y, int z) {
- int i = center.x; + public static Stream<SectionPos> aroundChunk(ChunkPos chunkPos, int radius, int minY, int maxY) { // Paper - fix params
- int j = center.z; int i = chunkPos.x;
- return betweenClosedStream(i - radius, minY, j - radius, i + radius, maxY, j + radius); int i1 = chunkPos.z;
+ return betweenClosedStream(center.x - radius, minY, center.z - radius, center.x + radius, maxY, center.z + radius); // Paper - simplify/inline - return betweenClosedStream(i - x, y, i1 - x, i + x, z, i1 + x);
+ return betweenClosedStream(i - radius, minY, i1 - radius, i + radius, maxY, i1 + radius); // Paper - simplify/inline
} }
public static Stream<SectionPos> betweenClosedStream(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { public static Stream<SectionPos> betweenClosedStream(final int x1, final int y1, final int z1, final int x2, final int y2, final int z2) {

View file

@ -15,100 +15,104 @@ Optimize collection by creating a list instead of a set of the key and value.
This lets us get faster foreach iteration, as well as avoids map lookups on This lets us get faster foreach iteration, as well as avoids map lookups on
the values when needed. the values when needed.
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 diff --git a/net/minecraft/world/level/pathfinder/PathFinder.java b/net/minecraft/world/level/pathfinder/PathFinder.java
--- a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java index a6ef296f7af1f784e1f0772947a6cb3519a3bc2a..81de6c1bbef1cafd3036e736dd305fbedc8368c6 100644
+++ b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java --- a/net/minecraft/world/level/pathfinder/PathFinder.java
@@ -0,0 +0,0 @@ public class PathFinder { +++ b/net/minecraft/world/level/pathfinder/PathFinder.java
if (node == null) { @@ -43,8 +43,12 @@ public class PathFinder {
if (start == null) {
return null; return null;
} else { } else {
- Map<Target, BlockPos> map = positions.stream() - Map<Target, BlockPos> map = targetPositions.stream()
- .collect(Collectors.toMap(pos -> this.nodeEvaluator.getTarget((double)pos.getX(), (double)pos.getY(), (double)pos.getZ()), Function.identity())); - .collect(Collectors.toMap(pos -> this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), Function.identity()));
+ // Paper start - Perf: remove streams and optimize collection + // Paper start - Perf: remove streams and optimize collection
+ List<Map.Entry<Target, BlockPos>> map = Lists.newArrayList(); + List<Map.Entry<Target, BlockPos>> map = Lists.newArrayList();
+ for (final BlockPos pos : positions) { + for (BlockPos pos : targetPositions) {
+ map.add(new java.util.AbstractMap.SimpleEntry<>(this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), pos)); + map.add(new java.util.AbstractMap.SimpleEntry<>(this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), pos));
+ } + }
+ // Paper end - Perf: remove streams and optimize collection + // Paper end - Perf: remove streams and optimize collection
Path path = this.findPath(node, map, followRange, distance, rangeMultiplier); Path path = this.findPath(start, map, maxRange, accuracy, searchDepthMultiplier);
this.nodeEvaluator.done(); this.nodeEvaluator.done();
return path; return path;
@@ -0,0 +0,0 @@ public class PathFinder { @@ -52,19 +56,19 @@ public class PathFinder {
} }
@Nullable @Nullable
- private Path findPath(Node startNode, Map<Target, BlockPos> positions, float followRange, int distance, float rangeMultiplier) { - private Path findPath(Node node, Map<Target, BlockPos> targetPositions, float maxRange, int accuracy, float searchDepthMultiplier) {
+ // Paper start - Perf: remove streams and optimize collection + private Path findPath(Node node, List<Map.Entry<Target, BlockPos>> positions, float maxRange, int accuracy, float searchDepthMultiplier) { // Paper - optimize collection
+ private Path findPath(Node startNode, List<Map.Entry<Target, BlockPos>> positions, float followRange, int distance, float rangeMultiplier) {
ProfilerFiller profilerFiller = Profiler.get(); ProfilerFiller profilerFiller = Profiler.get();
profilerFiller.push("find_path"); profilerFiller.push("find_path");
profilerFiller.markForCharting(MetricCategory.PATH_FINDING); profilerFiller.markForCharting(MetricCategory.PATH_FINDING);
- Set<Target> set = positions.keySet(); - Set<Target> set = targetPositions.keySet();
+ // Set<Target> set = positions.keySet(); + // Set<Target> set = targetPositions.keySet(); // Paper
startNode.g = 0.0F; node.g = 0.0F;
- startNode.h = this.getBestH(startNode, set); - node.h = this.getBestH(node, set);
+ startNode.h = this.getBestH(startNode, positions); // Paper - optimize collection + node.h = this.getBestH(node, positions); // Paper - optimize collection
startNode.f = startNode.h; node.f = node.h;
this.openSet.clear(); this.openSet.clear();
this.openSet.insert(startNode); this.openSet.insert(node);
- Set<Node> set2 = ImmutableSet.of(); - Set<Node> set1 = ImmutableSet.of();
+ // Set<Node> set2 = ImmutableSet.of(); // Paper - unused - diff on change + // Set<Node> set1 = ImmutableSet.of(); // Paper - unused - diff on change
int i = 0; int i = 0;
- Set<Target> set3 = Sets.newHashSetWithExpectedSize(set.size()); - Set<Target> set2 = Sets.newHashSetWithExpectedSize(set.size());
+ List<Map.Entry<Target, BlockPos>> entryList = Lists.newArrayListWithExpectedSize(positions.size()); // Paper - optimize collection + List<Map.Entry<Target, BlockPos>> entryList = Lists.newArrayListWithExpectedSize(positions.size()); // Paper - optimize collection
int j = (int)((float)this.maxVisitedNodes * rangeMultiplier); int i1 = (int)(this.maxVisitedNodes * searchDepthMultiplier);
while (!this.openSet.isEmpty()) { while (!this.openSet.isEmpty()) {
@@ -0,0 +0,0 @@ public class PathFinder { @@ -75,14 +79,18 @@ public class PathFinder {
Node node = this.openSet.pop(); Node node1 = this.openSet.pop();
node.closed = true; node1.closed = true;
- for (Target target : set) { - for (Target target : set) {
+ // Paper start - optimize collection + // Paper start - optimize collection
+ for (int i1 = 0; i1 < positions.size(); i1++) { + for (int positionIndex = 0, size = positions.size(); positionIndex < size; positionIndex++) {
+ final Map.Entry<Target, BlockPos> entry = positions.get(i1); + final Map.Entry<Target, BlockPos> entry = positions.get(positionIndex);
+ Target target = entry.getKey(); + Target target = entry.getKey();
if (node.distanceManhattan(target) <= (float)distance) { if (node1.distanceManhattan(target) <= accuracy) {
target.setReached(); target.setReached();
- set3.add(target); - set2.add(target);
+ entryList.add(entry); + entryList.add(entry);
+ // Paper end - Perf: remove streams and optimize collection + // Paper end - Perf: remove streams and optimize collection
} }
} }
- if (!set3.isEmpty()) { - if (!set2.isEmpty()) {
+ if (!entryList.isEmpty()) { // Paper - Perf: remove streams and optimize collection; rename + if (!entryList.isEmpty()) { // Paper - Perf: remove streams and optimize collection; rename
break; break;
} }
@@ -0,0 +0,0 @@ public class PathFinder { @@ -97,7 +105,7 @@ public class PathFinder {
if (node2.walkedDistance < followRange && (!node2.inOpenSet() || g < node2.g)) { if (node2.walkedDistance < maxRange && (!node2.inOpenSet() || f1 < node2.g)) {
node2.cameFrom = node; node2.cameFrom = node1;
node2.g = g; node2.g = f1;
- node2.h = this.getBestH(node2, set) * 1.5F; - node2.h = this.getBestH(node2, set) * 1.5F;
+ node2.h = this.getBestH(node2, positions) * 1.5F; // Paper - Perf: remove streams and optimize collection + node2.h = this.getBestH(node2, positions) * 1.5F; // Paper - Perf: remove streams and optimize collection
if (node2.inOpenSet()) { if (node2.inOpenSet()) {
this.openSet.changeCost(node2, node2.g + node2.h); this.openSet.changeCost(node2, node2.g + node2.h);
} else { } else {
@@ -0,0 +0,0 @@ public class PathFinder { @@ -109,25 +117,34 @@ public class PathFinder {
} }
} }
- Optional<Path> optional = !set3.isEmpty() - Optional<Path> optional = !set2.isEmpty()
- ? set3.stream().map(node -> this.reconstructPath(node.getBestNode(), positions.get(node), true)).min(Comparator.comparingInt(Path::getNodeCount)) - ? set2.stream()
- .map(pathfinder -> this.reconstructPath(pathfinder.getBestNode(), targetPositions.get(pathfinder), true))
- .min(Comparator.comparingInt(Path::getNodeCount))
- : set.stream() - : set.stream()
- .map(targetx -> this.reconstructPath(targetx.getBestNode(), positions.get(targetx), false)) - .map(pathfinder -> this.reconstructPath(pathfinder.getBestNode(), targetPositions.get(pathfinder), false))
- .min(Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount)); - .min(Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount));
+ // Paper start - Perf: remove streams and optimize collection + // Paper start - Perf: remove streams and optimize collection
+ Path best = null; + Path best = null;
+ boolean entryListIsEmpty = entryList.isEmpty(); + boolean entryListIsEmpty = entryList.isEmpty();
+ Comparator<Path> comparator = entryListIsEmpty ? Comparator.comparingInt(Path::getNodeCount) + Comparator<Path> comparator = entryListIsEmpty
+ ? Comparator.comparingInt(Path::getNodeCount)
+ : Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount); + : Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount);
+ for (Map.Entry<Target, BlockPos> entry : entryListIsEmpty ? positions : entryList) { + for (Map.Entry<Target, BlockPos> entry : entryListIsEmpty ? positions : entryList) {
+ Path path = this.reconstructPath(entry.getKey().getBestNode(), entry.getValue(), !entryListIsEmpty); + Path path = this.reconstructPath(entry.getKey().getBestNode(), entry.getValue(), !entryListIsEmpty);
+ if (best == null || comparator.compare(path, best) < 0) + if (best == null || comparator.compare(path, best) < 0) {
+ best = path; + best = path;
+ }
+ } + }
profilerFiller.pop(); profilerFiller.pop();
- return optional.isEmpty() ? null : optional.get(); - return optional.isEmpty() ? null : optional.get();
@ -116,8 +120,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper end - Perf: remove streams and optimize collection + // Paper end - Perf: remove streams and optimize collection
} }
protected float distance(Node a, Node b) { protected float distance(Node first, Node second) {
return a.distanceTo(b); return first.distanceTo(second);
} }
- private float getBestH(Node node, Set<Target> targets) { - private float getBestH(Node node, Set<Target> targets) {
@ -129,6 +133,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ for (int i = 0, targetsSize = targets.size(); i < targetsSize; i++) { + for (int i = 0, targetsSize = targets.size(); i < targetsSize; i++) {
+ final Target target = targets.get(i).getKey(); + final Target target = targets.get(i).getKey();
+ // Paper end - Perf: remove streams and optimize collection + // Paper end - Perf: remove streams and optimize collection
float g = node.distanceTo(target); float f1 = node.distanceTo(target);
target.updateBest(g, node); target.updateBest(f1, node);
f = Math.min(g, f); f = Math.min(f1, f);