Skip to content

Commit

Permalink
use fast iterator on transits to avoid extra allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Sep 1, 2024
1 parent ff4a4ce commit ecd6fa7
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,7 @@

public abstract class LogisticalTransporterBase extends Transmitter<IItemHandler, InventoryNetwork, LogisticalTransporterBase> {

protected final Int2ObjectMap<TransporterStack> transit = new Int2ObjectOpenHashMap<>();
protected final Int2ObjectOpenHashMap<TransporterStack> transit = new Int2ObjectOpenHashMap<>();
protected final Int2ObjectMap<TransporterStack> needsSync = new Int2ObjectOpenHashMap<>();
public final TransporterTier tier;
protected int nextId = 0;
Expand Down Expand Up @@ -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<TransporterStack> entry : transit.int2ObjectEntrySet()) {
int stackId = entry.getIntKey();
TransporterStack stack = entry.getValue();
for (ObjectIterator<Int2ObjectMap.Entry<TransporterStack>> iterator = transit.int2ObjectEntrySet().fastIterator(); iterator.hasNext(); ) {
int stackId;
TransporterStack stack;
{
Int2ObjectMap.Entry<TransporterStack> 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);
Expand Down

0 comments on commit ecd6fa7

Please sign in to comment.