You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2025/01/06 09:31:40 UTC

(lucene) branch main updated: Misc cleanups to TopScoreDocCollector (#13935)

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

abr 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 d92d0451738 Misc cleanups to TopScoreDocCollector (#13935)
d92d0451738 is described below

commit d92d045173826503469ad2eb13658d6db7d33e13
Author: Armin Braun <me...@obrown.io>
AuthorDate: Mon Jan 6 10:31:33 2025 +0100

    Misc cleanups to TopScoreDocCollector (#13935)
    
    We can be a little less verbose for one big conditional and
    optimize a few obvious spots in the logic.
---
 .../java/org/apache/lucene/search/HitQueue.java    | 24 ++++++++++------------
 .../org/apache/lucene/search/TopDocsCollector.java |  2 +-
 2 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/search/HitQueue.java b/lucene/core/src/java/org/apache/lucene/search/HitQueue.java
index dc2a7e5bff5..edb7d518e7a 100644
--- a/lucene/core/src/java/org/apache/lucene/search/HitQueue.java
+++ b/lucene/core/src/java/org/apache/lucene/search/HitQueue.java
@@ -62,24 +62,22 @@ public final class HitQueue extends PriorityQueue<ScoreDoc> {
   public HitQueue(int size, boolean prePopulate) {
     super(
         size,
-        () -> {
-          if (prePopulate) {
-            // Always set the doc Id to MAX_VALUE so that it won't be favored by
-            // lessThan. This generally should not happen since if score is not NEG_INF,
-            // TopScoreDocCollector will always add the object to the queue.
-            return new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY);
-          } else {
-            return null;
-          }
-        });
+        prePopulate
+            ? () -> {
+              // Always set the doc Id to MAX_VALUE so that it won't be favored by
+              // lessThan. This generally should not happen since if score is not NEG_INF,
+              // TopScoreDocCollector will always add the object to the queue.
+              return new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY);
+            }
+            : () -> null);
   }
 
   @Override
   protected final boolean lessThan(ScoreDoc hitA, ScoreDoc hitB) {
-    if (hitA.score == hitB.score) {
+    int cmp = Float.compare(hitA.score, hitB.score);
+    if (cmp == 0) {
       return hitA.doc > hitB.doc;
-    } else {
-      return hitA.score < hitB.score;
     }
+    return cmp < 0;
   }
 }
diff --git a/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java b/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
index 62ec4e1a3d4..3cb4024fe49 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
@@ -87,7 +87,7 @@ public abstract class TopDocsCollector<T extends ScoreDoc> implements Collector
     // In case pq was populated with sentinel values, there might be less
     // results than pq.size(). Therefore return all results until either
     // pq.size() or totalHits.
-    return totalHits < pq.size() ? totalHits : pq.size();
+    return Math.min(totalHits, pq.size());
   }
 
   /** Returns the top docs that were collected by this collector. */