Performance improvement to network scanning, fixed the need to have hashCode/equals implemented on nodes as well

This commit is contained in:
raoulvdberge
2017-05-15 19:02:50 +02:00
parent 75acce0031
commit ec0db805b0
4 changed files with 33 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -71,6 +72,11 @@ public interface INetworkNode {
*/
BlockPos getPos();
/**
* @return the world of this network node
*/
World getWorld();
/**
* Marks this node as dirty for saving.
*/

View File

@@ -23,6 +23,7 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
private TileController controller;
private List<INetworkNode> nodes = new ArrayList<>();
private Set<Integer> nodePositions = new HashSet<>();
public NetworkNodeGraph(TileController controller) {
this.controller = controller;
@@ -39,6 +40,7 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
}
Set<INetworkNode> newNodes = new HashSet<>();
Set<Integer> newNodePositions = new HashSet<>();
Queue<NodeToCheck> toCheck = new ArrayDeque<>();
INetworkNeighborhoodAware.Operator operator = (world, pos, side) -> {
@@ -51,7 +53,7 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
INetworkNodeProxy otherNodeProxy = NETWORK_NODE_PROXY_CAPABILITY.cast(tile.getCapability(NETWORK_NODE_PROXY_CAPABILITY, side));
INetworkNode otherNode = otherNodeProxy.getNode();
if (newNodes.add(otherNode)) {
if (newNodes.add(otherNode) && newNodePositions.add(getNodeHash(otherNode))) {
toCheck.add(new NodeToCheck(otherNode, world, pos, side, tile));
}
}
@@ -72,12 +74,17 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
}
List<INetworkNode> oldNodes = nodes;
Set<Integer> oldNodePositions = nodePositions;
nodes = new ArrayList<>(newNodes);
nodePositions = new HashSet<>(newNodePositions);
boolean changed = false;
for (INetworkNode node : nodes) {
if (!oldNodes.contains(node)) {
if (!oldNodePositions.contains(getNodeHash(node))) {
System.out.println("New node: " + node);
node.onConnected(controller);
changed = true;
@@ -85,7 +92,9 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
}
for (INetworkNode oldNode : oldNodes) {
if (!nodes.contains(oldNode)) {
if (!nodePositions.contains(getNodeHash(oldNode))) {
System.out.println("Removed node: " + oldNode);
oldNode.onDisconnected(controller);
changed = true;
@@ -97,6 +106,12 @@ public class NetworkNodeGraph implements INetworkNodeGraph {
}
}
private static int getNodeHash(INetworkNode node) {
int result = node.getWorld().provider.getDimension();
result = 31 * result + node.getPos().hashCode();
return result;
}
@Override
public List<INetworkNode> all() {
return nodes;

View File

@@ -14,6 +14,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
@@ -149,6 +150,11 @@ public abstract class NetworkNode implements INetworkNode, INetworkNeighborhoodA
return holder.pos();
}
@Override
public World getWorld() {
return holder.world();
}
public boolean canConduct(@Nullable EnumFacing direction) {
return true;
}
@@ -186,18 +192,4 @@ public abstract class NetworkNode implements INetworkNode, INetworkNeighborhoodA
public void setActive(boolean active) {
this.active = active;
}
@Override
public boolean equals(Object o) {
return o instanceof NetworkNode
&& holder.pos().equals(((NetworkNode) o).holder.pos())
&& holder.world().provider.getDimension() == ((NetworkNode) o).holder.world().provider.getDimension();
}
@Override
public int hashCode() {
int result = holder.pos().hashCode();
result = 31 * result + holder.world().provider.getDimension();
return result;
}
}