Skip to content

Commit ac5e60f

Browse files
committed
perf: add tree processing cache to avoid redundant computations
Cache NewClassTree processing in NullnessNoInitAnnotatedTypeFactory to prevent duplicate type annotation calculations.
1 parent 8b0032c commit ac5e60f

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

checker/src/main/java/org/checkerframework/checker/nullness/NullnessNoInitAnnotatedTypeFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public class NullnessNoInitAnnotatedTypeFactory
113113
private final ExecutableElement mapGet =
114114
TreeUtils.getMethod("java.util.Map", "get", 1, processingEnv);
115115

116+
// High-level optimization: cache entire addComputedTypeAnnotations processing flow
117+
private final java.util.Set<String> fullyProcessedTrees = new java.util.HashSet<>();
118+
116119
// List is in alphabetical order. If you update it, also update
117120
// ../../../../../../../../docs/manual/nullness-checker.tex
118121
// and make a pull request for variables NONNULL_ANNOTATIONS and BASE_COPYABLE_ANNOTATIONS in
@@ -483,6 +486,19 @@ protected void replacePolyQualifier(AnnotatedTypeMirror lhsType, Tree context) {
483486
}
484487
}
485488

489+
@Override
490+
protected void addComputedTypeAnnotations(Tree tree, AnnotatedTypeMirror type) {
491+
if (tree != null && tree instanceof NewClassTree) {
492+
String treeKey = tree.toString();
493+
if (fullyProcessedTrees.contains(treeKey)) {
494+
return;
495+
}
496+
fullyProcessedTrees.add(treeKey);
497+
}
498+
499+
super.addComputedTypeAnnotations(tree, type);
500+
}
501+
486502
@Override
487503
protected NullnessNoInitAnalysis createFlowAnalysis() {
488504
return new NullnessNoInitAnalysis(checker, this);

docs/examples/NewObject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class NewObject {
2+
void test() {
3+
Object object = new Object();
4+
}
5+
}

0 commit comments

Comments
 (0)