From cebdd76ca7fbd86b82f40852b207b2c6883f1b2d Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 29 Aug 2018 13:21:54 -0700 Subject: [PATCH] node map performance, fixes #107 --- .../pathing/calc/AbstractNodeCostSearch.java | 16 +++++++++++----- .../baritone/utils/pathing/BetterBlockPos.java | 12 ++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 18b8050f..b8f13f1e 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -20,10 +20,9 @@ package baritone.pathing.calc; import baritone.pathing.goals.Goal; import baritone.pathing.path.IPath; import baritone.utils.pathing.BetterBlockPos; +import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import net.minecraft.util.math.BlockPos; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; /** @@ -42,7 +41,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { protected final Goal goal; - protected final Map map; + private final Long2ObjectOpenHashMap map; // see issue #107 protected PathNode startNode; @@ -69,7 +68,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { AbstractNodeCostSearch(BlockPos start, Goal goal) { this.start = new BetterBlockPos(start.getX(), start.getY(), start.getZ()); this.goal = goal; - this.map = new HashMap<>(); + this.map = new Long2ObjectOpenHashMap<>(); } public void cancel() { @@ -112,7 +111,14 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { * @return The associated node */ protected PathNode getNodeAtPosition(BetterBlockPos pos) { - return map.computeIfAbsent(pos, p -> new PathNode(p, goal)); + // see issue #107 + long hashCode = pos.hashCode; + PathNode node = map.get(hashCode); + if (node == null) { + node = new PathNode(pos, goal); + map.put(hashCode, node); + } + return node; } @Override diff --git a/src/main/java/baritone/utils/pathing/BetterBlockPos.java b/src/main/java/baritone/utils/pathing/BetterBlockPos.java index 8518f668..6e4fc599 100644 --- a/src/main/java/baritone/utils/pathing/BetterBlockPos.java +++ b/src/main/java/baritone/utils/pathing/BetterBlockPos.java @@ -30,7 +30,7 @@ public class BetterBlockPos extends BlockPos { public final int x; public final int y; public final int z; - private final int hashCode; + public final long hashCode; public BetterBlockPos(int x, int y, int z) { super(x, y, z); @@ -48,10 +48,10 @@ public class BetterBlockPos extends BlockPos { * * That's why we grab out the X, Y, Z and calculate our own hashcode */ - int hash = 3241; - hash = 3457689 * hash + x; - hash = 8734625 * hash + y; - hash = 2873465 * hash + z; + long hash = 3241; + hash = 3457689L * hash + x; + hash = 8734625L * hash + y; + hash = 2873465L * hash + z; this.hashCode = hash; } @@ -61,7 +61,7 @@ public class BetterBlockPos extends BlockPos { @Override public final int hashCode() { - return hashCode; + return (int) hashCode; } @Override