From 920830ed8c89fd5cd6cf38b9d0db4607f349f863 Mon Sep 17 00:00:00 2001 From: cplir-c <31234188+cplir-c@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:10:46 -0500 Subject: [PATCH] Replace raw long counting with a Long2LongMap accumulator Attempts to fix fluid using 1 Bucket = 81k items: https://github.com/AppliedEnergistics/Applied-Energistics-2/issues/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. --- .../execution/ExecutingCraftingJob.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/appeng/crafting/execution/ExecutingCraftingJob.java b/src/main/java/appeng/crafting/execution/ExecutingCraftingJob.java index ce958e4a4b7..e27a14f4e29 100644 --- a/src/main/java/appeng/crafting/execution/ExecutingCraftingJob.java +++ b/src/main/java/appeng/crafting/execution/ExecutingCraftingJob.java @@ -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; @@ -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; }