Skip to content

Commit

Permalink
Improve battle calc logic for copying collections. (#11858)
Browse files Browse the repository at this point in the history
* Improve logic for copying collections.

Instead of copying the bombardingUnits, only copy when needed for serialization. Add some comments.

* Fix word in comment.

* Remove unused import.
  • Loading branch information
asvitkine authored Aug 5, 2023
1 parent aa61694 commit 7c1a019
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import games.strategy.triplea.odds.calculator.AggregateResults;
import games.strategy.triplea.odds.calculator.IBattleCalculator;
import games.strategy.triplea.util.TuvUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.triplea.java.collections.CollectionUtils;
Expand Down Expand Up @@ -253,7 +252,7 @@ private ProBattleResult callBattleCalc(
t,
attackingUnits,
defendingUnits,
new ArrayList<>(bombardingUnits),
bombardingUnits,
TerritoryEffectHelper.getEffects(t),
retreatWhenOnlyAirLeft,
runCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import games.strategy.triplea.delegate.battle.BattleTracker;
import games.strategy.triplea.delegate.battle.MustFightBattle;
import games.strategy.triplea.util.TuvCostsCalculator;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -68,13 +70,13 @@ public AggregateResults calculate(
: gameData.getPlayerList().getPlayerId(defender.getName());
final Territory location2 = gameData.getMap().getTerritory(location.getName());
final Collection<Unit> attackingUnits =
GameDataUtils.translateIntoOtherGameData(attacking, gameData);
translateCollectionIntoOtherGameData(attacking, gameData);
final Collection<Unit> defendingUnits =
GameDataUtils.translateIntoOtherGameData(defending, gameData);
translateCollectionIntoOtherGameData(defending, gameData);
final Collection<Unit> bombardingUnits =
GameDataUtils.translateIntoOtherGameData(bombarding, gameData);
translateCollectionIntoOtherGameData(bombarding, gameData);
final Collection<TerritoryEffect> territoryEffects2 =
GameDataUtils.translateIntoOtherGameData(territoryEffects, gameData);
translateCollectionIntoOtherGameData(territoryEffects, gameData);
gameData.performChange(ChangeFactory.removeUnits(location2, location2.getUnits()));
gameData.performChange(
ChangeFactory.addUnits(location2, mergeUnitCollections(attackingUnits, defendingUnits)));
Expand Down Expand Up @@ -125,6 +127,16 @@ public AggregateResults calculate(
}
}

private <T> Collection<T> translateCollectionIntoOtherGameData(
Collection<T> collection, GameData otherData) {
// translateIntoOtherGameData() uses serialization, so if the collection is not serializable,
// copy it into one that is. In particular, HashMap.keySet() and similar are not serializable.
if (!(collection instanceof Serializable)) {
collection = new ArrayList<>(collection);
}
return GameDataUtils.translateIntoOtherGameData(collection, otherData);
}

private Collection<Unit> mergeUnitCollections(Collection<Unit> c1, Collection<Unit> c2) {
var combined = new HashSet<>(c1);
combined.addAll(c2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ public AggregateResults calculate(
workers.parallelStream()
.map(
worker ->
// Note: Although we're running in parallel, the data passed in does not
// get modified, so no copies are necessary. Also, the outer calculate()
// call is synchronous, so there's no problem if the caller later modifies
// the collections that were provided.
worker.calculate(
attacker,
defender,
Expand Down

0 comments on commit 7c1a019

Please sign in to comment.