Skip to content

Commit

Permalink
Speed up IndexedDISI Sparse #AdvanceExactWithinBlock for tiny step ad…
Browse files Browse the repository at this point in the history
…vance (#12324)
  • Loading branch information
gf2121 authored Jun 13, 2023
1 parent c8e05c8 commit 30eba6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ Improvements
Optimizations
---------------------

* GITHUB#12324: Speed up sparse block advanceExact with tiny step in IndexedDISI. (Guo Feng)

* GITHUB#12270 Don't generate stacktrace in CollectionTerminatedException. (Armin Braun)

* GITHUB#12160: Concurrent rewrite for AbstractKnnVectorQuery. (Kaival Parikh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public final class IndexedDISI extends DocIdSetIterator {
private static void flush(
int block, FixedBitSet buffer, int cardinality, byte denseRankPower, IndexOutput out)
throws IOException {
assert block >= 0 && block < 65536;
assert block >= 0 && block < BLOCK_SIZE;
out.writeShort((short) block);
assert cardinality > 0 && cardinality <= 65536;
assert cardinality > 0 && cardinality <= BLOCK_SIZE;
out.writeShort((short) (cardinality - 1));
if (cardinality > MAX_ARRAY_LENGTH) {
if (cardinality != 65536) { // all docs are set
if (cardinality != BLOCK_SIZE) { // all docs are set
if (denseRankPower != -1) {
final byte[] rank = createRank(buffer, denseRankPower);
out.writeBytes(rank, rank.length);
Expand Down Expand Up @@ -418,6 +418,7 @@ public static RandomAccessInput createJumpTable(

// SPARSE variables
boolean exists;
int nextExistDocInBlock = -1;

// DENSE variables
long word;
Expand Down Expand Up @@ -495,7 +496,8 @@ private void readBlockHeader() throws IOException {
if (numValues <= MAX_ARRAY_LENGTH) {
method = Method.SPARSE;
blockEnd = slice.getFilePointer() + (numValues << 1);
} else if (numValues == 65536) {
nextExistDocInBlock = -1;
} else if (numValues == BLOCK_SIZE) {
method = Method.ALL;
blockEnd = slice.getFilePointer();
gap = block - index - 1;
Expand Down Expand Up @@ -550,6 +552,7 @@ boolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {
if (doc >= targetInBlock) {
disi.doc = disi.block | doc;
disi.exists = true;
disi.nextExistDocInBlock = doc;
return true;
}
}
Expand All @@ -560,13 +563,18 @@ boolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {
boolean advanceExactWithinBlock(IndexedDISI disi, int target) throws IOException {
final int targetInBlock = target & 0xFFFF;
// TODO: binary search
if (disi.nextExistDocInBlock > targetInBlock) {
assert !disi.exists;
return false;
}
if (target == disi.doc) {
return disi.exists;
}
for (; disi.index < disi.nextBlockIndex; ) {
int doc = Short.toUnsignedInt(disi.slice.readShort());
disi.index++;
if (doc >= targetInBlock) {
disi.nextExistDocInBlock = doc;
if (doc != targetInBlock) {
disi.index--;
disi.slice.seek(disi.slice.getFilePointer() - Short.BYTES);
Expand Down

0 comments on commit 30eba6d

Please sign in to comment.