Apache Lucene open-source search software
LUCENE-8796: Use exponential search in IntArrayDocIdSetIterator#advance (#667)
Luca Cavanna4fd09eb3e386
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 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 specifytheir 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 mismatchdiff --git a/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.java b/lucene/core/src/java/org/apache/lucene/util/IntArrayDocIdSet.javaindex 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 {@Overridepublic int nextDoc() throws IOException {- return doc = docs[++i];+ return doc = docs[i++];}@Overridepublic 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