Apache Lucene open-source search software
Pre-calculate minRequiredScore to speedup filterCompetitiveHits (#14827)
Samuel.G62e027603218
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 0790cb61454..d0958246ce5 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -194,6 +194,8 @@ Optimizations* GITHUB#14854: Using BatchScoreBulkScorer on CombinedFieldQuery. (Ge Song)+* GITHUB#14827: Pre-calculate minRequiredScore to speedup filterCompetitiveHits. (Ge Song, Adrien Grand)+Changes in Runtime Behavior---------------------* GITHUB#14823: Decrease TieredMergePolicy's default number of segments perdiff --git a/lucene/core/src/java/org/apache/lucene/search/ScorerUtil.java b/lucene/core/src/java/org/apache/lucene/search/ScorerUtil.javaindex 5c677d7a464..8b91b3f19ee 100644--- a/lucene/core/src/java/org/apache/lucene/search/ScorerUtil.java+++ b/lucene/core/src/java/org/apache/lucene/search/ScorerUtil.java@@ -116,6 +116,26 @@ class ScorerUtil {}}+ /**+ * Compute a minimum required score, so that (float) MathUtil.sumUpperBound(minRequiredScore ++ * maxRemainingScore, numScorers) <= minCompetitiveScore. The computed value may not be the+ * greatest value that meets this condition, which means that we may fail to filter out some docs.+ * However, this doesn't hurt correctness, it just means that these docs will be filtered out+ * later, and the extra work required to compute an optimal value would unlikely result in a+ * speedup.+ */+ static double minRequiredScore(+ double maxRemainingScore, float minCompetitiveScore, int numScorers) {+ double minRequiredScore = minCompetitiveScore - maxRemainingScore;+ // note: we want the float ulp in order to converge faster, not the double ulp+ double subtraction = Math.ulp(minCompetitiveScore);+ while ((float) MathUtil.sumUpperBound(minRequiredScore + maxRemainingScore, numScorers)+ > minCompetitiveScore) {+ minRequiredScore -= subtraction;+ }+ return minRequiredScore;+ }+/*** Filters competitive hits from the provided {@link DocAndScoreAccBuffer}.*@@ -128,15 +148,15 @@ class ScorerUtil {double maxRemainingScore,float minCompetitiveScore,int numScorers) {- if ((float) MathUtil.sumUpperBound(maxRemainingScore, numScorers) >= minCompetitiveScore) {+ double minRequiredScore = minRequiredScore(maxRemainingScore, minCompetitiveScore, numScorers);++ if (minRequiredScore <= 0) {return;}int newSize = 0;for (int i = 0; i < buffer.size; ++i) {- float maxPossibleScore =- (float) MathUtil.sumUpperBound(buffer.scores[i] + maxRemainingScore, numScorers);- if (maxPossibleScore >= minCompetitiveScore) {+ if (buffer.scores[i] >= minRequiredScore) {buffer.docs[newSize] = buffer.docs[i];buffer.scores[newSize] = buffer.scores[i];newSize++;diff --git a/lucene/core/src/test/org/apache/lucene/search/TestScorerUtil.java b/lucene/core/src/test/org/apache/lucene/search/TestScorerUtil.javaindex e96f098c221..360d797953d 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestScorerUtil.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestScorerUtil.java@@ -36,6 +36,7 @@ import org.apache.lucene.tests.util.LuceneTestCase;import org.apache.lucene.tests.util.TestUtil;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.MathUtil;import org.apache.lucene.util.SparseFixedBitSet;public class TestScorerUtil extends LuceneTestCase {@@ -93,4 +94,34 @@ public class TestScorerUtil extends LuceneTestCase {}}}++ public void testMinRequiredScore() {+ int iters = atLeast(100);+ for (int iter = 0; iter < iters; iter++) {+ double maxRemainingScore = random().nextDouble();+ float minCompetitiveScore = random().nextFloat();+ int numScorers = random().nextInt(1, 1000);++ double minRequiredScore =+ ScorerUtil.minRequiredScore(maxRemainingScore, minCompetitiveScore, numScorers);+ if (minCompetitiveScore < maxRemainingScore) {+ assertTrue(minRequiredScore <= 0);+ } else {+ assertTrue(+ (float) MathUtil.sumUpperBound(minRequiredScore + maxRemainingScore, numScorers)+ <= minCompetitiveScore);+ }++ // NOTE: we need to assert the internal while loop ends within an acceptable iterations. But+ // it seems there is no easy way to do this assertion, so the assertion below relies on the+ // internal implementation detail of ScorerUtil.minRequiredScore+ double initialMinRequiredScore = minCompetitiveScore - maxRemainingScore;+ double subtraction = Math.ulp(minCompetitiveScore);+ int expectConverge = 10;+ for (int i = 0; i < expectConverge; i++) {+ initialMinRequiredScore -= subtraction;+ }+ assertTrue(initialMinRequiredScore <= minRequiredScore);+ }+ }}