Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite findTargets for better performance. #11754

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -44,13 +43,13 @@ public Collection<Unit> getTargetUnits(final Collection<Unit> units) {
*/
public static List<TargetGroup> newTargetGroups(
final Collection<Unit> units, final Collection<Unit> enemyUnits) {

final Set<UnitType> unitTypes = units.stream().map(Unit::getType).collect(Collectors.toSet());
final boolean destroyerPresent = unitTypes.stream().anyMatch(Matches.unitTypeIsDestroyer());
final Set<UnitType> enemyUnitTypes =
enemyUnits.stream().map(Unit::getType).collect(Collectors.toSet());
final List<TargetGroup> targetGroups = new ArrayList<>();
for (final UnitType unitType : unitTypes) {
final Set<UnitType> targets = findTargets(unitType, unitTypes, enemyUnitTypes);
final Set<UnitType> targets = findTargets(unitType, destroyerPresent, enemyUnitTypes);
if (targets.isEmpty()) {
continue;
}
Expand All @@ -65,15 +64,21 @@ public static List<TargetGroup> newTargetGroups(
}

private static Set<UnitType> findTargets(
final UnitType unitType, final Set<UnitType> unitTypes, final Set<UnitType> enemyUnitTypes) {
final Set<UnitType> targets = new HashSet<>(enemyUnitTypes);
targets.removeAll(unitType.getUnitAttachment().getCanNotTarget());
return unitTypes.stream().anyMatch(Matches.unitTypeIsDestroyer())
? targets
: targets.stream()
.filter(
target -> !target.getUnitAttachment().getCanNotBeTargetedBy().contains(unitType))
.collect(Collectors.toSet());
UnitType unitType, boolean destroyerPresent, Set<UnitType> enemyUnitTypes) {
Set<UnitType> cannotTarget = unitType.getUnitAttachment().getCanNotTarget();
// Note: uses a single stream instead of a sequence of removeAll() calls for performance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment will be confusing to those not looking at this diff! =D
(Arguably this is something for the commit history)

return enemyUnitTypes.stream()
.filter(
targetUnitType -> {
if (cannotTarget.contains(targetUnitType)) {
return false;
}
if (destroyerPresent) {
return true;
}
return !targetUnitType.getUnitAttachment().getCanNotBeTargetedBy().contains(unitType);
})
.collect(Collectors.toSet());
}

private static Optional<TargetGroup> findTargetsInTargetGroups(
Expand Down