From 48d66c7e5575d11b13c0a08cb524669a51bb4c5f Mon Sep 17 00:00:00 2001 From: Snownee Date: Sun, 13 Dec 2020 08:19:28 +0800 Subject: [PATCH] Slightly improve redstone link performance --- .../logistics/RedstoneLinkNetworkHandler.java | 44 +++++++++++-------- .../behaviour/linked/LinkBehaviour.java | 12 ++--- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java b/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java index 17fa4c15e..a2e2fdfef 100644 --- a/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java +++ b/src/main/java/com/simibubi/create/content/logistics/RedstoneLinkNetworkHandler.java @@ -1,8 +1,9 @@ package com.simibubi.create.content.logistics; import java.util.HashMap; -import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -20,14 +21,24 @@ import net.minecraft.world.World; public class RedstoneLinkNetworkHandler { - static Map, Set>> connections = new HashMap<>(); + static final Map, Set>> connections = new IdentityHashMap<>(); public static class Frequency { + public static final Frequency EMPTY = new Frequency(ItemStack.EMPTY); + private static final Map simpleFrequencies = new IdentityHashMap<>(); private ItemStack stack; private Item item; private int color; - public Frequency(ItemStack stack) { + public static Frequency of(ItemStack stack) { + if (stack.isEmpty()) + return EMPTY; + if (!stack.hasTag()) + return simpleFrequencies.computeIfAbsent(stack.getItem(), $ -> new Frequency(stack)); + return new Frequency(stack); + } + + private Frequency(ItemStack stack) { this.stack = stack; item = stack.getItem(); CompoundNBT displayTag = stack.getChildTag("display"); @@ -35,7 +46,7 @@ public class RedstoneLinkNetworkHandler { } public ItemStack getStack() { - return stack.copy(); + return stack; } @Override @@ -45,6 +56,8 @@ public class RedstoneLinkNetworkHandler { @Override public boolean equals(Object obj) { + if (this == obj) + return true; return obj instanceof Frequency ? ((Frequency) obj).item == item && ((Frequency) obj).color == color : false; } @@ -65,7 +78,7 @@ public class RedstoneLinkNetworkHandler { Map, Set> networksInWorld = networksIn(actor.getWorld()); Pair key = actor.getNetworkKey(); if (!networksInWorld.containsKey(key)) - networksInWorld.put(key, new HashSet<>()); + networksInWorld.put(key, new LinkedHashSet<>()); return networksInWorld.get(key); } @@ -105,31 +118,26 @@ public class RedstoneLinkNetworkHandler { } if (!withinRange(actor, other)) continue; - power = Math.max(other.getTransmittedStrength(), power); - if (power == 15) - break; + + if (power < 15) + power = Math.max(other.getTransmittedStrength(), power); } // fix one-to-one loading order problem - if(actor.isListening()){ + if (actor.isListening()) { actor.newPosition = true; actor.updateReceiver(power); } - for (Iterator iterator = network.iterator(); iterator.hasNext();) { - LinkBehaviour other = iterator.next(); - if (other.tileEntity.isRemoved()) { - iterator.remove(); - continue; - } - if (!withinRange(actor, other)) - continue; - if (other.isListening()) + for (LinkBehaviour other : network) { + if (other != actor && other.isListening() && withinRange(actor, other)) other.updateReceiver(power); } } public static boolean withinRange(LinkBehaviour from, LinkBehaviour to) { + if (from == to) + return true; return from.getPos().withinDistance(to.getPos(), AllConfigs.SERVER.logistics.linkRange.get()); } diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java index 871c126ce..6246d20ea 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java @@ -39,8 +39,8 @@ public class LinkBehaviour extends TileEntityBehaviour { protected LinkBehaviour(SmartTileEntity te, Pair slots) { super(te); - frequencyFirst = new Frequency(ItemStack.EMPTY); - frequencyLast = new Frequency(ItemStack.EMPTY); + frequencyFirst = Frequency.EMPTY; + frequencyLast = Frequency.EMPTY; firstSlot = slots.getLeft(); secondSlot = slots.getRight(); textShift = Vec3d.ZERO; @@ -133,8 +133,8 @@ public class LinkBehaviour extends TileEntityBehaviour { newPosition = positionInTag != positionKey; super.read(nbt, clientPacket); - frequencyFirst = new Frequency(ItemStack.read(nbt.getCompound("FrequencyFirst"))); - frequencyLast = new Frequency(ItemStack.read(nbt.getCompound("FrequencyLast"))); + frequencyFirst = Frequency.of(ItemStack.read(nbt.getCompound("FrequencyFirst"))); + frequencyLast = Frequency.of(ItemStack.read(nbt.getCompound("FrequencyLast"))); } public void setFrequency(boolean first, ItemStack stack) { @@ -148,9 +148,9 @@ public class LinkBehaviour extends TileEntityBehaviour { getHandler().removeFromNetwork(this); if (first) - frequencyFirst = new Frequency(stack); + frequencyFirst = Frequency.of(stack); else - frequencyLast = new Frequency(stack); + frequencyLast = Frequency.of(stack); if (!changed) return;