From b515056e224e2bbd3cb2b611046ed0726345ca7d Mon Sep 17 00:00:00 2001 From: asvitkine Date: Wed, 12 Jul 2023 08:59:42 -0400 Subject: [PATCH] Optimization: Reduce amount of code under lock. 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). --- .../strategy/triplea/ui/CommentPanel.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java index 091f756b210..8bd306aa994 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java @@ -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); } } }); @@ -174,7 +177,6 @@ private void loadHistory() { ThreadRunner.runInNewThread( () -> { final HistoryNode rootNode = (HistoryNode) data.getHistory().getRoot(); - @SuppressWarnings("unchecked") final Enumeration nodeEnum = rootNode.preorderEnumeration(); final Pattern p = Pattern.compile("^COMMENT: (.*)"); String player = "";