PaperMC/paper-server/patches/features/0019-Optimize-Pathfinder-Remove-Streams-Optimized-collect.patch

138 lines
7.1 KiB
Diff
Raw Normal View History

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 4 Aug 2020 22:24:15 +0200
Subject: [PATCH] Optimize Pathfinder - Remove Streams / Optimized collections
I utilized the IDE to convert streams to non streams code, so shouldn't
be any risk of behavior change. Only did minor optimization of the
generated code set to remove unnecessary things.
I expect us to just drop this patch on next major update and re-apply
it with the IDE again and re-apply the collections optimization.
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
the values when needed.
2024-12-16 23:46:48 +01:00
diff --git a/net/minecraft/world/level/pathfinder/PathFinder.java b/net/minecraft/world/level/pathfinder/PathFinder.java
index a6ef296f7af1f784e1f0772947a6cb3519a3bc2a..81de6c1bbef1cafd3036e736dd305fbedc8368c6 100644
--- a/net/minecraft/world/level/pathfinder/PathFinder.java
+++ b/net/minecraft/world/level/pathfinder/PathFinder.java
@@ -43,8 +43,12 @@ public class PathFinder {
if (start == null) {
2022-06-08 08:25:32 +02:00
return null;
} else {
2024-12-16 23:46:48 +01:00
- Map<Target, BlockPos> map = targetPositions.stream()
- .collect(Collectors.toMap(pos -> this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), Function.identity()));
+ // Paper start - Perf: remove streams and optimize collection
2022-06-08 08:25:32 +02:00
+ List<Map.Entry<Target, BlockPos>> map = Lists.newArrayList();
2024-12-16 23:46:48 +01:00
+ for (BlockPos pos : targetPositions) {
2024-04-25 13:02:27 +02:00
+ map.add(new java.util.AbstractMap.SimpleEntry<>(this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), pos));
2022-06-08 08:25:32 +02:00
+ }
+ // Paper end - Perf: remove streams and optimize collection
2024-12-16 23:46:48 +01:00
Path path = this.findPath(start, map, maxRange, accuracy, searchDepthMultiplier);
2022-06-08 08:25:32 +02:00
this.nodeEvaluator.done();
return path;
2024-12-16 23:46:48 +01:00
@@ -52,19 +56,19 @@ public class PathFinder {
}
@Nullable
2024-12-16 23:46:48 +01:00
- private Path findPath(Node node, Map<Target, BlockPos> targetPositions, float maxRange, int accuracy, float searchDepthMultiplier) {
+ private Path findPath(Node node, List<Map.Entry<Target, BlockPos>> positions, float maxRange, int accuracy, float searchDepthMultiplier) { // Paper - optimize collection
2024-10-24 22:29:29 +02:00
ProfilerFiller profilerFiller = Profiler.get();
profilerFiller.push("find_path");
profilerFiller.markForCharting(MetricCategory.PATH_FINDING);
2024-12-16 23:46:48 +01:00
- Set<Target> set = targetPositions.keySet();
+ // Set<Target> set = targetPositions.keySet(); // Paper
node.g = 0.0F;
- node.h = this.getBestH(node, set);
+ node.h = this.getBestH(node, positions); // Paper - optimize collection
node.f = node.h;
this.openSet.clear();
2024-12-16 23:46:48 +01:00
this.openSet.insert(node);
- Set<Node> set1 = ImmutableSet.of();
+ // Set<Node> set1 = ImmutableSet.of(); // Paper - unused - diff on change
int i = 0;
2024-12-16 23:46:48 +01:00
- Set<Target> set2 = Sets.newHashSetWithExpectedSize(set.size());
+ List<Map.Entry<Target, BlockPos>> entryList = Lists.newArrayListWithExpectedSize(positions.size()); // Paper - optimize collection
2024-12-16 23:46:48 +01:00
int i1 = (int)(this.maxVisitedNodes * searchDepthMultiplier);
while (!this.openSet.isEmpty()) {
2024-12-16 23:46:48 +01:00
@@ -75,14 +79,18 @@ public class PathFinder {
Node node1 = this.openSet.pop();
node1.closed = true;
- for (Target target : set) {
+ // Paper start - optimize collection
2024-12-16 23:46:48 +01:00
+ for (int positionIndex = 0, size = positions.size(); positionIndex < size; positionIndex++) {
+ final Map.Entry<Target, BlockPos> entry = positions.get(positionIndex);
+ Target target = entry.getKey();
2024-12-16 23:46:48 +01:00
if (node1.distanceManhattan(target) <= accuracy) {
target.setReached();
2024-12-16 23:46:48 +01:00
- set2.add(target);
+ entryList.add(entry);
+ // Paper end - Perf: remove streams and optimize collection
}
}
2024-12-16 23:46:48 +01:00
- if (!set2.isEmpty()) {
+ if (!entryList.isEmpty()) { // Paper - Perf: remove streams and optimize collection; rename
break;
}
2024-12-16 23:46:48 +01:00
@@ -97,7 +105,7 @@ public class PathFinder {
if (node2.walkedDistance < maxRange && (!node2.inOpenSet() || f1 < node2.g)) {
node2.cameFrom = node1;
node2.g = f1;
- node2.h = this.getBestH(node2, set) * 1.5F;
+ node2.h = this.getBestH(node2, positions) * 1.5F; // Paper - Perf: remove streams and optimize collection
if (node2.inOpenSet()) {
this.openSet.changeCost(node2, node2.g + node2.h);
} else {
2024-12-16 23:46:48 +01:00
@@ -109,25 +117,34 @@ public class PathFinder {
}
}
2024-12-16 23:46:48 +01:00
- Optional<Path> optional = !set2.isEmpty()
- ? set2.stream()
- .map(pathfinder -> this.reconstructPath(pathfinder.getBestNode(), targetPositions.get(pathfinder), true))
- .min(Comparator.comparingInt(Path::getNodeCount))
- : set.stream()
2024-12-16 23:46:48 +01:00
- .map(pathfinder -> this.reconstructPath(pathfinder.getBestNode(), targetPositions.get(pathfinder), false))
- .min(Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount));
+ // Paper start - Perf: remove streams and optimize collection
+ Path best = null;
+ boolean entryListIsEmpty = entryList.isEmpty();
2024-12-16 23:46:48 +01:00
+ Comparator<Path> comparator = entryListIsEmpty
+ ? Comparator.comparingInt(Path::getNodeCount)
+ : Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount);
+ for (Map.Entry<Target, BlockPos> entry : entryListIsEmpty ? positions : entryList) {
+ Path path = this.reconstructPath(entry.getKey().getBestNode(), entry.getValue(), !entryListIsEmpty);
2024-12-16 23:46:48 +01:00
+ if (best == null || comparator.compare(path, best) < 0) {
+ best = path;
2024-12-16 23:46:48 +01:00
+ }
+ }
2024-10-24 22:29:29 +02:00
profilerFiller.pop();
- return optional.isEmpty() ? null : optional.get();
+ return best;
+ // Paper end - Perf: remove streams and optimize collection
}
2024-12-16 23:46:48 +01:00
protected float distance(Node first, Node second) {
return first.distanceTo(second);
2022-06-08 08:25:32 +02:00
}
- private float getBestH(Node node, Set<Target> targets) {
+ private float getBestH(Node node, List<Map.Entry<Target, BlockPos>> targets) { // Paper - Perf: remove streams and optimize collection; Set<Target> -> List<Map.Entry<Target, BlockPos>>
float f = Float.MAX_VALUE;
- for (Target target : targets) {
+ // Paper start - Perf: remove streams and optimize collection
+ for (int i = 0, targetsSize = targets.size(); i < targetsSize; i++) {
+ final Target target = targets.get(i).getKey();
+ // Paper end - Perf: remove streams and optimize collection
2024-12-16 23:46:48 +01:00
float f1 = node.distanceTo(target);
target.updateBest(f1, node);
f = Math.min(f1, f);