Skip to content

Commit

Permalink
Replace raw long counting with a Long2LongMap accumulator
Browse files Browse the repository at this point in the history
Attempts to fix fluid using 1 Bucket = 81k items: AppliedEnergistics#7321
I chose to use a Long2LongMap accumulator so that different AEKeyTypes
	with the same denominator could be summed in the same map slot,
	and avoid the internal-rounding issues that would be had if I
	divided all the values by their Unit denominators.
  • Loading branch information
cplir-c committed Jul 18, 2024
1 parent 868a983 commit 920830e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/appeng/crafting/execution/ExecutingCraftingJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;

import it.unimi.dsi.fastutil.longs.Long2LongArrayMap;

import appeng.api.config.Actionable;
import appeng.api.crafting.IPatternDetails;
import appeng.api.crafting.PatternDetailsHelper;
Expand Down Expand Up @@ -69,19 +71,29 @@ interface CraftingDifferenceListener {
this.remainingAmount = this.finalOutput.amount();
this.waitingFor = new ListCraftingInventory(postCraftingDifference::onCraftingDifference);

// Fill waiting for and tasks
long totalPending = 0;
// Fill `waitingFor` and `tasks`
// Count the number of pending units, by unit size
Long2LongArrayMap pendingUnits = new Long2LongArrayMap();
for (var entry : plan.emittedItems()) {
waitingFor.insert(entry.getKey(), entry.getLongValue(), Actionable.MODULATE);
totalPending += entry.getLongValue();

long unitKey = entry.getKey().getAmountPerUnit();
// pendingUnits.entry(unitKey).orDefault().value += entry.value;
pendingUnits.mergeLong(unitKey, entry.getLongValue(), (long l, long r) -> l + r);
}
for (var entry : plan.patternTimes().entrySet()) {
tasks.computeIfAbsent(entry.getKey(), p -> new TaskProgress()).value += entry.getValue();
for (var output : entry.getKey().getOutputs()) {
totalPending += output.amount() * entry.getValue();
long additional = output.amount() * entry.getValue();
// pendingUnits.entry(unitKey).orDefault().value += entry.value;
pendingUnits.mergeLong(unitKey, additional, (long l, long r) -> l + r);
}
}
this.timeTracker = new ElapsedTimeTracker(totalPending);
long totalPendingUnits = 0;
for (var entry : pending.entrySet()) {
totalPendingUnits += entry.getLongValue() / entry.getLongKey();
}
this.timeTracker = new ElapsedTimeTracker(totalPendingUnits);
this.link = link;
this.playerId = playerId;
}
Expand Down

0 comments on commit 920830e

Please sign in to comment.