Skip to content

Commit b90c971

Browse files
committed
SONARJAVA-1389 SE: cache hashcode of programState
1 parent 76980ea commit b90c971

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

java-squid/src/main/java/org/sonar/java/se/ProgramState.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
public class ProgramState {
3838

39+
private int hashCode;
40+
3941
public static final ProgramState EMPTY_STATE = new ProgramState(
4042
Maps.<Symbol, SymbolicValue>newHashMap(),
4143
/* Empty state knows that null literal is null */
@@ -44,15 +46,16 @@ public class ProgramState {
4446
.put(SymbolicValue.TRUE_LITERAL, ConstraintManager.BooleanConstraint.TRUE)
4547
.put(SymbolicValue.FALSE_LITERAL, ConstraintManager.BooleanConstraint.FALSE)
4648
.build(),
47-
AVLTree.<ExplodedGraph.ProgramPoint, Integer>create(),
49+
AVLTree.<ExplodedGraph.ProgramPoint, Integer>create(),
4850
Lists.<SymbolicValue>newLinkedList());
4951

5052
final PMap<ExplodedGraph.ProgramPoint, Integer> visitedPoints;
5153
final Deque<SymbolicValue> stack;
5254
Map<Symbol, SymbolicValue> values;
5355
Map<SymbolicValue, Object> constraints;
5456

55-
public ProgramState(Map<Symbol, SymbolicValue> values, Map<SymbolicValue, Object> constraints, PMap<ExplodedGraph.ProgramPoint, Integer> visitedPoints, Deque<SymbolicValue> stack) {
57+
public ProgramState(Map<Symbol, SymbolicValue> values, Map<SymbolicValue, Object> constraints, PMap<ExplodedGraph.ProgramPoint, Integer> visitedPoints,
58+
Deque<SymbolicValue> stack) {
5659
this.values = ImmutableMap.copyOf(values);
5760
this.constraints = ImmutableMap.copyOf(constraints);
5861
this.visitedPoints = visitedPoints;
@@ -66,7 +69,7 @@ static ProgramState stackValue(ProgramState ps, SymbolicValue sv) {
6669
}
6770

6871
static Pair<ProgramState, List<SymbolicValue>> unstack(ProgramState programState, int nbElements) {
69-
if(nbElements == 0) {
72+
if (nbElements == 0) {
7073
return new Pair<>(programState, Collections.<SymbolicValue>emptyList());
7174
}
7275
Preconditions.checkArgument(programState.stack.size() >= nbElements, nbElements);
@@ -98,7 +101,7 @@ public SymbolicValue peekValue() {
98101

99102
int numberOfTimeVisited(ExplodedGraph.ProgramPoint programPoint) {
100103
Integer count = visitedPoints.get(programPoint);
101-
return count==null ? 0 : count;
104+
return count == null ? 0 : count;
102105
}
103106

104107
@Override
@@ -117,7 +120,10 @@ public boolean equals(Object o) {
117120

118121
@Override
119122
public int hashCode() {
120-
return Objects.hash(values, constraints, peekValue());
123+
if (hashCode == 0) {
124+
hashCode = Objects.hash(values, constraints, peekValue());
125+
}
126+
return hashCode;
121127
}
122128

123129
@Override

0 commit comments

Comments
 (0)