Skip to content

Commit

Permalink
Optimization: Reduce amount of code under lock. (#11770)
Browse files Browse the repository at this point in the history
This was showing in profiles when playing all-AI Imperialism 1974, which creates a lot of history nodes.

With this optimization, I measured the time it takes to do 2 AI's turns on that map and saw around 10% total runtime improvement (~20s to ~19s).
  • Loading branch information
asvitkine committed Jul 12, 2023
1 parent 235085e commit 559f26c
Showing 1 changed file with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,34 @@ public void treeStructureChanged(final TreeModelEvent e) {
private void readHistoryTreeEvent(final TreeModelEvent e) {
SwingAction.invokeNowOrLater(
() -> {
try (GameData.Unlocker ignored = data.acquireReadLock()) {
final Document doc = text.getDocument();
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
final TreeNode child =
node == null ? null : (node.getChildCount() > 0 ? node.getLastChild() : null);
final String title =
child != null
? (child instanceof Event ? ((Event) child).getDescription() : child.toString())
: (node != null ? node.getTitle() : "");
final Pattern p = Pattern.compile("^COMMENT: (.*)");
final Matcher m = p.matcher(title);
if (m.matches()) {
final GamePlayer gamePlayer = data.getSequence().getStep().getPlayerId();
final int round = data.getSequence().getRound();
final String player = gamePlayer.getName();
final Icon icon = iconMap.get(gamePlayer);
try {
// insert into ui document
final String prefix = " " + player + "(" + round + ") : ";
text.insertIcon(icon);
doc.insertString(doc.getLength(), prefix, bold);
doc.insertString(doc.getLength(), m.group(1) + "\n", normal);
} catch (final BadLocationException e1) {
log.error("Failed to add history node", e1);
}
final Document doc = text.getDocument();
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
final TreeNode child =
node == null ? null : (node.getChildCount() > 0 ? node.getLastChild() : null);
final String title =
child != null
? (child instanceof Event ? ((Event) child).getDescription() : child.toString())
: (node != null ? node.getTitle() : "");
final Pattern p = Pattern.compile("^COMMENT: (.*)");
final Matcher m = p.matcher(title);
if (m.matches()) {
final GamePlayer gamePlayer;
final String player;
final int round;
try (GameData.Unlocker ignored = data.acquireReadLock()) {
gamePlayer = data.getSequence().getStep().getPlayerId();
player = gamePlayer.getName();
round = data.getSequence().getRound();
}
final Icon icon = iconMap.get(gamePlayer);
try {
// insert into ui document
final String prefix = " " + player + "(" + round + ") : ";
text.insertIcon(icon);
doc.insertString(doc.getLength(), prefix, bold);
doc.insertString(doc.getLength(), m.group(1) + "\n", normal);
} catch (final BadLocationException e1) {
log.error("Failed to add history node", e1);
}
}
});
Expand All @@ -174,7 +177,6 @@ private void loadHistory() {
ThreadRunner.runInNewThread(
() -> {
final HistoryNode rootNode = (HistoryNode) data.getHistory().getRoot();
@SuppressWarnings("unchecked")
final Enumeration<TreeNode> nodeEnum = rootNode.preorderEnumeration();
final Pattern p = Pattern.compile("^COMMENT: (.*)");
String player = "";
Expand Down

0 comments on commit 559f26c

Please sign in to comment.