From 1e2ae34dbe452a1eebc12019ae632b2279210cad Mon Sep 17 00:00:00 2001 From: Babbaj Date: Tue, 15 Oct 2024 18:25:42 -0400 Subject: [PATCH] crucial performance optimization --- src/main/java/baritone/pathing/calc/Path.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index 0e176d5e1..b749dff64 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -27,6 +27,7 @@ import baritone.pathing.movement.Moves; import baritone.pathing.path.CutoffPath; import baritone.utils.pathing.PathBase; +import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Collections; @@ -77,27 +78,23 @@ class Path extends PathBase { this.goal = goal; this.context = context; PathNode current = end; - LinkedList tempPath = new LinkedList<>(); - LinkedList tempNodes = new LinkedList<>(); - // Repeatedly inserting to the beginning of an arraylist is O(n^2) - // Instead, do it into a linked list, then convert at the end + List tempPath = new ArrayList<>(); + List tempNodes = new ArrayList<>(); while (current != null) { - tempNodes.addFirst(current); - tempPath.addFirst(new BetterBlockPos(current.x, current.y, current.z)); + tempNodes.add(current); + tempPath.add(new BetterBlockPos(current.x, current.y, current.z)); current = current.previous; } + // If the position the player is at is different from the position we told A* to start from if (!realStart.equals(startNodePos)) { PathNode fakeNode = new PathNode(realStart.x, realStart.y, realStart.z, goal); fakeNode.cost = 0; - tempNodes.addFirst(fakeNode); - tempPath.addFirst(realStart); + tempNodes.add(fakeNode); + tempPath.add(realStart); } - - // Can't directly convert from the PathNode pseudo linked list to an array because we don't know how long it is - // inserting into a LinkedList keeps track of length, then when we addall (which calls .toArray) it's able - // to performantly do that conversion since it knows the length. - this.path = new ArrayList<>(tempPath); - this.nodes = new ArrayList<>(tempNodes); + // Nodes are traversed last to first so we need to reverse the list + this.path = Lists.reverse(tempPath); + this.nodes = Lists.reverse(tempNodes); } @Override