From d34844e76caf37a391b75f626ab79a80f1b70b8f Mon Sep 17 00:00:00 2001 From: Sean Harmeyer <117398532+sharmeye@users.noreply.github.com> Date: Fri, 27 Sep 2024 08:25:43 -0500 Subject: [PATCH] 1746 trick view variable search causes large spike in cpu usage (#1786) * Add new "greedy search" option to toggle multi-threaded variable search * Back down to a single thread * Fixed a typo. --------- Co-authored-by: Hong Chen --- .../src/main/java/trick/sie/utils/SearchPanel.java | 11 ++++++++++- .../java/src/main/java/trick/sie/utils/Searcher.java | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/trick_source/java/src/main/java/trick/sie/utils/SearchPanel.java b/trick_source/java/src/main/java/trick/sie/utils/SearchPanel.java index 34c7de17..2fe40178 100644 --- a/trick_source/java/src/main/java/trick/sie/utils/SearchPanel.java +++ b/trick_source/java/src/main/java/trick/sie/utils/SearchPanel.java @@ -115,7 +115,8 @@ public class SearchPanel extends JXPanel { listModel.clear(); searcher.search(textField.getText().trim(), caseSensitiveCheckBox.isSelected(), - regularExpressionCheckBox.isSelected()); + regularExpressionCheckBox.isSelected(), + greedySearchCheckBox.isSelected()); if (searcher.elementCount == 0) { progressBar.setIndeterminate(true); } @@ -136,6 +137,12 @@ public class SearchPanel extends JXPanel { setToolTipText("Toggle regular expression searching."); }}; + /** toggles case-insensitive searching */ + JCheckBox greedySearchCheckBox = new JCheckBox("Greedy Search") {{ + setName("greedySearchCheckBox"); + setToolTipText("Toggle multi-threaded search (Warning: may cause overruns)."); + }}; + /** search results list model */ EfficientListModel listModel = new EfficientListModel(); @@ -227,6 +234,7 @@ public class SearchPanel extends JXPanel { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(caseSensitiveCheckBox); add(regularExpressionCheckBox); + add(greedySearchCheckBox); }}); }}, constraints); @@ -306,6 +314,7 @@ public class SearchPanel extends JXPanel { textField.setEnabled(enabled); caseSensitiveCheckBox.setEnabled(enabled); regularExpressionCheckBox.setEnabled(enabled); + greedySearchCheckBox.setEnabled(enabled); list.setEnabled(enabled); list.setComponentPopupMenu(enabled ? popupMenu : null); } diff --git a/trick_source/java/src/main/java/trick/sie/utils/Searcher.java b/trick_source/java/src/main/java/trick/sie/utils/Searcher.java index 04a2245c..aaf20e1b 100644 --- a/trick_source/java/src/main/java/trick/sie/utils/Searcher.java +++ b/trick_source/java/src/main/java/trick/sie/utils/Searcher.java @@ -89,9 +89,10 @@ public class Searcher { * @param targetText the text for which to search * @param caseSensitive enables case sensitive searching * @param regularExpression enables regular expression searching + * @param greedSearch enabled multi-threaded search */ public void search(final String targetText, final boolean caseSensitive, - final boolean regularExpression) { + final boolean regularExpression, final boolean greedySearch) { final SearchFunction searchFunction = regularExpression ? @@ -124,7 +125,11 @@ public class Searcher { cancelSearch(); count = 0; - threads = Runtime.getRuntime().availableProcessors(); + if (greedySearch) { + threads = Runtime.getRuntime().availableProcessors(); + } else { + threads = 1; + } propertyChangeListener.propertyChange(new PropertyChangeEvent(this, "progress", 0, 0)); final ConcurrentLinkedQueue roots = new ConcurrentLinkedQueue(rootTemplates); executorService = Executors.newFixedThreadPool(threads);