-
Notifications
You must be signed in to change notification settings - Fork 29
Cache the hashCode for dataflow expressions #1643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,9 +88,15 @@ public boolean isModifiableByOtherCode() { | |
| return true; | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(dimensions, initializers, getType().toString()); | ||
| if (hashCodeCache == 0) { | ||
| hashCodeCache = Objects.hash(dimensions, initializers, getType().toString()); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
+91
to
+99
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -130,9 +130,15 @@ public boolean containsModifiableAliasOf(Store<?> store, JavaExpression other) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| || right.containsModifiableAliasOf(store, other); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Cache the hashCode. Recomputed if zero. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private int hashCodeCache = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int hashCode() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Objects.hash(operationKind, left, right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hashCodeCache == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hashCodeCache = Objects.hash(operationKind, left, right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return hashCodeCache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+139
to
143
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hashCodeCache = Objects.hash(operationKind, left, right); | |
| } | |
| return hashCodeCache; | |
| } | |
| hashCodeCache = computeHashCode(); | |
| } | |
| return hashCodeCache; | |
| } | |
| /** | |
| * Computes a hash code consistent with equals(Object). | |
| * | |
| * <p>For commutative operations, the operand contribution must be order-independent because | |
| * equals(Object) considers (left, right) equal to (right, left). For non-commutative | |
| * operations, preserve the ordered hash. | |
| */ | |
| private int computeHashCode() { | |
| if (isCommutative()) { | |
| int leftHash = left.hashCode(); | |
| int rightHash = right.hashCode(); | |
| int first = Math.min(leftHash, rightHash); | |
| int second = Math.max(leftHash, rightHash); | |
| return Objects.hash(operationKind, first, second); | |
| } | |
| return Objects.hash(operationKind, left, right); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,9 +117,15 @@ public boolean equals(@Nullable Object obj) { | |
| || this.getReceiver() instanceof ThisReference); | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(getField(), getReceiver()); | ||
| if (hashCodeCache == 0) { | ||
| hashCodeCache = Objects.hash(getField(), getReceiver()); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
124
to
+128
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,14 +64,21 @@ public VariableElement getElement() { | |
| return element; | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| VarSymbol vs = (VarSymbol) element; | ||
| return Objects.hash( | ||
| index, | ||
| vs.name.toString(), | ||
| TypeAnnotationUtils.unannotatedType(vs.type).toString(), | ||
| vs.owner.toString()); | ||
| if (hashCodeCache == 0) { | ||
| VarSymbol vs = (VarSymbol) element; | ||
| hashCodeCache = | ||
| Objects.hash( | ||
| index, | ||
| vs.name.toString(), | ||
| TypeAnnotationUtils.unannotatedType(vs.type).toString(), | ||
| vs.owner.toString()); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
+67
to
+81
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,10 +79,16 @@ public VariableElement getElement() { | |
| return element; | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| VarSymbol vs = (VarSymbol) element; | ||
| return Objects.hash(vs.pos, vs.name, vs.owner); | ||
| if (hashCodeCache == 0) { | ||
| VarSymbol vs = (VarSymbol) element; | ||
| hashCodeCache = Objects.hash(vs.pos, vs.name, vs.owner); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
+82
to
+91
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,13 +167,20 @@ public boolean equals(@Nullable Object obj) { | |
| && arguments.equals(other.arguments); | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| if (method.getKind() == ElementKind.CONSTRUCTOR) { | ||
| // No two constructor instances have the same hashcode. | ||
| return System.identityHashCode(this); | ||
| if (hashCodeCache == 0) { | ||
| if (method.getKind() == ElementKind.CONSTRUCTOR) { | ||
| // No two constructor instances have the same hashcode. | ||
| hashCodeCache = System.identityHashCode(this); | ||
| } else { | ||
| hashCodeCache = Objects.hash(method, receiver, arguments); | ||
| } | ||
| } | ||
| return Objects.hash(method, receiver, arguments); | ||
| return hashCodeCache; | ||
|
Comment on lines
174
to
+183
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,9 +105,15 @@ public boolean containsModifiableAliasOf(Store<?> store, JavaExpression other) { | |
| return operand.containsModifiableAliasOf(store, other); | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(operationKind, operand); | ||
| if (hashCodeCache == 0) { | ||
| hashCodeCache = Objects.hash(operationKind, operand); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
+108
to
+116
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,9 +163,15 @@ public String toString() { | |
| return value == null ? "null" : value.toString(); | ||
| } | ||
|
|
||
| /** Cache the hashCode. Recomputed if zero. */ | ||
| private int hashCodeCache = 0; | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value, type.toString()); | ||
| if (hashCodeCache == 0) { | ||
| hashCodeCache = Objects.hash(value, type.toString()); | ||
| } | ||
| return hashCodeCache; | ||
|
Comment on lines
+166
to
+174
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hashCodeCacheuses 0 as the "not computed" sentinel. IfObjects.hash(array, index)computes to 0, this will recompute every call (and re-allocate) rather than caching. Use a separate boolean/nullable cache to distinguish "not computed" from a computed 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would require yet another field to store "computed" value. Seems okay to re-compute if the hash happens to be 0 - all other cases are covered.