@@ -16,7 +16,7 @@ This lets us get faster foreach iteration, as well as avoids map lookups on
1616the values when needed.
1717
1818diff --git a/net/minecraft/world/level/pathfinder/PathFinder.java b/net/minecraft/world/level/pathfinder/PathFinder.java
19- index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d718893d0 100644
19+ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..5894260481ea395c26a5b506e38530f7b2032b33 100644
2020--- a/net/minecraft/world/level/pathfinder/PathFinder.java
2121+++ b/net/minecraft/world/level/pathfinder/PathFinder.java
2222@@ -48,8 +48,12 @@ public class PathFinder {
@@ -52,15 +52,14 @@ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d
5252 this.openSet.clear();
5353 this.openSet.insert(node);
5454 boolean asBoolean = this.captureDebug.getAsBoolean();
55- - Set<Node> set1 = asBoolean ? new HashSet<>() : Set.of();
56- + // Set<Node> set1 = asBoolean ? new HashSet<>() : Set.of(); // Paper - unused debug
55+ Set<Node> set1 = asBoolean ? new HashSet<>() : Set.of();
5756 int i = 0;
5857- Set<Target> set2 = Sets.newHashSetWithExpectedSize(set.size());
5958+ List<Map.Entry<Target, BlockPos>> entryList = Lists.newArrayListWithExpectedSize(positions.size()); // Paper - optimize collection
6059 int i1 = (int)(this.maxVisitedNodes * maxVisitedNodesMultiplier);
6160
6261 while (!this.openSet.isEmpty()) {
63- @@ -81,20 +85,21 @@ public class PathFinder {
62+ @@ -81,14 +85,18 @@ public class PathFinder {
6463 Node node1 = this.openSet.pop();
6564 node1.closed = true;
6665
@@ -82,13 +81,7 @@ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d
8281 break;
8382 }
8483
85- - if (asBoolean) {
86- - set1.add(node1);
87- - }
88-
89- if (!(node1.distanceTo(node) >= maxRange)) {
90- int neighbors = this.nodeEvaluator.getNeighbors(this.neighbors, node1);
91- @@ -107,7 +112,7 @@ public class PathFinder {
84+ @@ -107,7 +115,7 @@ public class PathFinder {
9285 if (node2.walkedDistance < maxRange && (!node2.inOpenSet() || f1 < node2.g)) {
9386 node2.cameFrom = node1;
9487 node2.g = f1;
@@ -97,7 +90,7 @@ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d
9790 if (node2.inOpenSet()) {
9891 this.openSet.changeCost(node2, node2.g + node2.h);
9992 } else {
100- @@ -119,34 +124,34 @@ public class PathFinder {
93+ @@ -119,34 +127,41 @@ public class PathFinder {
10194 }
10295 }
10396
@@ -108,13 +101,6 @@ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d
108101- : set.stream()
109102- .map(pathfinder -> this.reconstructPath(pathfinder.getBestNode(), targets.get(pathfinder), false))
110103- .min(Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount));
111- - profilerFiller.pop();
112- - if (optional.isEmpty()) {
113- - return null;
114- - } else {
115- - Path path = optional.get();
116- - if (asBoolean) {
117- - path.setDebug(this.openSet.getHeap(), set1.toArray(Node[]::new), set);
118104+ // Paper start - Perf: remove streams and optimize collection
119105+ Path best = null;
120106+ boolean entryListIsEmpty = entryList.isEmpty();
@@ -125,11 +111,24 @@ index 1a9d8e6dd55ce30aca1c65b32417d61fd0a10f84..f90c2bb95e304bd6b9b8539d89f38d2d
125111+ Path path = this.reconstructPath(entry.getKey().getBestNode(), entry.getValue(), !entryListIsEmpty);
126112+ if (best == null || comparator.compare(path, best) < 0) {
127113+ best = path;
114+ + }
115+ + }
116+ profilerFiller.pop();
117+ - if (optional.isEmpty()) {
118+ - return null;
119+ - } else {
120+ - Path path = optional.get();
121+ - if (asBoolean) {
122+ - path.setDebug(this.openSet.getHeap(), set1.toArray(Node[]::new), set);
123+ + if(asBoolean && best != null) {
124+ + Set<Target> set = Sets.newHashSet();
125+ + for(Map.Entry<Target, BlockPos> entry : positions) {
126+ + set.add(entry.getKey());
128127 }
129128-
130129- return path;
130+ + best.setDebug(this.openSet.getHeap(), set1.toArray(Node[]::new), set);
131131 }
132- + profilerFiller.pop();
133132+ return best;
134133+ // Paper end - Perf: remove streams and optimize collection
135134 }
0 commit comments