Skip to content

Commit

Permalink
Improve skipping of positions.
Browse files Browse the repository at this point in the history
jpountz committed Jan 25, 2024
1 parent 6b45d4a commit 045c361
Showing 1 changed file with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -763,11 +763,7 @@ public int nextPosition() throws IOException {
return -1;
}
if (skippedPositions > 0) {
assert posIndex == -1;
for (long i = 0; i < skippedPositions; ++i) {
readPosition();
}
resetProxData();
skipPositions(skippedPositions);
skippedPositions = 0;
}
if (++posIndex >= freq) {
@@ -777,6 +773,51 @@ public int nextPosition() throws IOException {
return pos;
}

private void skipPositions(long skippedPositions) throws IOException {
assert skippedPositions > 0;
assert posIndex == -1;

int p = (int) (posIndexInBlock & (ForUtil.BLOCK_SIZE - 1));
if (p != 0) {
// Read positions from the current block until either the end of the block, or skippedPositions have been skipped
final int remainingPositionsInCurrentBlock = ForUtil.BLOCK_SIZE - p;
final int toSkip = (int) Math.min(skippedPositions, remainingPositionsInCurrentBlock);
skippedPositions -= toSkip;
for (int i = 0; i < toSkip; ++i) {
readPosition();
}
}

if (skippedPositions > 0) {
assert (posIndexInBlock & (ForUtil.BLOCK_SIZE - 1)) == 0;

while (skippedPositions >= ForUtil.BLOCK_SIZE) {
// We can skip an entire block of positions
pforUtil.skip(prox);
if (options.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0) {
pforUtil.skip(prox);
pforUtil.skip(prox);
}
if (hasPayloads) {
pforUtil.decode(prox, payloadLengthBuffer);
long lengthSum = 0;
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
lengthSum += Math.max(1, payloadLengthBuffer[i]) - 1;
}
prox.skipBytes(lengthSum);
}
posIndexInBlock += ForUtil.BLOCK_SIZE;
skippedPositions -= ForUtil.BLOCK_SIZE;
}

for (long i = 0; i < skippedPositions; ++i) {
readPosition();
}
}

resetProxData();
}

private void readPosition() throws IOException {
if (posIndexInBlock >= numPositionsInBlock) {
throw new IllegalStateException();

0 comments on commit 045c361

Please sign in to comment.