From 73c33e01004e076a46e888d969181caed3f7dd67 Mon Sep 17 00:00:00 2001 From: qwertyuioplkjhgfd Date: Fri, 25 Feb 2022 20:51:16 -0800 Subject: [PATCH 1/2] Make round robin better --- .../core/networks/cargo/CargoNetworkTask.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java index edece7dcb..e5e8f3928 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java @@ -168,6 +168,7 @@ class CargoNetworkTask implements Runnable { boolean roundrobin = Objects.equals(cfg.getString("round-robin"), "true"); boolean smartFill = Objects.equals(cfg.getString("smart-fill"), "true"); + int index = 0; Collection destinations; if (roundrobin) { // Use an ArrayDeque to perform round-robin sorting @@ -175,8 +176,9 @@ class CargoNetworkTask implements Runnable { // An ArrayDequeue is preferable as opposed to a LinkedList: // - The number of elements does not change. // - ArrayDequeue has better iterative performance + index = network.roundRobin.getOrDefault(inputNode, 0); Deque tempDestinations = new ArrayDeque<>(outputNodes); - roundRobinSort(inputNode, tempDestinations); + roundRobinSort(index, tempDestinations); destinations = tempDestinations; } else { // Using an ArrayList here since we won't need to sort the destinations @@ -190,8 +192,12 @@ class CargoNetworkTask implements Runnable { if (target.isPresent()) { ItemStackWrapper wrapper = ItemStackWrapper.wrap(item); item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item, wrapper); + index++; if (item == null) { + if (roundrobin) { + network.roundRobin.put(inputNode, index % outputNodes.size()); + } break; } } @@ -204,27 +210,19 @@ class CargoNetworkTask implements Runnable { * This method sorts a given {@link Deque} of output node locations using a semi-accurate * round-robin method. * - * @param inputNode - * The {@link Location} of the input node + * @param index + * The round-robin index of the input node * @param outputNodes * A {@link Deque} of {@link Location Locations} of the output nodes */ - private void roundRobinSort(Location inputNode, Deque outputNodes) { - int index = network.roundRobin.getOrDefault(inputNode, 0); - + private void roundRobinSort(int index, Deque outputNodes) { if (index < outputNodes.size()) { // Not ideal but actually not bad performance-wise over more elegant alternatives for (int i = 0; i < index; i++) { Location temp = outputNodes.removeFirst(); outputNodes.add(temp); } - - index++; - } else { - index = 1; } - - network.roundRobin.put(inputNode, index); } } From d2d4dd54d5ad4b006a127fed5b497e15bb1985cd Mon Sep 17 00:00:00 2001 From: qwertyuioplkjhgfd <45241413+qwertyuioplkjhgfd@users.noreply.github.com> Date: Tue, 15 Mar 2022 22:47:29 -0700 Subject: [PATCH 2/2] comments and fix a mistake --- .../slimefun4/core/networks/cargo/CargoNetworkTask.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java index e5e8f3928..df29a6059 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNetworkTask.java @@ -171,12 +171,14 @@ class CargoNetworkTask implements Runnable { int index = 0; Collection destinations; if (roundrobin) { + // The current round-robin index of the (unsorted) outputNodes list, + // or the index at which to start searching for valid output nodes + index = network.roundRobin.getOrDefault(inputNode, 0); // Use an ArrayDeque to perform round-robin sorting // Since the impl for roundRobinSort just does Deque.addLast(Deque#removeFirst) // An ArrayDequeue is preferable as opposed to a LinkedList: // - The number of elements does not change. // - ArrayDequeue has better iterative performance - index = network.roundRobin.getOrDefault(inputNode, 0); Deque tempDestinations = new ArrayDeque<>(outputNodes); roundRobinSort(index, tempDestinations); destinations = tempDestinations; @@ -192,15 +194,16 @@ class CargoNetworkTask implements Runnable { if (target.isPresent()) { ItemStackWrapper wrapper = ItemStackWrapper.wrap(item); item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item, wrapper); - index++; if (item == null) { if (roundrobin) { - network.roundRobin.put(inputNode, index % outputNodes.size()); + // The output was valid, set the round robin index to the node after this one + network.roundRobin.put(inputNode, (index + 1) % outputNodes.size()); } break; } } + index++; } return item;