Skip to content

Commit

Permalink
Avoid Object2LongMap.Entry allocations (#8362)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
embeddedt and shartte authored Feb 14, 2025
1 parent ea12402 commit 1853dac
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/main/java/appeng/api/stacks/VariantCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.NoSuchElementException;

import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
import it.unimi.dsi.fastutil.objects.Object2LongSortedMap;

import appeng.api.config.FuzzyMode;
Expand Down Expand Up @@ -92,7 +93,7 @@ public boolean isEmpty() {
@Override
public Iterator<Object2LongMap.Entry<AEKey>> iterator() {
if (!dropZeros) {
return getRecords().object2LongEntrySet().iterator();
return Object2LongMaps.fastIterator(getRecords());
}

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

public NonDefaultIterator() {
this.parent = getRecords().object2LongEntrySet().iterator();
this.parent = Object2LongMaps.fastIterator(getRecords());
this.next = seekNext();
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/appeng/me/cells/BasicCellInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.world.item.ItemStack;

import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;

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

@Override
public void getAvailableStacks(KeyCounter out) {
for (var entry : this.getCellItems().object2LongEntrySet()) {
for (var entry : Object2LongMaps.fastIterable(this.getCellItems())) {
out.add(entry.getKey(), entry.getLongValue());
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/appeng/me/service/StorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ private void updateCachedStacks() {
}
}
// Post watcher update for removed stacks
for (var entry : cachedAvailableAmounts.object2LongEntrySet()) {
var what = entry.getKey();
for (var what : cachedAvailableAmounts.keySet()) {
var newAmount = cachedAvailableStacks.get(what);
if (newAmount == 0) {
postWatcherUpdate(what, newAmount);
Expand Down

0 comments on commit 1853dac

Please sign in to comment.