Skip to content

Commit 4a0679e

Browse files
authored
Omit some expensive GameData members when cloning for BattleCalc. (#10467)
* Minimize battle calc data cloning. * Clean up * Improve comment. * Address style comments.
1 parent 37463e9 commit 4a0679e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

game-app/game-core/src/main/java/games/strategy/engine/data/GameData.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class GameData implements Serializable, GameState {
107107
@Getter private transient TechTracker techTracker = new TechTracker(this);
108108
private final IGameLoader loader = new TripleA();
109109
private History gameHistory = new History(this);
110-
private final List<Tuple<IAttachment, List<Tuple<String, String>>>> attachmentOrderAndValues =
110+
private List<Tuple<IAttachment, List<Tuple<String, String>>>> attachmentOrderAndValues =
111111
new ArrayList<>();
112112
private final Map<String, TerritoryEffect> territoryEffectList = new HashMap<>();
113113
private final BattleRecordsList battleRecordsList = new BattleRecordsList(this);
@@ -401,6 +401,11 @@ public List<Tuple<IAttachment, List<Tuple<String, String>>>> getAttachmentOrderA
401401
return attachmentOrderAndValues;
402402
}
403403

404+
public void setAttachmentOrderAndValues(
405+
List<Tuple<IAttachment, List<Tuple<String, String>>>> values) {
406+
attachmentOrderAndValues = values;
407+
}
408+
404409
/**
405410
* Returns all relationshipTypes that are valid in this game, default there is the NullRelation
406411
* (relation with the Null player / Neutral) and the SelfRelation (Relation with yourself) all

game-app/game-core/src/main/java/games/strategy/engine/framework/GameDataManager.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,27 @@ public static void saveGame(
207207
public static void saveGameUncompressed(
208208
final OutputStream sink,
209209
final GameData data,
210-
final boolean saveDelegateInfo,
210+
final boolean saveDelegateInfoHistoryAndAttachmentsOrder,
211211
final Version engineVersion)
212212
throws IOException {
213213
// write to temporary file first in case of error
214214
try (ObjectOutputStream outStream = new ObjectOutputStream(sink)) {
215215
outStream.writeObject(engineVersion);
216216
data.acquireReadLock();
217217
try {
218-
outStream.writeObject(data);
219-
if (saveDelegateInfo) {
218+
if (saveDelegateInfoHistoryAndAttachmentsOrder) {
219+
outStream.writeObject(data);
220220
writeDelegates(data, outStream);
221221
} else {
222+
// TODO: Attachment order data is only used for XML export and takes up lots of memory.
223+
// Could we remove it and just get the info again from the XML when exporting?
224+
final var attachments = data.getAttachmentOrderAndValues();
225+
final var history = data.getHistory();
226+
data.setAttachmentOrderAndValues(null);
227+
data.resetHistory();
228+
outStream.writeObject(data);
229+
data.setAttachmentOrderAndValues(attachments);
230+
data.setHistory(history);
222231
outStream.writeObject(DELEGATE_LIST_END);
223232
}
224233
} finally {

0 commit comments

Comments
 (0)