From ae4688c8544e9e533ce098d05311baf78018d373 Mon Sep 17 00:00:00 2001 From: Matteo Hausner Date: Sun, 13 Oct 2024 00:36:34 +0200 Subject: [PATCH] Optimization: get rid of HashSet allocation in updateOutputSets() --- .../runmode/OutputRunMode.java | 44 ++++++------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/src/main/java/de/bwravencl/controllerbuddy/runmode/OutputRunMode.java b/src/main/java/de/bwravencl/controllerbuddy/runmode/OutputRunMode.java index 3d4fd38f..7e03c724 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/runmode/OutputRunMode.java +++ b/src/main/java/de/bwravencl/controllerbuddy/runmode/OutputRunMode.java @@ -197,50 +197,32 @@ public static String getVJoyArchFolderName() { static void updateOutputSets(final Set sourceSet, final Set oldDownSet, final Set newUpSet, final Set newDownSet, final boolean keepStillDown) { - final var stillDownSet = new HashSet(); - newUpSet.clear(); + newDownSet.clear(); - for (final var oldCode : oldDownSet) { - var stillDown = false; - - for (final var newCode : sourceSet) { - if (newCode.equals(oldCode)) { - stillDown = true; - break; - } - } + final var oldDownSetIterator = oldDownSet.iterator(); + while (oldDownSetIterator.hasNext()) { + final var oldDownElement = oldDownSetIterator.next(); + final var stillDown = sourceSet.stream().anyMatch(sourceElement -> sourceElement.equals(oldDownElement)); if (stillDown) { - stillDownSet.add(oldCode); + if (keepStillDown) { + newDownSet.add(oldDownElement); + } } else { - newUpSet.add(oldCode); + newUpSet.add(oldDownElement); + oldDownSetIterator.remove(); } } - newDownSet.clear(); - - if (keepStillDown) { - newDownSet.addAll(stillDownSet); - } - - for (final var newCode : sourceSet) { - var alreadyDown = false; - - for (final var oldCode : oldDownSet) { - if (oldCode.equals(newCode)) { - alreadyDown = true; - break; - } - } + for (final var sourceElement : sourceSet) { + final var alreadyDown = oldDownSet.stream().anyMatch(oldElement -> oldElement.equals(sourceElement)); if (!alreadyDown) { - newDownSet.add(newCode); + newDownSet.add(sourceElement); } } - oldDownSet.clear(); - oldDownSet.addAll(stillDownSet); oldDownSet.addAll(newDownSet); }