Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-8796: Use exponential search in IntArrayDocIdSetIterator#advance (#667)

Luca Cavanna4fd09eb3e386
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index b85ddf0bada..122358dcc14 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -103,6 +103,11 @@ Improvements
* LUCENE-8845: Allow Intervals.prefix() and Intervals.wildcard() to specify
their maximum allowed expansions (Alan Woodward)
+Optimizations
+
+* LUCENE-8796: Use exponential search instead of binary search in
+ IntArrayDocIdSet#advance method (Luca Cavanna via Adrien Grand)
+
Test Framework
* LUCENE-8825: CheckHits now display the shard index in case of mismatch
diff --git a/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.java b/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.java
index e82956cae8c..c3ed8cae556 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.java
@@ -52,7 +52,7 @@ final class IntArrayDocIdSet extends DocIdSet {
private final int[] docs;
private final int length;
- private int i = -1;
+ private int i = 0;
private int doc = -1;
IntArrayDocIdSetIterator(int[] docs, int length) {
@@ -67,16 +67,21 @@ final class IntArrayDocIdSet extends DocIdSet {
@Override
public int nextDoc() throws IOException {
- return doc = docs[++i];
+ return doc = docs[i++];
}
@Override
public int advance(int target) throws IOException {
- i = Arrays.binarySearch(docs, i + 1, length, target);
+ int bound = 1;
+ //given that we use this for small arrays only, this is very unlikely to overflow
+ while(i + bound < length && docs[i + bound] < target) {
+ bound *= 2;
+ }
+ i = Arrays.binarySearch(docs, i + bound / 2, Math.min(i + bound + 1, length), target);
if (i < 0) {
i = -1 - i;
}
- return doc = docs[i];
+ return doc = docs[i++];
}
@Override