Skip to content

Commit

Permalink
1746 trick view variable search causes large spike in cpu usage (#1786)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
sharmeye and hchen99 authored Sep 27, 2024
1 parent c007cfc commit d34844e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 10 additions & 1 deletion trick_source/java/src/main/java/trick/sie/utils/SearchPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public void actionPerformed(ActionEvent actionEvent) {
listModel.clear();
searcher.search(textField.getText().trim(),
caseSensitiveCheckBox.isSelected(),
regularExpressionCheckBox.isSelected());
regularExpressionCheckBox.isSelected(),
greedySearchCheckBox.isSelected());
if (searcher.elementCount == 0) {
progressBar.setIndeterminate(true);
}
Expand All @@ -136,6 +137,12 @@ public void actionPerformed(ActionEvent actionEvent) {
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();

Expand Down Expand Up @@ -227,6 +234,7 @@ public SearchPanel() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(caseSensitiveCheckBox);
add(regularExpressionCheckBox);
add(greedySearchCheckBox);
}});
}}, constraints);

Expand Down Expand Up @@ -306,6 +314,7 @@ public void setEnabled(boolean enabled) {
textField.setEnabled(enabled);
caseSensitiveCheckBox.setEnabled(enabled);
regularExpressionCheckBox.setEnabled(enabled);
greedySearchCheckBox.setEnabled(enabled);
list.setEnabled(enabled);
list.setComponentPopupMenu(enabled ? popupMenu : null);
}
Expand Down
9 changes: 7 additions & 2 deletions trick_source/java/src/main/java/trick/sie/utils/Searcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ public void cancelSearch() {
* @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 ?

Expand Down Expand Up @@ -124,7 +125,11 @@ public boolean search(SieTemplate sieTemplate) {

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<SieTemplate> roots = new ConcurrentLinkedQueue<SieTemplate>(rootTemplates);
executorService = Executors.newFixedThreadPool(threads);
Expand Down

0 comments on commit d34844e

Please sign in to comment.