You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2024/07/10 11:35:04 UTC

(lucene) branch branch_9x updated: GITHUB#13175: Stop double-checking priority queue inserts in some FacetCount classes (#13488)

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

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


The following commit(s) were added to refs/heads/branch_9x by this push:
     new b175c90bf59 GITHUB#13175: Stop double-checking priority queue inserts in some FacetCount classes (#13488)
b175c90bf59 is described below

commit b175c90bf596a42fd7d9babaa2b0984e618a04ae
Author: Jakub Slowinski <32...@users.noreply.github.com>
AuthorDate: Tue Jul 9 15:25:02 2024 +0100

    GITHUB#13175: Stop double-checking priority queue inserts in some FacetCount classes (#13488)
    
    * GITHUB#13175: Stop double-checking priority queue inserts
    
    Removing 2 cases of bottomX optimizations where insertWithOverflow already handles the check.
    
    Closes #13175
    
    * Update CHANGES.txt
    
    ---------
    
    Co-authored-by: Jakub Slowinski <js...@amazon.com>
---
 lucene/CHANGES.txt                                 |  2 +
 .../lucene/facet/StringValueFacetCounts.java       | 48 ++++++++--------------
 .../AbstractSortedSetDocValueFacetCounts.java      | 28 +++++--------
 3 files changed, 29 insertions(+), 49 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index de33de5dfa8..70191a6b4fc 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -38,6 +38,8 @@ Optimizations
 
 * GITHUB#12941: Don't preserve auxiliary buffer contents in LSBRadixSorter if it grows. (Stefan Vodita)
 
+* GITHUB#13175: Stop double-checking priority queue inserts in some FacetCount classes. (Jakub Slowinski)
+
 Changes in runtime behavior
 ---------------------
 
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
index 8830a2ae4ec..e5c8152fc5f 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
@@ -180,8 +180,6 @@ public class StringValueFacetCounts extends Facets {
     topN = Math.min(topN, cardinality);
     TopOrdAndIntNumberQueue q = null;
     TopOrdAndIntNumberQueue.OrdAndInt reuse = null;
-    int bottomCount = 0;
-    int bottomOrd = Integer.MAX_VALUE;
     int childCount = 0; // total number of labels with non-zero count
 
     if (sparseCounts != null) {
@@ -189,7 +187,22 @@ public class StringValueFacetCounts extends Facets {
         childCount++; // every count in sparseValues should be non-zero
         int ord = sparseCount.key;
         int count = sparseCount.value;
-        if (count > bottomCount || (count == bottomCount && ord < bottomOrd)) {
+        if (q == null) {
+          // Lazy init for sparse case:
+          q = new TopOrdAndIntNumberQueue(topN);
+        }
+        if (reuse == null) {
+          reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.newOrdAndValue();
+        }
+        reuse.ord = ord;
+        reuse.value = count;
+        reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.insertWithOverflow(reuse);
+      }
+    } else if (denseCounts != null) {
+      for (int i = 0; i < denseCounts.length; i++) {
+        int count = denseCounts[i];
+        if (count != 0) {
+          childCount++;
           if (q == null) {
             // Lazy init for sparse case:
             q = new TopOrdAndIntNumberQueue(topN);
@@ -197,36 +210,9 @@ public class StringValueFacetCounts extends Facets {
           if (reuse == null) {
             reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.newOrdAndValue();
           }
-          reuse.ord = ord;
+          reuse.ord = i;
           reuse.value = count;
           reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.insertWithOverflow(reuse);
-          if (q.size() == topN) {
-            bottomCount = ((TopOrdAndIntNumberQueue.OrdAndInt) q.top()).value;
-            bottomOrd = q.top().ord;
-          }
-        }
-      }
-    } else if (denseCounts != null) {
-      for (int i = 0; i < denseCounts.length; i++) {
-        int count = denseCounts[i];
-        if (count != 0) {
-          childCount++;
-          if (count > bottomCount || (count == bottomCount && i < bottomOrd)) {
-            if (q == null) {
-              // Lazy init for sparse case:
-              q = new TopOrdAndIntNumberQueue(topN);
-            }
-            if (reuse == null) {
-              reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.newOrdAndValue();
-            }
-            reuse.ord = i;
-            reuse.value = count;
-            reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.insertWithOverflow(reuse);
-            if (q.size() == topN) {
-              bottomCount = ((TopOrdAndIntNumberQueue.OrdAndInt) q.top()).value;
-              bottomOrd = q.top().ord;
-            }
-          }
         }
       }
     }
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java
index ae3db329d26..d428fa6a632 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java
@@ -323,8 +323,6 @@ abstract class AbstractSortedSetDocValueFacetCounts extends Facets {
   private TopChildrenForPath computeTopChildren(
       PrimitiveIterator.OfInt childOrds, int topN, DimConfig dimConfig, int pathOrd) {
     TopOrdAndIntNumberQueue q = null;
-    int bottomCount = 0;
-    int bottomOrd = Integer.MAX_VALUE;
     int pathCount = 0;
     int childCount = 0;
 
@@ -335,23 +333,17 @@ abstract class AbstractSortedSetDocValueFacetCounts extends Facets {
       if (count > 0) {
         pathCount += count;
         childCount++;
-        if (count > bottomCount || (count == bottomCount && ord < bottomOrd)) {
-          if (q == null) {
-            // Lazy init, so we don't create this for the
-            // sparse case unnecessarily
-            q = new TopOrdAndIntNumberQueue(topN);
-          }
-          if (reuse == null) {
-            reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.newOrdAndValue();
-          }
-          reuse.ord = ord;
-          reuse.value = count;
-          reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.insertWithOverflow(reuse);
-          if (q.size() == topN) {
-            bottomCount = ((TopOrdAndIntNumberQueue.OrdAndInt) q.top()).value;
-            bottomOrd = q.top().ord;
-          }
+        if (q == null) {
+          // Lazy init, so we don't create this for the
+          // sparse case unnecessarily
+          q = new TopOrdAndIntNumberQueue(topN);
+        }
+        if (reuse == null) {
+          reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.newOrdAndValue();
         }
+        reuse.ord = ord;
+        reuse.value = count;
+        reuse = (TopOrdAndIntNumberQueue.OrdAndInt) q.insertWithOverflow(reuse);
       }
     }