From ecd6fa7e77423f444f66dfb208d8c7cbfb6ef3de Mon Sep 17 00:00:00 2001 From: Thiakil Date: Sun, 1 Sep 2024 12:58:05 +0800 Subject: [PATCH] use fast iterator on transits to avoid extra allocations --- .../transmitter/LogisticalTransporterBase.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java b/src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java index 36984a1bcca..ba156296af2 100644 --- a/src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java +++ b/src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java @@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.objects.ObjectIterator; import java.util.Collection; import java.util.Collections; import java.util.PrimitiveIterator.OfInt; @@ -46,7 +47,7 @@ public abstract class LogisticalTransporterBase extends Transmitter { - protected final Int2ObjectMap transit = new Int2ObjectOpenHashMap<>(); + protected final Int2ObjectOpenHashMap transit = new Int2ObjectOpenHashMap<>(); protected final Int2ObjectMap needsSync = new Int2ObjectOpenHashMap<>(); public final TransporterTier tier; protected int nextId = 0; @@ -152,9 +153,14 @@ public void onUpdateServer() { //Note: Our calls to getTileEntity are not done with a chunkMap as we don't tend to have that many tiles we // are checking at once from here and given this gets called each tick, it would cause unnecessary garbage // collection to occur actually causing the tick time to go up slightly. - for (Int2ObjectMap.Entry entry : transit.int2ObjectEntrySet()) { - int stackId = entry.getIntKey(); - TransporterStack stack = entry.getValue(); + for (ObjectIterator> iterator = transit.int2ObjectEntrySet().fastIterator(); iterator.hasNext(); ) { + int stackId; + TransporterStack stack; + { + Int2ObjectMap.Entry entry = iterator.next();//don't store it anywhere + stackId = entry.getIntKey(); + stack = entry.getValue(); + } if (!stack.initiatedPath) {//Initiate any paths and remove things that can't go places if (stack.itemStack.isEmpty() || !recalculate(stackId, stack, Long.MAX_VALUE)) { deletes.add(stackId);