From 8cd2da515487b86f71395c7c19efefce10b49c8a Mon Sep 17 00:00:00 2001 From: asvitkine Date: Sun, 23 Jul 2023 20:41:10 -0400 Subject: [PATCH] Some small optimizations to BattleModel, (#11806) 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.) --- .../strategy/triplea/ui/BattleModel.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/BattleModel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/BattleModel.java index cea8463f88c..f76f8495aed 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/BattleModel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/BattleModel.java @@ -70,18 +70,21 @@ void setEnemyBattleModel(final BattleModel enemyBattleModel) { } void notifyRetreat(final Collection retreating) { - units.removeAll(retreating); - refresh(); + if (units.removeAll(retreating)) { + refresh(); + } } void removeCasualties(final Collection killed) { - units.removeAll(killed); - refresh(); + if (units.removeAll(killed)) { + refresh(); + } } void addUnits(final Collection units) { - this.units.addAll(units); - refresh(); + if (this.units.addAll(units)) { + refresh(); + } } Collection getUnits() { @@ -100,13 +103,14 @@ void refresh() { final List 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 { @@ -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();