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

This commit is contained in:
Luca Cavanna 2019-06-18 10:29:51 +02:00 committed by Adrien Grand
parent fb6e28d9f1
commit 4fd09eb3e3
2 changed files with 14 additions and 4 deletions

View File

@ -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

View File

@ -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