Skip to content

Commit

Permalink
Cache the result of AbstractAnalysis.getResult() (#6624)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst committed May 28, 2024
1 parent a28e351 commit d388b89
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public Direction getDirection() {
return this.direction;
}

/** A cache for {@link #getResult()}. */
private @Nullable AnalysisResult<V, S> getResultCache;

@Override
@SuppressWarnings("nullness:contracts.precondition.override") // implementation field
@RequiresNonNull("cfg")
Expand All @@ -168,8 +171,16 @@ public AnalysisResult<V, S> getResult() {
throw new BugInCF(
"AbstractAnalysis::getResult() shouldn't be called when the analysis is running.");
}
return new AnalysisResult<>(
nodeValues, inputs, cfg.getTreeLookup(), cfg.getPostfixNodeLookup(), finalLocalValues);
if (getResultCache == null) {
getResultCache =
new AnalysisResult<>(
nodeValues,
inputs,
cfg.getTreeLookup(),
cfg.getPostfixNodeLookup(),
finalLocalValues);
}
return getResultCache;
}

@Override
Expand Down Expand Up @@ -427,6 +438,7 @@ protected void initFields(ControlFlowGraph cfg) {
nodeValues.clear();
finalLocalValues.clear();
this.cfg = cfg;
getResultCache = null;
}

/**
Expand Down
34 changes: 17 additions & 17 deletions dataflow/tests/busyexpr/Expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

2:
Process order: 1
AnalysisResult#1
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
<entry>

3:
Process order: 2
AnalysisResult#3
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
a [ VariableDeclaration ]
Expand All @@ -55,14 +55,14 @@ After: busy expressions = (b >> a)

4:
Process order: 3
AnalysisResult#5
AnalysisResult#0
Before: busy expressions = (b >> a)
~~~~~~~~~
ConditionalBlock: then: 8, else: 14

8:
Process order: 4
AnalysisResult#7
AnalysisResult#0
Before: busy expressions = (b >> a)
~~~~~~~~~
x [ LocalVariable ]
Expand All @@ -80,7 +80,7 @@ After: busy expressions = none

14:
Process order: 8
AnalysisResult#9
AnalysisResult#0
Before: busy expressions = (b >> a)
~~~~~~~~~
y [ LocalVariable ]
Expand All @@ -104,7 +104,7 @@ After: busy expressions = (a - b)

9:
Process order: 5
AnalysisResult#11
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
Test [ ClassName ]
Expand All @@ -114,7 +114,7 @@ After: busy expressions = none

15:
Process order: 9
AnalysisResult#13
AnalysisResult#0
Before: busy expressions = (a - b)
~~~~~~~~~
(this).test((a - b)) [ MethodInvocation ]
Expand All @@ -124,7 +124,7 @@ After: busy expressions = none

11:
Process order: 6
AnalysisResult#15
AnalysisResult#0
Before: busy expressions = (a - b)
~~~~~~~~~
new Test((a - b)) [ ObjectCreation ]
Expand All @@ -134,14 +134,14 @@ After: busy expressions = none

1:
Process order: 17
AnalysisResult#17
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
<exceptional-exit>

16:
Process order: 10
AnalysisResult#19
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
expression statement test(a - b) [ ExpressionStatement ]
Expand All @@ -151,7 +151,7 @@ After: busy expressions = none

12:
Process order: 7
AnalysisResult#21
AnalysisResult#0
Before: busy expressions = (a + b)
~~~~~~~~~
expression statement new Test(a - b) [ ExpressionStatement ]
Expand All @@ -167,7 +167,7 @@ After: busy expressions = none

17:
Process order: 11
AnalysisResult#23
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
d [ VariableDeclaration ]
Expand All @@ -182,7 +182,7 @@ After: busy expressions = none

18:
Process order: 12
AnalysisResult#25
AnalysisResult#0
Before: busy expressions = (y / x)
~~~~~~~~~
(y / x) [ IntegerDivision ]
Expand All @@ -192,7 +192,7 @@ After: busy expressions = (y / x)

19:
Process order: 13
AnalysisResult#27
AnalysisResult#0
Before: busy expressions = (y / x)
~~~~~~~~~
(y / x) [ IntegerDivision ]
Expand All @@ -205,7 +205,7 @@ After: busy expressions = none

21:
Process order: 14
AnalysisResult#29
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
marker (start of catch block for ArithmeticException #0) [ CatchMarker ]
Expand All @@ -221,7 +221,7 @@ After: busy expressions = none

23:
Process order: 15
AnalysisResult#31
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
d [ LocalVariable ]
Expand All @@ -232,7 +232,7 @@ After: busy expressions = none

0:
Process order: 16
AnalysisResult#33
AnalysisResult#0
Before: busy expressions = none
~~~~~~~~~
<exit>
8 changes: 4 additions & 4 deletions dataflow/tests/constant-propagation/Expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ a [ LocalVariable ] > 0
5 [ IntegerLiteral ] > 5
(a > 5) [ GreaterThan ]
~~~~~~~~~
AnalysisResult#1
AnalysisResult#0
After: constant propagation = {a=0}

4:
Expand All @@ -46,7 +46,7 @@ a [ LocalVariable ]
b = a [ Assignment ]
expression statement b = a [ ExpressionStatement ]
~~~~~~~~~
AnalysisResult#4
AnalysisResult#0
After: constant propagation = {a=0, b=0}

10:
Expand All @@ -59,7 +59,7 @@ b [ LocalVariable ]
b = 4 [ Assignment ]
expression statement b = 4 [ ExpressionStatement ]
~~~~~~~~~
AnalysisResult#7
AnalysisResult#0
After: constant propagation = {a=0, b=4}

11:
Expand All @@ -70,7 +70,7 @@ Before: constant propagation = {a=0, b=T}
b [ LocalVariable ]
return b [ Return ]
~~~~~~~~~
AnalysisResult#10
AnalysisResult#0
After: constant propagation = {a=0, b=T}

0:
Expand Down
22 changes: 11 additions & 11 deletions dataflow/tests/live-variable/Expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

2:
Process order: 1
AnalysisResult#1
AnalysisResult#0
Before: live variables = none
~~~~~~~~~
<entry>

3:
Process order: 2
AnalysisResult#3
AnalysisResult#0
Before: live variables = none
~~~~~~~~~
a [ VariableDeclaration ]
Expand All @@ -41,14 +41,14 @@ After: live variables = a, b, c

4:
Process order: 3
AnalysisResult#5
AnalysisResult#0
Before: live variables = a, b, c
~~~~~~~~~
ConditionalBlock: then: 8, else: 10

8:
Process order: 4
AnalysisResult#7
AnalysisResult#0
Before: live variables = a, c
~~~~~~~~~
d [ VariableDeclaration ]
Expand All @@ -62,7 +62,7 @@ After: live variables = a

10:
Process order: 5
AnalysisResult#9
AnalysisResult#0
Before: live variables = a, b
~~~~~~~~~
e [ VariableDeclaration ]
Expand All @@ -76,7 +76,7 @@ After: live variables = a

11:
Process order: 6
AnalysisResult#11
AnalysisResult#0
Before: live variables = a
~~~~~~~~~
f [ VariableDeclaration ]
Expand All @@ -95,7 +95,7 @@ After: live variables = a, b

12:
Process order: 7
AnalysisResult#13
AnalysisResult#0
Before: live variables = a
~~~~~~~~~
(1 / a) [ IntegerDivision ]
Expand All @@ -105,7 +105,7 @@ After: live variables = a

13:
Process order: 8
AnalysisResult#15
AnalysisResult#0
Before: live variables = a
~~~~~~~~~
(1 / a) [ IntegerDivision ]
Expand All @@ -118,7 +118,7 @@ After: live variables = a

15:
Process order: 9
AnalysisResult#17
AnalysisResult#0
Before: live variables = a, b
~~~~~~~~~
marker (start of catch block for ArithmeticException #0) [ CatchMarker ]
Expand All @@ -134,7 +134,7 @@ After: live variables = a

17:
Process order: 10
AnalysisResult#19
AnalysisResult#0
Before: live variables = a
~~~~~~~~~
a [ LocalVariable ]
Expand All @@ -145,7 +145,7 @@ After: live variables = none

0:
Process order: 11
AnalysisResult#21
AnalysisResult#0
Before: live variables = none
~~~~~~~~~
<exit>
8 changes: 4 additions & 4 deletions dataflow/tests/reachingdef/Expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ a [ LocalVariable ]
0 [ IntegerLiteral ]
(a > 0) [ GreaterThan ]
~~~~~~~~~
AnalysisResult#1
AnalysisResult#0
After: reaching definitions = { a = 1, b = 2, c = 3, x = "a", y = "b" }

4:
Expand All @@ -58,7 +58,7 @@ c [ LocalVariable ]
(a + c) [ NumericalAddition ]
d = (a + c) [ Assignment ]
~~~~~~~~~
AnalysisResult#4
AnalysisResult#0
After: reaching definitions = { a = 1, b = 2, c = 3, x = "a", y = "b", d = (a + c) }

10:
Expand All @@ -72,7 +72,7 @@ b [ LocalVariable ]
(a + b) [ NumericalAddition ]
e = (a + b) [ Assignment ]
~~~~~~~~~
AnalysisResult#7
AnalysisResult#0
After: reaching definitions = { a = 1, b = 2, c = 3, x = "a", y = "b", e = (a + b) }

11:
Expand All @@ -96,7 +96,7 @@ expression statement x += y [ ExpressionStatement ]
a [ LocalVariable ]
return a [ Return ]
~~~~~~~~~
AnalysisResult#10
AnalysisResult#0
After: reaching definitions = { c = 3, y = "b", e = (a + b), d = (a + c), b = 0, a = b, x = (x + y) }

0:
Expand Down

0 comments on commit d388b89

Please sign in to comment.