node map performance, fixes #107

This commit is contained in:
Leijurv 2018-08-29 13:21:54 -07:00
parent 5c5507cc9e
commit cebdd76ca7
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 17 additions and 11 deletions

View File

@ -20,10 +20,9 @@ package baritone.pathing.calc;
import baritone.pathing.goals.Goal; import baritone.pathing.goals.Goal;
import baritone.pathing.path.IPath; import baritone.pathing.path.IPath;
import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.BetterBlockPos;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
/** /**
@ -42,7 +41,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
protected final Goal goal; protected final Goal goal;
protected final Map<BetterBlockPos, PathNode> map; private final Long2ObjectOpenHashMap<PathNode> map; // see issue #107
protected PathNode startNode; protected PathNode startNode;
@ -69,7 +68,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
AbstractNodeCostSearch(BlockPos start, Goal goal) { AbstractNodeCostSearch(BlockPos start, Goal goal) {
this.start = new BetterBlockPos(start.getX(), start.getY(), start.getZ()); this.start = new BetterBlockPos(start.getX(), start.getY(), start.getZ());
this.goal = goal; this.goal = goal;
this.map = new HashMap<>(); this.map = new Long2ObjectOpenHashMap<>();
} }
public void cancel() { public void cancel() {
@ -112,7 +111,14 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
* @return The associated node * @return The associated node
*/ */
protected PathNode getNodeAtPosition(BetterBlockPos pos) { 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 @Override

View File

@ -30,7 +30,7 @@ public class BetterBlockPos extends BlockPos {
public final int x; public final int x;
public final int y; public final int y;
public final int z; public final int z;
private final int hashCode; public final long hashCode;
public BetterBlockPos(int x, int y, int z) { public BetterBlockPos(int x, int y, int z) {
super(x, y, 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 * That's why we grab out the X, Y, Z and calculate our own hashcode
*/ */
int hash = 3241; long hash = 3241;
hash = 3457689 * hash + x; hash = 3457689L * hash + x;
hash = 8734625 * hash + y; hash = 8734625L * hash + y;
hash = 2873465 * hash + z; hash = 2873465L * hash + z;
this.hashCode = hash; this.hashCode = hash;
} }
@ -61,7 +61,7 @@ public class BetterBlockPos extends BlockPos {
@Override @Override
public final int hashCode() { public final int hashCode() {
return hashCode; return (int) hashCode;
} }
@Override @Override