Skip to content

Commit 2baa7f1

Browse files
committed
Eagerly compute (and cache) the hashCode
This actually matters, because while the hashCode for a BooleanQuery is already cached, it's not eagerly computed. Pre-computing the hashCode moves the initial calculation of the hashCode outside of anywhere that we're holding a lock.
1 parent a995a12 commit 2baa7f1

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,15 @@ private static final class BitsetCacheKey {
327327

328328
final IndexReader.CacheKey index;
329329
final Query query;
330+
final int hashCode;
330331

331332
private BitsetCacheKey(IndexReader.CacheKey index, Query query) {
332333
this.index = index;
333334
this.query = query;
335+
// compute the hashCode eagerly, since it's used multiple times in the cache implementation anyway -- the query here will
336+
// be a ConstantScoreQuery around a BooleanQuery, and BooleanQuery already *lazily* caches the hashCode, so this isn't
337+
// altogether that much faster in reality, but it makes it more explicit here that we're doing this
338+
this.hashCode = computeHashCode();
334339
}
335340

336341
@Override
@@ -345,9 +350,15 @@ public boolean equals(Object other) {
345350
return Objects.equals(this.index, that.index) && Objects.equals(this.query, that.query);
346351
}
347352

353+
private int computeHashCode() {
354+
int result = index.hashCode();
355+
result = 31 * result + query.hashCode();
356+
return result;
357+
}
358+
348359
@Override
349360
public int hashCode() {
350-
return Objects.hash(index, query);
361+
return hashCode;
351362
}
352363

353364
@Override

0 commit comments

Comments
 (0)