From 2a0fa7932ac83ea174b6028508fe758e7793d7e5 Mon Sep 17 00:00:00 2001 From: Evan Darke Date: Tue, 24 Nov 2026 17:32:23 -0800 Subject: [PATCH 1/3] initial fix --- .../lucene/search/ReqExclBulkScorer.java | 101 +++++++++++------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java index 179265aee065..6b3cc1e9e60f 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java @@ -26,64 +26,89 @@ final class ReqExclBulkScorer extends BulkScorer { private final BulkScorer req; private final DocIdSetIterator exclApproximation; private final TwoPhaseIterator exclTwoPhase; - private final WindowBits windowBits = new WindowBits(); + private final WindowBits windowBits; ReqExclBulkScorer(BulkScorer req, Scorer excl) { - this.req = req; - this.exclTwoPhase = excl.twoPhaseIterator(); - if (exclTwoPhase != null) { - this.exclApproximation = exclTwoPhase.approximation(); - } else { - this.exclApproximation = excl.iterator(); - } + this(req, excl.twoPhaseIterator(), excl); } ReqExclBulkScorer(BulkScorer req, DocIdSetIterator excl) { - this.req = req; - this.exclTwoPhase = null; - this.exclApproximation = excl; + this(req, null, excl); } ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl) { + this(req, excl, excl.approximation()); + } + + private ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator twoPhaseExcl, Scorer fallbackExcl) { + this( + req, + twoPhaseExcl, + twoPhaseExcl != null ? twoPhaseExcl.approximation() : fallbackExcl.iterator()); + } + + private ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl, DocIdSetIterator approximation) { this.req = req; this.exclTwoPhase = excl; - this.exclApproximation = excl.approximation(); + this.exclApproximation = approximation; + // Bulk scoring is disabled when using a two phase iterator so no need to allocate window + this.windowBits = exclTwoPhase != null ? null : new WindowBits(); } @Override public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException { - FixedBitSet windowMask = windowBits.windowMask; - int upTo = min; - while (upTo < max) { - int windowMin = upTo; - int windowMax = (int) Math.min((long) max, (long) windowMin + WINDOW_SIZE); - - if (exclApproximation.docID() < windowMin) { - exclApproximation.advance(windowMin); + if (exclTwoPhase != null) { + // TwoPhase exclusions have a cheap approximation and expensive refinement, so we avoid + // refining exclusions unless we have a definite required match. + int exclDoc = exclApproximation.docID(); + + while (upTo < max) { + if (exclDoc < upTo) { + exclDoc = exclApproximation.advance(upTo); + } + if (exclDoc == upTo) { + if (exclTwoPhase.matches()) { + // upTo is excluded (matches() just confirmed it), so skip the whole run of consecutive + // excluded docs at once. The default TwoPhaseIterator#docIDRunEnd() conservatively + // returns the current doc, so clamp to at least upTo+1 to guarantee progress when the + // run end is not overridden. + upTo = Math.max(upTo + 1, Math.min(exclTwoPhase.docIDRunEnd(), max)); + } + exclDoc = exclApproximation.nextDoc(); + } else { + upTo = req.score(collector, acceptDocs, upTo, Math.min(exclDoc, max)); + } } + } else { + FixedBitSet windowMask = windowBits.windowMask; - windowMask.clear(); - if (exclTwoPhase == null) { + while (upTo < max) { + int windowMin = upTo; + int windowMax = (int) Math.min((long) max, (long) windowMin + WINDOW_SIZE); + + if (exclApproximation.docID() < windowMin) { + exclApproximation.advance(windowMin); + } + + windowMask.clear(); exclApproximation.intoBitSet(windowMax, windowMask, windowMin); - } else { - exclTwoPhase.intoBitSet(windowMax, windowMask, windowMin); - } - int windowLength = windowMax - windowMin; - int validWindowLength = - acceptDocs == null - ? windowLength - : Math.max(0, Math.min(windowLength, acceptDocs.length() - windowMin)); - windowMask.flip(0, validWindowLength); - if (validWindowLength < windowLength) { - windowMask.clear(validWindowLength, windowLength); - } - if (acceptDocs != null) { - acceptDocs.applyMask(windowMask, windowMin); + int windowLength = windowMax - windowMin; + int validWindowLength = + acceptDocs == null + ? windowLength + : Math.max(0, Math.min(windowLength, acceptDocs.length() - windowMin)); + windowMask.flip(0, validWindowLength); + if (validWindowLength < windowLength) { + windowMask.clear(validWindowLength, windowLength); + } + if (acceptDocs != null) { + acceptDocs.applyMask(windowMask, windowMin); + } + windowBits.reset(windowMin, windowMax, acceptDocs == null ? max : acceptDocs.length()); + upTo = req.score(collector, windowBits, windowMin, windowMax); } - windowBits.reset(windowMin, windowMax, acceptDocs == null ? max : acceptDocs.length()); - upTo = req.score(collector, windowBits, windowMin, windowMax); } if (upTo == max) { From f4730f4ac6ccfb42136931335d1cb4517ef24c99 Mon Sep 17 00:00:00 2001 From: Evan Darke Date: Wed, 25 Nov 2026 14:47:15 -0800 Subject: [PATCH 2/3] Attempt cost estimate for TPI without bulk optimization --- .../lucene/search/BooleanScorerSupplier.java | 2 +- .../lucene/search/DocValuesRangeIterator.java | 5 + .../lucene/search/ReqExclBulkScorer.java | 150 +++++++++++------- .../apache/lucene/search/ReqExclScorer.java | 2 +- .../lucene/search/TwoPhaseIterator.java | 14 +- .../lucene/search/TestReqExclBulkScorer.java | 11 +- 6 files changed, 119 insertions(+), 65 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java index 55d2be36d3ec..1fcb26212ad4 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java @@ -262,7 +262,7 @@ BulkScorer booleanScorer() throws IOException { ? prohibited.get(0) : new DisjunctionSumScorer( prohibited, ScoreMode.COMPLETE_NO_SCORES, positiveScorerCost); - return new ReqExclBulkScorer(positiveScorer, prohibitedScorer); + return new ReqExclBulkScorer(positiveScorer, prohibitedScorer, maxDoc); } } diff --git a/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeIterator.java b/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeIterator.java index 8c62d54a6f05..972fb220b291 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeIterator.java +++ b/lucene/core/src/java/org/apache/lucene/search/DocValuesRangeIterator.java @@ -346,6 +346,11 @@ public final int docIDRunEnd() throws IOException { }; } + @Override + public boolean isBulkOptimized() { + return true; + } + @Override public final void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException { while (blockIterator.docID() < upTo) { diff --git a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java index 6b3cc1e9e60f..2e7493950d68 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java @@ -28,91 +28,125 @@ final class ReqExclBulkScorer extends BulkScorer { private final TwoPhaseIterator exclTwoPhase; private final WindowBits windowBits; - ReqExclBulkScorer(BulkScorer req, Scorer excl) { - this(req, excl.twoPhaseIterator(), excl); + ReqExclBulkScorer(BulkScorer req, Scorer excl, int maxDoc) { + this(req, excl.twoPhaseIterator(), excl, maxDoc); } - ReqExclBulkScorer(BulkScorer req, DocIdSetIterator excl) { - this(req, null, excl); + ReqExclBulkScorer(BulkScorer req, DocIdSetIterator excl, int maxDoc) { + this(req, null, excl, maxDoc); } - ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl) { - this(req, excl, excl.approximation()); + ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl, int maxDoc) { + this(req, excl, excl.approximation(), maxDoc); } - private ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator twoPhaseExcl, Scorer fallbackExcl) { + private ReqExclBulkScorer( + BulkScorer req, TwoPhaseIterator twoPhaseExcl, Scorer fallbackExcl, int maxDoc) { this( req, twoPhaseExcl, - twoPhaseExcl != null ? twoPhaseExcl.approximation() : fallbackExcl.iterator()); + twoPhaseExcl != null ? twoPhaseExcl.approximation() : fallbackExcl.iterator(), + maxDoc); } - private ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl, DocIdSetIterator approximation) { + private ReqExclBulkScorer( + BulkScorer req, TwoPhaseIterator excl, DocIdSetIterator approximation, int maxDoc) { this.req = req; this.exclTwoPhase = excl; this.exclApproximation = approximation; - // Bulk scoring is disabled when using a two phase iterator so no need to allocate window - this.windowBits = exclTwoPhase != null ? null : new WindowBits(); + this.windowBits = shouldUseWindow(req, excl, maxDoc) ? new WindowBits() : null; + } + + private static boolean shouldUseWindow(BulkScorer req, TwoPhaseIterator excl, int maxDoc) { + if (excl == null) { + return true; + } + if (excl.isBulkOptimized() == true) { + return true; + } + return shouldUseWindow(req.cost(), excl.approximation().cost(), excl.matchCost(), maxDoc); + } + + static boolean shouldUseWindow(long reqCost, long exclApproxCost, float matchCost, int maxDoc) { + double reqFraction = (double) Math.min(maxDoc, reqCost) / maxDoc; + double exclFraction = (double) Math.min(maxDoc, exclApproxCost) / maxDoc; + + double windowCost = exclFraction * (ReqExclScorer.ADVANCE_COST + matchCost * (1 - reqFraction)); + double switches = 2 * exclFraction * reqFraction / (exclFraction + reqFraction); + double leapFrogCost = switches * 2 * ReqExclScorer.ADVANCE_COST; + return windowCost < leapFrogCost; } @Override public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException { - int upTo = min; + int upTo = + windowBits == null + ? scoreLeapFrog(collector, acceptDocs, min, max) + : scoreWindowed(collector, acceptDocs, min, max); + if (upTo == max) { + upTo = req.score(collector, acceptDocs, max, max); + } + return upTo; + } - if (exclTwoPhase != null) { - // TwoPhase exclusions have a cheap approximation and expensive refinement, so we avoid - // refining exclusions unless we have a definite required match. - int exclDoc = exclApproximation.docID(); + private int scoreLeapFrog(LeafCollector collector, Bits acceptDocs, int min, int max) + throws IOException { + int upTo = min; + int exclDoc = exclApproximation.docID(); - while (upTo < max) { - if (exclDoc < upTo) { - exclDoc = exclApproximation.advance(upTo); - } - if (exclDoc == upTo) { - if (exclTwoPhase.matches()) { - // upTo is excluded (matches() just confirmed it), so skip the whole run of consecutive - // excluded docs at once. The default TwoPhaseIterator#docIDRunEnd() conservatively - // returns the current doc, so clamp to at least upTo+1 to guarantee progress when the - // run end is not overridden. - upTo = Math.max(upTo + 1, Math.min(exclTwoPhase.docIDRunEnd(), max)); - } - exclDoc = exclApproximation.nextDoc(); - } else { - upTo = req.score(collector, acceptDocs, upTo, Math.min(exclDoc, max)); + while (upTo < max) { + if (exclDoc < upTo) { + exclDoc = exclApproximation.advance(upTo); + } + if (exclDoc == upTo) { + if (exclTwoPhase.matches()) { + // upTo is excluded (matches() just confirmed it), so skip the whole run of consecutive + // excluded docs at once. The default TwoPhaseIterator#docIDRunEnd() conservatively + // returns the current doc, so clamp to at least upTo+1 to guarantee progress when the + // run end is not overridden. + upTo = Math.max(upTo + 1, Math.min(exclTwoPhase.docIDRunEnd(), max)); } + exclDoc = exclApproximation.nextDoc(); + } else { + upTo = req.score(collector, acceptDocs, upTo, Math.min(exclDoc, max)); } - } else { - FixedBitSet windowMask = windowBits.windowMask; + } + return upTo; + } - while (upTo < max) { - int windowMin = upTo; - int windowMax = (int) Math.min((long) max, (long) windowMin + WINDOW_SIZE); + private int scoreWindowed(LeafCollector collector, Bits acceptDocs, int min, int max) + throws IOException { + FixedBitSet windowMask = windowBits.windowMask; + int upTo = min; - if (exclApproximation.docID() < windowMin) { - exclApproximation.advance(windowMin); - } + while (upTo < max) { + int windowMin = upTo; + int windowMax = (int) Math.min(max, (long) windowMin + WINDOW_SIZE); - windowMask.clear(); - exclApproximation.intoBitSet(windowMax, windowMask, windowMin); - int windowLength = windowMax - windowMin; - int validWindowLength = - acceptDocs == null - ? windowLength - : Math.max(0, Math.min(windowLength, acceptDocs.length() - windowMin)); - windowMask.flip(0, validWindowLength); - if (validWindowLength < windowLength) { - windowMask.clear(validWindowLength, windowLength); - } - if (acceptDocs != null) { - acceptDocs.applyMask(windowMask, windowMin); - } - windowBits.reset(windowMin, windowMax, acceptDocs == null ? max : acceptDocs.length()); - upTo = req.score(collector, windowBits, windowMin, windowMax); + if (exclApproximation.docID() < windowMin) { + exclApproximation.advance(windowMin); } - } - if (upTo == max) { - upTo = req.score(collector, acceptDocs, max, max); + windowMask.clear(); + if (exclTwoPhase == null) { + exclApproximation.intoBitSet(windowMax, windowMask, windowMin); + } else { + exclTwoPhase.intoBitSet(windowMax, windowMask, windowMin); + } + int windowLength = windowMax - windowMin; + int validWindowLength = + acceptDocs == null + ? windowLength + : Math.max(0, Math.min(windowLength, acceptDocs.length() - windowMin)); + windowMask.flip(0, validWindowLength); + if (validWindowLength < windowLength) { + windowMask.clear(validWindowLength, windowLength); + } + if (acceptDocs != null) { + acceptDocs.applyMask(windowMask, windowMin); + } + windowBits.reset(windowMin, windowMax, acceptDocs == null ? max : acceptDocs.length()); + upTo = req.score(collector, windowBits, windowMin, windowMax); } return upTo; } diff --git a/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java index 914a1e56724f..df7616d939b2 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/ReqExclScorer.java @@ -101,7 +101,7 @@ public Collection getChildren() { * wrong, especially given that the cost of this method usually depends on how far you want to * advance, but it's probably better than nothing. */ - private static final int ADVANCE_COST = 10; + static final int ADVANCE_COST = 10; private static float matchCost( DocIdSetIterator reqApproximation, diff --git a/lucene/core/src/java/org/apache/lucene/search/TwoPhaseIterator.java b/lucene/core/src/java/org/apache/lucene/search/TwoPhaseIterator.java index 0f495ec2f0c4..b87a37ca434e 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TwoPhaseIterator.java +++ b/lucene/core/src/java/org/apache/lucene/search/TwoPhaseIterator.java @@ -142,7 +142,8 @@ public int docIDRunEnd() throws IOException { * {@link #matches()} — i.e. it is functionally identical to the leap-frog evaluation, just * writing into a bit set. Implementations that can confirm matches in bulk — for instance a * vectorized scan over a columnar block — should override it; the bit set window is then - * collected in one shot rather than one doc at a time. + * collected in one shot rather than one doc at a time. Subclasses that override the default + * implementation should consider overriding {@link #isBulkOptimized}. */ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException { DocIdSetIterator approximation = approximation(); @@ -153,6 +154,17 @@ public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOExcept } } + /** + * Returns true if this TwoPhaseIterator provides an optimized implementation of {@link + * #intoBitSet}. + * + *

This method can provide a hint to BulkScorer implementations when it comes to choosing an + * optimal lead iterator. + */ + public boolean isBulkOptimized() { + return false; + } + /** * Confirms {@link #matches()} only for the doc IDs whose bit is set in {@code bitSet} (the * document whose ID is {@code i} being stored at bit {@code i - offset}), clearing bits for docs diff --git a/lucene/core/src/test/org/apache/lucene/search/TestReqExclBulkScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestReqExclBulkScorer.java index 8c569f0b9f47..9e297f8ba342 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestReqExclBulkScorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestReqExclBulkScorer.java @@ -88,9 +88,10 @@ public long cost() { ReqExclBulkScorer reqExcl; if (twoPhase) { reqExcl = - new ReqExclBulkScorer(reqBulkScorer, new RandomTwoPhaseView(random(), excl.iterator())); + new ReqExclBulkScorer( + reqBulkScorer, new RandomTwoPhaseView(random(), excl.iterator()), maxDoc); } else { - reqExcl = new ReqExclBulkScorer(reqBulkScorer, excl.iterator()); + reqExcl = new ReqExclBulkScorer(reqBulkScorer, excl.iterator(), maxDoc); } final FixedBitSet actualMatches = new FixedBitSet(maxDoc); final FixedBitSet acceptedDocs = new FixedBitSet(maxDoc); @@ -190,7 +191,8 @@ public void testDenseScore() throws IOException { 0f, ScoreMode.COMPLETE_NO_SCORES, new BitSetIterator(required, required.cardinality())), - exclusion); + exclusion, + maxDoc); FixedBitSet actual = new FixedBitSet(maxDoc); assertEquals( DocIdSetIterator.NO_MORE_DOCS, @@ -239,7 +241,8 @@ public void testSparseScore() throws IOException { new ReqExclBulkScorer( new ConstantScoreBulkScorer( 0f, ScoreMode.COMPLETE_NO_SCORES, DocIdSetIterator.all(maxDoc)), - exclusion); + exclusion, + maxDoc); FixedBitSet collected = new FixedBitSet(maxDoc); assertEquals( DocIdSetIterator.NO_MORE_DOCS, From 22cf286935c5d02879059721b68626da937e24af Mon Sep 17 00:00:00 2001 From: Evan Darke Date: Wed, 25 Nov 2026 20:57:06 -0800 Subject: [PATCH 3/3] Add 128x penalty to leapfrog --- .../src/java/org/apache/lucene/search/ReqExclBulkScorer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java index 2e7493950d68..07c6c5ac79ed 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java @@ -74,7 +74,7 @@ static boolean shouldUseWindow(long reqCost, long exclApproxCost, float matchCos double windowCost = exclFraction * (ReqExclScorer.ADVANCE_COST + matchCost * (1 - reqFraction)); double switches = 2 * exclFraction * reqFraction / (exclFraction + reqFraction); double leapFrogCost = switches * 2 * ReqExclScorer.ADVANCE_COST; - return windowCost < leapFrogCost; + return (windowCost / 128) < leapFrogCost; } @Override