Skip to content

Apache Lucene open-source search software

PostingsDecodingUtil: interchange loops to enable better memory access and SIMD vectorisation (#15110)

Ramakrishna Chilakaeb876b618da5
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index bc5fd58775d..1ef3da4985c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -154,6 +154,8 @@ New Features
* GITHUB#14729: Support for Re-Ranking Queries using Late Interaction Model Multi-Vectors. (Vigya Sharma, Jim Ferenczi)
+* GITHUB#15110: PostingsDecodingUtil: interchange loops to enable better memory access and SIMD vectorisation. (Ramakrishna Chilaka)
+
Improvements
---------------------
* GITHUB#14458: Add an IndexDeletion policy that retains the last N commits. (Owais Kazi)
diff --git a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/PostingIndexInputBenchmark.java b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/PostingIndexInputBenchmark.java
index 0881d0f6bf9..c72809da847 100644
--- a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/PostingIndexInputBenchmark.java
+++ b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/PostingIndexInputBenchmark.java
@@ -105,4 +105,20 @@ public class PostingIndexInputBenchmark {
postingIn.decodeAndPrefixSum(bpv, 100, values);
bh.consume(values);
}
+
+ @Benchmark
+ @Fork(jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
+ public void decodeVector(Blackhole bh) throws IOException {
+ in.seek(3); // random unaligned offset
+ postingIn.decode(bpv, values);
+ bh.consume(values);
+ }
+
+ @Benchmark
+ @Fork(jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
+ public void decodeAndPrefixSumVector(Blackhole bh) throws IOException {
+ in.seek(3); // random unaligned offset
+ postingIn.decodeAndPrefixSum(bpv, 100, values);
+ bh.consume(values);
+ }
}
diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/PostingDecodingUtil.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/PostingDecodingUtil.java
index e45ce55bbc5..b66600eee35 100644
--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/PostingDecodingUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/PostingDecodingUtil.java
@@ -44,14 +44,21 @@ public class PostingDecodingUtil {
public void splitInts(
int count, int[] b, int bShift, int dec, int bMask, int[] c, int cIndex, int cMask)
throws IOException {
- // Default implementation, which takes advantage of the C2 compiler's loop unrolling and
- // auto-vectorization.
in.readInts(c, cIndex, count);
- int maxIter = (bShift - 1) / dec;
- for (int i = 0; i < count; ++i) {
- for (int j = 0; j <= maxIter; ++j) {
- b[count * j + i] = (c[cIndex + i] >>> (bShift - j * dec)) & bMask;
+ final int maxIter = (bShift - 1) / dec;
+
+ // Process each shift level across all elements (better for vectorization)
+ for (int j = 0; j <= maxIter; ++j) {
+ final int shift = bShift - j * dec;
+ final int bOffset = count * j;
+ // Vectorizable loop: contiguous memory access with simple operations
+ for (int i = 0; i < count; ++i) {
+ b[bOffset + i] = (c[cIndex + i] >>> shift) & bMask;
}
+ }
+
+ // Apply mask to c array (vectorizable)
+ for (int i = 0; i < count; ++i) {
c[cIndex + i] &= cMask;
}
}