From 04886489266fdbade25aa45d27340474cfcd4004 Mon Sep 17 00:00:00 2001 From: ah-OOG-ah <75745146+ah-OOG-ah@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:08:43 -0500 Subject: [PATCH] Replace linked lists in LSC averaging I really don't know why they were there. --- .../MTELapotronicSuperCapacitor.java | 102 ++++++------------ 1 file changed, 31 insertions(+), 71 deletions(-) diff --git a/src/main/java/kekztech/common/tileentities/MTELapotronicSuperCapacitor.java b/src/main/java/kekztech/common/tileentities/MTELapotronicSuperCapacitor.java index b4e63059931..a8682041377 100644 --- a/src/main/java/kekztech/common/tileentities/MTELapotronicSuperCapacitor.java +++ b/src/main/java/kekztech/common/tileentities/MTELapotronicSuperCapacitor.java @@ -26,6 +26,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; +import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.UUID; @@ -106,14 +107,16 @@ private enum TopState { private int counter = 1; private boolean balanced = false; - private final Queue energyInputValues = new LinkedList<>(); - private final Queue energyOutputValues = new LinkedList<>(); - - private final Queue energyInputValues5m = new LinkedList<>(); - private final Queue energyOutputValues5m = new LinkedList<>(); - - private final Queue energyInputValues1h = new LinkedList<>(); - private final Queue energyOutputValues1h = new LinkedList<>(); + // Holds one hour of ticks + private final long[] energyInput = new long[60 * 60 * 20]; + private final long[] energyOutput = new long[60 * 60 * 20]; + private int bufferPos = 0; + private long averageInput1m = 0; + private long averageOutput1m = 0; + private long averageInput5m = 0; + private long averageOutput5m = 0; + private long averageInput1h = 0; + private long averageOutput1h = 0; private final long max_passive_drain_eu_per_tick_per_uhv_cap = 1_000_000; private final long max_passive_drain_eu_per_tick_per_uev_cap = 100_000_000; @@ -788,41 +791,22 @@ public boolean onRunningTick(ItemStack stack) { tBMTE.injectEnergyUnits(ForgeDirection.UNKNOWN, inputLastTick, 1L); tBMTE.drainEnergyUnits(ForgeDirection.UNKNOWN, outputLastTick, 1L); - // Add I/O values to Queues - if (energyInputValues.size() > DURATION_AVERAGE_TICKS) { - energyInputValues.remove(); - } - energyInputValues.offer(inputLastTick); - - if (energyOutputValues.size() > DURATION_AVERAGE_TICKS) { - energyOutputValues.remove(); - } - - energyOutputValues.offer(outputLastTick); - - // Add I/O values to Queues 5 min - if (energyInputValues5m.size() > 6000) { - energyInputValues5m.remove(); - } - energyInputValues5m.offer(inputLastTick); - - if (energyOutputValues5m.size() > 6000) { - energyOutputValues5m.remove(); - } - - energyOutputValues5m.offer(outputLastTick); + // Pull off oldest I/O values + final long droppedInput = energyInput[bufferPos]; + final long droppedOutput = energyOutput[bufferPos]; - // Add I/O values to Queues 1 hour - if (energyInputValues1h.size() > 72000) { - energyInputValues1h.remove(); - } - energyInputValues1h.offer(inputLastTick); + // Update running counters + averageInput1m -= droppedInput / 20 * 60; + averageInput5m -= droppedInput / 20 * 60 * 5; + averageInput1h -= droppedInput / 20 * 60 * 60; + averageOutput1m -= droppedOutput / 20 * 60; + averageOutput5m -= droppedOutput / 20 * 60 * 5; + averageOutput1h -= droppedOutput / 20 * 60 * 60; - if (energyOutputValues1h.size() > 72000) { - energyOutputValues1h.remove(); - } - - energyOutputValues1h.offer(outputLastTick); + // Insert values and bump the head + energyInput[bufferPos] = inputLastTick; + energyOutput[bufferPos] = outputLastTick; + bufferPos++; return true; } @@ -924,51 +908,27 @@ private long getPowerToPush(long hatchWatts) { } private long getAvgIn() { - long sum = 0L; - for (long l : energyInputValues) { - sum += l; - } - return sum / Math.max(energyInputValues.size(), 1); + return averageInput1m; } private long getAvgOut() { - long sum = 0L; - for (long l : energyOutputValues) { - sum += l; - } - return sum / Math.max(energyOutputValues.size(), 1); + return averageOutput1m; } private long getAvgIn5m() { - double sum = 0; - for (long l : energyInputValues5m) { - sum += l; - } - return (long) sum / Math.max(energyInputValues5m.size(), 1); + return averageInput5m; } private long getAvgOut5m() { - double sum = 0; - for (long l : energyOutputValues5m) { - sum += l; - } - return (long) sum / Math.max(energyOutputValues5m.size(), 1); + return averageOutput5m; } private long getAvgIn1h() { - double sum = 0; - for (long l : energyInputValues1h) { - sum += l; - } - return (long) sum / Math.max(energyInputValues1h.size(), 1); + return averageInput1h; } private long getAvgOut1h() { - double sum = 0; - for (long l : energyOutputValues1h) { - sum += l; - } - return (long) sum / Math.max(energyOutputValues1h.size(), 1); + return averageOutput1h; } private String getTimeTo() {