Skip to content

Commit 1853dac

Browse files
embeddedtshartte
andauthored
Avoid Object2LongMap.Entry allocations (#8362)
Small tweaks to reduce the number of entries that get allocated on each server tick when updating the cached stacks. A number of remaining allocations here appear to be from https://github.com/AppliedEnergistics/Applied-Energistics-2/blob/126ff28d93ffc717c9dda8a40503660165e7a4de/src/main/java/appeng/api/stacks/VariantCounter.java#L95. Ideally that should use `Object2LongMaps.fastIterator`, but I am not sure if doing so is safe, since I'm not familiar with the rest of the code. --------- Co-authored-by: shartte <shartte@users.noreply.github.com>
1 parent ea12402 commit 1853dac

3 files changed

Lines changed: 6 additions & 5 deletions

File tree

src/main/java/appeng/api/stacks/VariantCounter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.NoSuchElementException;
66

77
import it.unimi.dsi.fastutil.objects.Object2LongMap;
8+
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
89
import it.unimi.dsi.fastutil.objects.Object2LongSortedMap;
910

1011
import appeng.api.config.FuzzyMode;
@@ -92,7 +93,7 @@ public boolean isEmpty() {
9293
@Override
9394
public Iterator<Object2LongMap.Entry<AEKey>> iterator() {
9495
if (!dropZeros) {
95-
return getRecords().object2LongEntrySet().iterator();
96+
return Object2LongMaps.fastIterator(getRecords());
9697
}
9798

9899
return new NonDefaultIterator();
@@ -141,7 +142,7 @@ private class NonDefaultIterator implements Iterator<Object2LongMap.Entry<AEKey>
141142
private Object2LongMap.Entry<AEKey> next;
142143

143144
public NonDefaultIterator() {
144-
this.parent = getRecords().object2LongEntrySet().iterator();
145+
this.parent = Object2LongMaps.fastIterator(getRecords());
145146
this.next = seekNext();
146147
}
147148

src/main/java/appeng/me/cells/BasicCellInventory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.world.item.ItemStack;
2929

3030
import it.unimi.dsi.fastutil.objects.Object2LongMap;
31+
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
3132
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
3233

3334
import appeng.api.config.Actionable;
@@ -239,7 +240,7 @@ private void loadCellItems() {
239240

240241
@Override
241242
public void getAvailableStacks(KeyCounter out) {
242-
for (var entry : this.getCellItems().object2LongEntrySet()) {
243+
for (var entry : Object2LongMaps.fastIterable(this.getCellItems())) {
243244
out.add(entry.getKey(), entry.getLongValue());
244245
}
245246
}

src/main/java/appeng/me/service/StorageService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ private void updateCachedStacks() {
127127
}
128128
}
129129
// Post watcher update for removed stacks
130-
for (var entry : cachedAvailableAmounts.object2LongEntrySet()) {
131-
var what = entry.getKey();
130+
for (var what : cachedAvailableAmounts.keySet()) {
132131
var newAmount = cachedAvailableStacks.get(what);
133132
if (newAmount == 0) {
134133
postWatcherUpdate(what, newAmount);

0 commit comments

Comments
 (0)