Skip to content

Commit 15b8d54

Browse files
optimise prefix sums in Lucene104PostingsReader (#15175)
1 parent 18c73e0 commit 15b8d54

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104PostingsReader.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ public void init(IndexInput termsIn, SegmentReadState state) throws IOException
190190
}
191191
}
192192

193-
static void prefixSum(int[] buffer, int count, long base) {
194-
buffer[0] += base;
195-
for (int i = 1; i < count; ++i) {
196-
buffer[i] += buffer[i - 1];
193+
static void prefixSum(int[] buffer, int count, int base) {
194+
int sum = base;
195+
for (int i = 0; i < count; ++i) {
196+
sum += buffer[i];
197+
buffer[i] = sum;
197198
}
198199
}
199200

@@ -593,11 +594,7 @@ private void refillFullBlock() throws IOException {
593594
if (bitsPerValue > 0) {
594595
// block is encoded as 256 packed integers that record the delta between doc IDs
595596
forUtil.decode(bitsPerValue, docInUtil, docBuffer);
596-
int sum = prevDocID;
597-
for (int i = 0; i < BLOCK_SIZE; ++i) {
598-
sum += docBuffer[i];
599-
docBuffer[i] = sum;
600-
}
597+
prefixSum(docBuffer, BLOCK_SIZE, prevDocID);
601598
encoding = DeltaEncoding.PACKED;
602599
} else {
603600
// block is encoded as a bit set
@@ -619,9 +616,7 @@ private void refillFullBlock() throws IOException {
619616
for (int i = 0; i < numLongs - 1; ++i) {
620617
docCumulativeWordPopCounts[i] = Long.bitCount(docBitSet.getBits()[i]);
621618
}
622-
for (int i = 1; i < numLongs - 1; ++i) {
623-
docCumulativeWordPopCounts[i] += docCumulativeWordPopCounts[i - 1];
624-
}
619+
prefixSum(docCumulativeWordPopCounts, numLongs - 1, 0);
625620
docCumulativeWordPopCounts[numLongs - 1] = BLOCK_SIZE;
626621
assert docCumulativeWordPopCounts[numLongs - 2]
627622
+ Long.bitCount(docBitSet.getBits()[numLongs - 1])

0 commit comments

Comments
 (0)