Skip to content

Commit

Permalink
Optimize checks that report exactly the same fix in multiple diagnost…
Browse files Browse the repository at this point in the history
…ics, like SameNameButDifferent

RELNOTES=N/A
PiperOrigin-RevId: 498574682
cushon authored and Error Prone Team committed Dec 30, 2022
1 parent 8ddb7cb commit bcf4dcf
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.EnumSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
@@ -81,10 +82,18 @@ private JavacErrorDescriptionListener(
this.context = context;
this.dontUseErrors = dontUseErrors;
checkNotNull(endPositions);
// Optimization for checks that emit the same fix multiple times. Consider a check that renames
// all uses of a symbol, and reports the diagnostic on all occurrences of the symbol. This can
// be useful in environments where diagnostics are only shown on changed lines, but can lead to
// quadratic behaviour during fix application if we're not careful.
//
// Using IdentityHashMap is sufficient when the repeated fixes are exactly the same instance.
// Fix doesn't implement value equality, and we don't want to pay for it here anyways.
IdentityHashMap<Fix, AppliedFix> cache = new IdentityHashMap<>();
try {
CharSequence sourceFileContent = sourceFile.getCharContent(true);
AppliedFix.Applier applier = AppliedFix.fromSource(sourceFileContent, endPositions);
fixToAppliedFix = applier::apply;
fixToAppliedFix = fix -> cache.computeIfAbsent(fix, applier::apply);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Original file line number Diff line number Diff line change
@@ -153,17 +153,17 @@ private void handle(Tree tree) {
String simpleName = row.getKey();
Map<TypeSymbol, List<TreePath>> columns = row.getValue();

SuggestedFix.Builder fix = SuggestedFix.builder();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
if (columns.size() > 1) {
for (Map.Entry<TypeSymbol, List<TreePath>> cell : columns.entrySet()) {
for (TreePath treePath : cell.getValue()) {
TypeSymbol typeSymbol = cell.getKey();
getBetterImport(typeSymbol, simpleName)
.ifPresent(
imp -> {
String qualifiedName = qualifyType(state.withPath(treePath), fix, imp);
String qualifiedName = qualifyType(state.withPath(treePath), fixBuilder, imp);
String newSimpleName = qualifiedName + "." + simpleName;
fix.replace(treePath.getLeaf(), newSimpleName);
fixBuilder.replace(treePath.getLeaf(), newSimpleName);
});
}
}
@@ -175,13 +175,11 @@ private void handle(Tree tree) {
columns.keySet().stream()
.map(t -> t.getQualifiedName().toString())
.collect(joining(", ", "[", "]")));
SuggestedFix fix = fixBuilder.build();
for (List<TreePath> treePaths : trimmedTable.row(simpleName).values()) {
for (TreePath treePath : treePaths) {
state.reportMatch(
buildDescription(treePath.getLeaf())
.setMessage(message)
.addFix(fix.build())
.build());
buildDescription(treePath.getLeaf()).setMessage(message).addFix(fix).build());
}
}
}

0 comments on commit bcf4dcf

Please sign in to comment.