Skip to content

Apache Lucene open-source search software

Speedup MaxScoreCache.computeMaxScore (#13865)

Armin Braun0fb7f9f89db7
diff --git a/lucene/core/src/java/org/apache/lucene/index/Impacts.java b/lucene/core/src/java/org/apache/lucene/index/Impacts.java
index 35e8cca5c70..e366b6f6b0b 100644
--- a/lucene/core/src/java/org/apache/lucene/index/Impacts.java
+++ b/lucene/core/src/java/org/apache/lucene/index/Impacts.java
@@ -40,7 +40,8 @@ public abstract class Impacts {
/**
* Return impacts on the given level. These impacts are sorted by increasing frequency and
* increasing unsigned norm, and only valid until the doc ID returned by {@link
- * #getDocIdUpTo(int)} for the same level, included. The returned list is never empty. NOTE: There
+ * #getDocIdUpTo(int)} for the same level, included. The returned list is never empty and should
+ * implement {@link java.util.RandomAccess} if it contains more than a single element. NOTE: There
* is no guarantee that these impacts actually appear in postings, only that they trigger scores
* that are greater than or equal to the impacts that actually appear in postings.
*/
diff --git a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
index 2d38370e86a..a449f675daa 100644
--- a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java
@@ -71,7 +71,9 @@ public final class MaxScoreCache {
private float computeMaxScore(List<Impact> impacts) {
float maxScore = 0;
- for (Impact impact : impacts) {
+ var scorer = this.scorer;
+ for (int i = 0, length = impacts.size(); i < length; i++) {
+ Impact impact = impacts.get(i);
maxScore = Math.max(scorer.score(impact.freq, impact.norm), maxScore);
}
return maxScore;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java
index 50300dc30bc..ae6e8813890 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
+import java.util.RandomAccess;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValuesSkipIndexType;
@@ -700,7 +701,10 @@ public class AssertingLeafReader extends FilterLeafReader {
public List<Impact> getImpacts(int level) {
assert validFor == Math.max(impactsEnum.docID(), impactsEnum.lastShallowTarget)
: "Cannot reuse impacts after advancing the iterator";
- return in.getImpacts(level);
+ List<Impact> impacts = in.getImpacts(level);
+ assert impacts.size() <= 1 || impacts instanceof RandomAccess
+ : "impact lists longer than 1 should implement RandomAccess but saw impacts = " + impacts;
+ return impacts;
}
}