Skip to content

Commit 949c87f

Browse files
authored
Cache IntervalWindow hashCode (#36612)
1 parent 0c10658 commit 949c87f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/windowing/IntervalWindow.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@
3838
* (inclusive) to {@link #end} (exclusive).
3939
*/
4040
public class IntervalWindow extends BoundedWindow implements Comparable<IntervalWindow> {
41+
4142
/** Start of the interval, inclusive. */
4243
private final Instant start;
4344

4445
/** End of the interval, exclusive. */
4546
private final Instant end;
4647

48+
// Cached hashCode. ints don't tear and access don't need to be synchronized.
49+
// Stale reads if any will return 0 and will recalculate hashCode.
50+
// ByteString and String hashCodes are cached similarly.
51+
private int hashCode; // Default is 0.
52+
4753
/** Creates a new IntervalWindow that represents the half-open time interval [start, end). */
4854
public IntervalWindow(Instant start, Instant end) {
4955
this.start = start;
@@ -103,10 +109,13 @@ public boolean equals(@Nullable Object o) {
103109

104110
@Override
105111
public int hashCode() {
106-
// The end values are themselves likely to be arithmetic sequence, which
107-
// is a poor distribution to use for a hashtable, so we
108-
// add a highly non-linear transformation.
109-
return (int) (start.getMillis() + modInverse((int) (end.getMillis() << 1) + 1));
112+
if (hashCode == 0) {
113+
// The end values are themselves likely to be arithmetic sequence, which
114+
// is a poor distribution to use for a hashtable, so we
115+
// add a highly non-linear transformation.
116+
hashCode = (int) (start.getMillis() + modInverse((int) (end.getMillis() << 1) + 1));
117+
}
118+
return hashCode;
110119
}
111120

112121
/** Compute the inverse of (odd) x mod 2^32. */

0 commit comments

Comments
 (0)