Skip to content

Commit

Permalink
Optimization: get rid of HashSet allocation in updateOutputSets()
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Oct 12, 2024
1 parent 8d33f46 commit ae4688c
Showing 1 changed file with 13 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,50 +197,32 @@ public static String getVJoyArchFolderName() {

static <T> void updateOutputSets(final Set<T> sourceSet, final Set<T> oldDownSet, final Set<T> newUpSet,
final Set<T> newDownSet, final boolean keepStillDown) {
final var stillDownSet = new HashSet<T>();

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);
}

Expand Down

0 comments on commit ae4688c

Please sign in to comment.