Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up IndexedDISI Sparse #AdvanceExactWithinBlock for tiny step advance #12324

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,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#12286 Toposort use iterator to avoid stackoverflow. (Tang Donghai)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ public static RandomAccessInput createJumpTable(

// SPARSE variables
boolean exists;
int nextExistDocInBlock = -1;
zhaih marked this conversation as resolved.
Show resolved Hide resolved

// DENSE variables
long word;
Expand Down Expand Up @@ -495,6 +496,7 @@ private void readBlockHeader() throws IOException {
if (numValues <= MAX_ARRAY_LENGTH) {
method = Method.SPARSE;
blockEnd = slice.getFilePointer() + (numValues << 1);
nextExistDocInBlock = -1;
} else if (numValues == 65536) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to BLOCK_SIZE just by the way?

Copy link
Contributor Author

@gf2121 gf2121 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure i've got your point. Do you mean change to nextExistDocInBlock = BLOCK_SIZE; ?
I think that will make SPARSE$advanceExact always return false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I (perhaps github webpage as well :)) didn't make it clear: I want to change 65536 to BLOCK_SIZE.
(Actually later on I found there's several more 65536 in this file, would you like to do a batch replacement?)

method = Method.ALL;
blockEnd = slice.getFilePointer();
Expand Down Expand Up @@ -550,32 +552,41 @@ boolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {
if (doc >= targetInBlock) {
disi.doc = disi.block | doc;
disi.exists = true;
disi.nextExistDocInBlock = doc;
return true;
}
}
disi.nextExistDocInBlock = Integer.MAX_VALUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I actually wonder whether we really need to set it to MAX_VALUE here (and below) since the next advance within the same block would be prevented by disi.index anyway. But set it to MAX_VALUE does no harm as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes! disi.index should be enough :)

return false;
}

@Override
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);
break;
disi.exists = false;
return false;
}
disi.exists = true;
return true;
}
}
disi.nextExistDocInBlock = Integer.MAX_VALUE;
disi.exists = false;
return false;
}
Expand Down