You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2024/09/10 20:45:20 UTC

(lucene) branch main updated: Reorder checks in LRUQueryCache#count (#13742)

This is an automated email from the ASF dual-hosted git repository.

jpountz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 97ca2c42e80 Reorder checks in LRUQueryCache#count (#13742)
97ca2c42e80 is described below

commit 97ca2c42e80134a56f8dae998e6cc7820f1a3cca
Author: Shubham Chaudhary <36...@users.noreply.github.com>
AuthorDate: Wed Sep 11 02:15:14 2024 +0530

    Reorder checks in LRUQueryCache#count (#13742)
---
 lucene/CHANGES.txt                                 |  2 ++
 .../org/apache/lucene/search/LRUQueryCache.java    | 25 +++++++++-------------
 2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 016ac462976..fb49750584a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -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
 ---------------------
 
diff --git a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
index 7f82e78e056..df17e41f597 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
@@ -816,15 +816,9 @@ public class LRUQueryCache implements QueryCache, Accountable {
 
     @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
@@ -834,24 +828,24 @@ public class LRUQueryCache implements QueryCache, Accountable {
 
       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;
@@ -860,11 +854,12 @@ public class LRUQueryCache implements QueryCache, Accountable {
       } 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