Skip to content

Commit

Permalink
Some small optimizations to BattleModel, (#11806)
Browse files Browse the repository at this point in the history
Only refresh if something changed.
Also, get lhtr property outside of a lock, since it won't be changing.

(The locking here showed a lot of contention.)
  • Loading branch information
asvitkine committed Jul 24, 2023
1 parent 17542ce commit 8cd2da5
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ void setEnemyBattleModel(final BattleModel enemyBattleModel) {
}

void notifyRetreat(final Collection<Unit> retreating) {
units.removeAll(retreating);
refresh();
if (units.removeAll(retreating)) {
refresh();
}
}

void removeCasualties(final Collection<Unit> killed) {
units.removeAll(killed);
refresh();
if (units.removeAll(killed)) {
refresh();
}
}

void addUnits(final Collection<Unit> units) {
this.units.addAll(units);
refresh();
if (this.units.addAll(units)) {
refresh();
}
}

Collection<Unit> getUnits() {
Expand All @@ -100,13 +103,14 @@ void refresh() {
final List<Unit> units = new ArrayList<>(this.units);
final TotalPowerAndTotalRolls unitPowerAndRollsMap;
final boolean isAirPreBattleOrPreRaid = battleType.isAirBattle();
final boolean lhtrHeavyBombers = Properties.getLhtrHeavyBombers(gameData.getProperties());
try (GameData.Unlocker ignored = gameData.acquireReadLock()) {
final CombatValue combatValue;
if (isAirPreBattleOrPreRaid) {
combatValue =
CombatValueBuilder.airBattleCombatValue()
.side(BattleState.Side.DEFENSE)
.lhtrHeavyBombers(Properties.getLhtrHeavyBombers(gameData.getProperties()))
.lhtrHeavyBombers(lhtrHeavyBombers)
.gameDiceSides(gameData.getDiceSides())
.build();
} else {
Expand All @@ -117,7 +121,7 @@ void refresh() {
.side(side)
.gameSequence(gameData.getSequence())
.supportAttachments(gameData.getUnitTypeList().getSupportRules())
.lhtrHeavyBombers(Properties.getLhtrHeavyBombers(gameData.getProperties()))
.lhtrHeavyBombers(lhtrHeavyBombers)
.gameDiceSides(gameData.getDiceSides())
.territoryEffects(territoryEffects)
.build();
Expand Down

0 comments on commit 8cd2da5

Please sign in to comment.