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

Reduce use of expensive getTransporting(). #11913

Merged
merged 3 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
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 @@ -385,10 +385,13 @@ private void markTransportsMovement(
final boolean paratroopsLanding =
arrived.stream().anyMatch(paratroopNAirTransports)
&& MoveValidator.allLandUnitsAreBeingParatroopered(arrived);
final Map<Unit, Collection<Unit>> dependentAirTransportableUnits =
new HashMap<>(
MoveValidator.getDependents(
CollectionUtils.getMatches(arrived, Matches.unitCanTransport())));
final Map<Unit, Collection<Unit>> dependentAirTransportableUnits = new HashMap<>();
for (final Unit unit : arrived) {
Unit transport = unit.getTransportedBy();
if (transport != null) {
dependentAirTransportableUnits.computeIfAbsent(transport, u -> new ArrayList<>()).add(unit);
}
}
// add newly created dependents
for (final Entry<Unit, Collection<Unit>> entry : airTransportDependents.entrySet()) {
Collection<Unit> dependents = dependentAirTransportableUnits.get(entry.getKey());
Expand All @@ -414,7 +417,7 @@ private void markTransportsMovement(
// mark transports as having transported
for (final Unit load : transporting.keySet()) {
final Unit transport = transporting.get(load);
if (!transport.getTransporting().contains(load)) {
if (!transport.equals(load.getTransportedBy())) {
final Change change = TransportTracker.loadTransportChange(transport, load);
currentMove.addChange(change);
currentMove.load(transport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,12 @@ private static Collection<Unit> findNonDependentUnits(
final Collection<Unit> units,
final Route route,
final Map<Unit, Collection<Unit>> airTransportDependents) {
final Map<Unit, Collection<Unit>> dependentsMap =
getDependents(CollectionUtils.getMatches(units, Matches.unitCanTransport()));
final Set<Unit> dependents =
dependentsMap.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
dependents.addAll(
airTransportDependents.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet()));
final Collection<Unit> unitsWithoutDependents = new ArrayList<>();
unitsWithoutDependents.addAll(route.getStart().isWater() ? getNonLand(units) : units);
unitsWithoutDependents.removeAll(dependents);
unitsWithoutDependents.removeIf(u -> u.getTransportedBy() != null);
for (Collection<Unit> deps : airTransportDependents.values()) {
unitsWithoutDependents.removeAll(deps);
}
return unitsWithoutDependents;
}

Expand Down Expand Up @@ -943,15 +938,6 @@ private static IntegerMap<Unit> getLandTransportsWithCapacity(
return map;
}

public static Map<Unit, Collection<Unit>> getDependents(final Collection<Unit> units) {
// just worry about transports
final Map<Unit, Collection<Unit>> dependents = new HashMap<>();
for (final Unit unit : units) {
dependents.put(unit, unit.getTransporting());
}
return dependents;
}

/**
* Checks that there are no enemy units on the route except possibly at the end. Submerged enemy
* units are not considered as they don't affect movement. AA and factory dont count as enemy.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package games.strategy.triplea.delegate.battle.steps.retreat;

import static games.strategy.triplea.Constants.UNIT_ATTACHMENT_NAME;
import static games.strategy.triplea.delegate.battle.BattleState.Side.DEFENSE;
import static games.strategy.triplea.delegate.battle.FakeBattleState.givenBattleStateBuilder;
import static games.strategy.triplea.delegate.battle.steps.BattleStepsTest.givenUnitCanEvade;
Expand Down Expand Up @@ -28,7 +27,6 @@
import games.strategy.engine.delegate.IDelegateBridge;
import games.strategy.engine.display.IDisplay;
import games.strategy.engine.history.IDelegateHistoryWriter;
import games.strategy.triplea.attachments.UnitAttachment;
import games.strategy.triplea.delegate.ExecutionStack;
import games.strategy.triplea.delegate.battle.BattleActions;
import games.strategy.triplea.delegate.battle.BattleState;
Expand Down Expand Up @@ -225,9 +223,6 @@ void retreatHappensWhenNotSubmersibleButHasRetreatTerritories() {
final GameData gameData = givenGameData().build();

final Unit unit = givenRealUnitCanEvade(gameData, defender);
final UnitAttachment unitAttachment =
(UnitAttachment) unit.getType().getAttachment(UNIT_ATTACHMENT_NAME);
when(unitAttachment.getTransportCapacity()).thenReturn(-1);
final Collection<Unit> retreatingUnits = List.of(unit);

final Territory retreatTerritory = mock(Territory.class);
Expand Down Expand Up @@ -265,7 +260,6 @@ void retreatHappensWhenNotSubmersibleButHasRetreatTerritories() {

@Test
void retreatHappensWhenDefendingIsSubmersibleAndHasRetreatTerritories() {

final Territory retreatTerritory = mock(Territory.class);
when(retreatTerritory.isWater()).thenReturn(true);
final UnitCollection retreatTerritoryCollection = mock(UnitCollection.class);
Expand All @@ -280,9 +274,6 @@ void retreatHappensWhenDefendingIsSubmersibleAndHasRetreatTerritories() {
.build();

final Unit unit = givenRealUnitCanEvade(gameData, defender);
final UnitAttachment unitAttachment =
(UnitAttachment) unit.getType().getAttachment(UNIT_ATTACHMENT_NAME);
when(unitAttachment.getTransportCapacity()).thenReturn(-1);
final Collection<Unit> retreatingUnits = List.of(unit);

final BattleState battleState =
Expand Down