Skip to content

Commit

Permalink
Performance Improvements
Browse files Browse the repository at this point in the history
CopyFinderBasedBulkSearch:

 - implemented matches method
 - extracted matcher from loop

JavaFixUtilities:

 - fast path for JavaFixUtilities can-safely-remove scanners
  • Loading branch information
mbien committed Jul 15, 2024
1 parent 7fba278 commit 7ad885e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import com.sun.source.util.TreePath;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.lang.model.type.TypeMirror;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.modules.java.hints.providers.spi.HintDescription.AdditionalQueryConstraints;
import org.netbeans.api.java.source.matching.Matcher;
Expand All @@ -51,18 +49,17 @@ public CopyFinderBasedBulkSearch() {
@Override
public Map<String, Collection<TreePath>> match(CompilationInfo info, AtomicBoolean cancel, TreePath toSearch, BulkPattern pattern, Map<String, Long> timeLog) {
Parameters.notNull("info", info);

Map<String, Collection<TreePath>> result = new HashMap<>();
TreePath topLevel = new TreePath(info.getCompilationUnit());

for (Entry<Tree, String> e : ((BulkPatternImpl) pattern).pattern2Code.entrySet()) {
for (Occurrence od : Matcher.create(info).setCancel(new AtomicBoolean()).setUntypedMatching().setCancel(cancel).match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, e.getKey()), Collections.<String, TypeMirror>emptyMap()))) {
Collection<TreePath> c = result.get(e.getValue());
Matcher matcher = Matcher.create(info)
.setUntypedMatching()
.setCancel(cancel);

if (c == null) {
result.put(e.getValue(), c = new LinkedList<>());
}

c.add(od.getOccurrenceRoot());
for (Entry<Tree, String> e : ((BulkPatternImpl) pattern).pattern2Code.entrySet()) {
for (Occurrence od : matcher.match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, e.getKey()), Map.of()))) {
result.computeIfAbsent(e.getValue(), k -> new LinkedList<>())
.add(od.getOccurrenceRoot());
}
}

Expand All @@ -71,8 +68,20 @@ public Map<String, Collection<TreePath>> match(CompilationInfo info, AtomicBoole

@Override
public boolean matches(CompilationInfo info, AtomicBoolean cancel, TreePath toSearch, BulkPattern pattern) {
//XXX: performance
return !match(info, cancel, toSearch, pattern).isEmpty();
Parameters.notNull("info", info);

TreePath topLevel = new TreePath(info.getCompilationUnit());
Matcher matcher = Matcher.create(info)
.setUntypedMatching()
.setCancel(cancel);

for (Tree tree : ((BulkPatternImpl) pattern).pattern2Code.keySet()) {
if (!matcher.match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, tree), Map.of())).isEmpty()) {
return true;
}
}

return false;
}

@Override
Expand Down Expand Up @@ -109,7 +118,7 @@ private static final class BulkPatternImpl extends BulkPattern {
private final Map<Tree, String> pattern2Code;

public BulkPatternImpl(Collection<? extends AdditionalQueryConstraints> additionalConstraints, Map<Tree, String> pattern2Code) {
super(new LinkedList<String>(pattern2Code.values()), null, null, new LinkedList<AdditionalQueryConstraints>(additionalConstraints));
super(new LinkedList<>(pattern2Code.values()), null, null, new LinkedList<>(additionalConstraints));
this.pattern2Code = pattern2Code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1851,9 +1851,11 @@ public Void scan(Tree tree, Void p) {
if (tree == assignmentTree.getVariable()) {
if (!info.getTreeUtilities().isExpressionStatement(assignmentTree.getExpression()) && canHaveSideEffects(assignmentTree.getExpression())) {
ret.set(false);
return null;
}
} else {
ret.set(false);
return null;
}
}
case AND_ASSIGNMENT, DIVIDE_ASSIGNMENT, LEFT_SHIFT_ASSIGNMENT, MINUS_ASSIGNMENT,
Expand All @@ -1863,13 +1865,16 @@ public Void scan(Tree tree, Void p) {
if (tree == compoundAssignmentTree.getVariable()) {
if (!info.getTreeUtilities().isExpressionStatement(compoundAssignmentTree.getExpression()) && canHaveSideEffects(compoundAssignmentTree.getExpression())) {
ret.set(false);
return null;
}
} else {
ret.set(false);
return null;
}
}
default -> {
ret.set(false);
return null;
}
}
}
Expand All @@ -1891,6 +1896,7 @@ public Void scan(Tree tree, Void p) {
case METHOD_INVOCATION, NEW_CLASS, POSTFIX_DECREMENT, POSTFIX_INCREMENT,
PREFIX_DECREMENT, PREFIX_INCREMENT -> {
ret.set(true);
return null;
}
}
}
Expand Down

0 comments on commit 7ad885e

Please sign in to comment.