Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
101 changes: 80 additions & 21 deletions lucene/core/src/java/org/apache/lucene/search/ReqExclBulkScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,102 @@ 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();
}
ReqExclBulkScorer(BulkScorer req, Scorer excl, int maxDoc) {
this(req, excl.twoPhaseIterator(), excl, maxDoc);
}

ReqExclBulkScorer(BulkScorer req, DocIdSetIterator excl) {
this.req = req;
this.exclTwoPhase = null;
this.exclApproximation = excl;
ReqExclBulkScorer(BulkScorer req, DocIdSetIterator excl, int maxDoc) {
this(req, null, excl, maxDoc);
}

ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl, int maxDoc) {
this(req, excl, excl.approximation(), maxDoc);
}

ReqExclBulkScorer(BulkScorer req, TwoPhaseIterator excl) {
private ReqExclBulkScorer(
BulkScorer req, TwoPhaseIterator twoPhaseExcl, Scorer fallbackExcl, int maxDoc) {
this(
req,
twoPhaseExcl,
twoPhaseExcl != null ? twoPhaseExcl.approximation() : fallbackExcl.iterator(),
maxDoc);
}

private ReqExclBulkScorer(
BulkScorer req, TwoPhaseIterator excl, DocIdSetIterator approximation, int maxDoc) {
this.req = req;
this.exclTwoPhase = excl;
this.exclApproximation = excl.approximation();
this.exclApproximation = approximation;
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 / 128) < leapFrogCost;
}

@Override
public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException {
FixedBitSet windowMask = windowBits.windowMask;
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;
}

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));
}
}
return upTo;
}

private int scoreWindowed(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);
int windowMax = (int) Math.min(max, (long) windowMin + WINDOW_SIZE);

if (exclApproximation.docID() < windowMin) {
exclApproximation.advance(windowMin);
Expand All @@ -85,10 +148,6 @@ public int score(LeafCollector collector, Bits acceptDocs, int min, int max) thr
windowBits.reset(windowMin, windowMax, acceptDocs == null ? max : acceptDocs.length());
upTo = req.score(collector, windowBits, windowMin, windowMax);
}

if (upTo == max) {
upTo = req.score(collector, acceptDocs, max, max);
}
return upTo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Collection<ChildScorable> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}.
*
* <p>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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading