Skip to content

Commit

Permalink
Reorder checks in LRUQueryCache#count (#13742)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamvishu authored Sep 10, 2024
1 parent 0d57933 commit 97ca2c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ Optimizations

* GITHUB#13587: Use Max WAND optimizations with ToParentBlockJoinQuery when using ScoreMode.Max (Mike Pellegrini)

* GITHUB#13742: Reorder checks in LRUQueryCache#count (Shubham Chaudhary)

Changes in runtime behavior
---------------------

Expand Down
25 changes: 10 additions & 15 deletions lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,9 @@ public long cost() {

@Override
public int count(LeafReaderContext context) throws IOException {
// If the wrapped weight can count quickly then use that
int innerCount = in.count(context);
if (innerCount != -1) {
return innerCount;
}

// Our cache won't have an accurate count if there are deletions
if (context.reader().hasDeletions()) {
return -1;
return in.count(context);
}

// Otherwise check if the count is in the cache
Expand All @@ -834,24 +828,24 @@ public int count(LeafReaderContext context) throws IOException {

if (in.isCacheable(context) == false) {
// this segment is not suitable for caching
return -1;
return in.count(context);
}

// Short-circuit: Check whether this segment is eligible for caching
// before we take a lock because of #get
if (shouldCache(context) == false) {
return -1;
return in.count(context);
}

final IndexReader.CacheHelper cacheHelper = context.reader().getCoreCacheHelper();
if (cacheHelper == null) {
// this reader has no cacheHelper
return -1;
return in.count(context);
}

// If the lock is already busy, prefer using the uncached version than waiting
if (readLock.tryLock() == false) {
return -1;
return in.count(context);
}

CacheAndCount cached;
Expand All @@ -860,11 +854,12 @@ public int count(LeafReaderContext context) throws IOException {
} finally {
readLock.unlock();
}
if (cached == null) {
// Not cached
return -1;
if (cached != null) {
// cached
return cached.count();
}
return cached.count();
// Not cached, check if the wrapped weight can count quickly then use that
return in.count(context);
}

@Override
Expand Down

0 comments on commit 97ca2c4

Please sign in to comment.