switch to LinkedHashMap for 23x performance improvement!

This commit is contained in:
George Paton 2025-07-01 17:44:52 +10:00
parent c08a90553a
commit 25a2172c6b

View File

@ -2,6 +2,7 @@ package com.hbm.uninos;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Map.Entry;
@ -21,16 +22,16 @@ import net.minecraft.world.World;
* @author hbm
*/
public class UniNodespace {
public static HashMap<World, UniNodeWorld> worlds = new HashMap();
public static Set<NodeNet> activeNodeNets = new HashSet();
public static GenNode getNode(World world, int x, int y, int z, INetworkProvider type) {
UniNodeWorld nodeWorld = worlds.get(world);
if(nodeWorld != null) return nodeWorld.nodes.get(new Pair(new BlockPos(x, y, z), type));
return null;
}
public static void createNode(World world, GenNode node) {
UniNodeWorld nodeWorld = worlds.get(world);
if(nodeWorld == null) {
@ -39,21 +40,21 @@ public class UniNodespace {
}
nodeWorld.pushNode(node);
}
public static void destroyNode(World world, int x, int y, int z, INetworkProvider type) {
GenNode node = getNode(world, x, y, z, type);
if(node != null) {
worlds.get(world).popNode(node);
}
}
public static void updateNodespace() {
for(World world : MinecraftServer.getServer().worldServers) {
UniNodeWorld nodeWorld = worlds.get(world);
if(nodeWorld == null) continue;
for(Entry<Pair<BlockPos, INetworkProvider>, GenNode> entry : nodeWorld.nodes.entrySet()) {
GenNode node = entry.getValue();
INetworkProvider provider = entry.getKey().getValue();
@ -63,19 +64,19 @@ public class UniNodespace {
}
}
}
updateNetworks();
}
private static void updateNetworks() {
for(NodeNet net : activeNodeNets) net.resetTrackers(); //reset has to be done before everything else
for(NodeNet net : activeNodeNets) net.update();
}
/** Goes over each connection point of the given node, tries to find neighbor nodes and to join networks with them */
private static void checkNodeConnection(World world, GenNode node, INetworkProvider provider) {
for(DirPos con : node.connections) {
GenNode conNode = getNode(world, con.getX(), con.getY(), con.getZ(), provider); // get whatever neighbor node intersects with that connection
if(conNode != null) { // if there is a node at that place
@ -85,10 +86,10 @@ public class UniNodespace {
}
}
}
if(node.net == null || !node.net.isValid()) provider.provideNetwork().joinLink(node);
}
/** Checks if the node can be connected to given the DirPos, skipSideCheck will ignore the DirPos' direction value */
public static boolean checkConnection(GenNode connectsTo, DirPos connectFrom, boolean skipSideCheck) {
for(DirPos revCon : connectsTo.connections) {
@ -98,10 +99,10 @@ public class UniNodespace {
}
return false;
}
/** Links two nodes with different or potentially no networks */
private static void connectToNode(GenNode origin, GenNode connection) {
if(origin.hasValidNet() && connection.hasValidNet()) { // both nodes have nets, but the nets are different (previous assumption), join networks
if(origin.net.links.size() > connection.net.links.size()) {
origin.net.joinNetworks(connection.net);
@ -114,18 +115,18 @@ public class UniNodespace {
origin.net.joinLink(connection);
}
}
public static class UniNodeWorld {
public HashMap<Pair<BlockPos, INetworkProvider>, GenNode> nodes = new HashMap();
public HashMap<Pair<BlockPos, INetworkProvider>, GenNode> nodes = new LinkedHashMap<>();
/** Adds a node at all its positions to the nodespace */
public void pushNode(GenNode node) {
for(BlockPos pos : node.positions) {
nodes.put(new Pair(pos, node.networkProvider), node);
}
}
/** Removes the specified node from all positions from nodespace */
public void popNode(GenNode node) {
if(node.net != null) node.net.destroy();