Apache Lucene open-source search software
Speed up prefix sums when decoding doc IDs. (#13658)
Adrien Grand299d7f6721f1
diff --git a/gradle/generation/forUtil.gradle b/gradle/generation/forUtil.gradleindex 5de1d850a02..7ef3b53fd92 100644--- a/gradle/generation/forUtil.gradle+++ b/gradle/generation/forUtil.gradle@@ -43,6 +43,31 @@ configure(project(":lucene:core")) {andThenTasks: ["spotlessJava", "spotlessJavaApply"],mustRunBefore: [ "compileJava" ]])++ task generateForDeltaUtilInternal() {+ description "Regenerate gen_ForDeltaUtil.py"+ group "generation"++ def genDir = file("src/java/org/apache/lucene/codecs/lucene912")+ def genScript = file("${genDir}/gen_ForDeltaUtil.py")+ def genOutput = file("${genDir}/ForDeltaUtil.java")++ inputs.file genScript+ outputs.file genOutput++ doLast {+ quietExec {+ workingDir genDir+ executable project.externalTool("python3")+ args = [ '-B', genScript ]+ }+ }+ }++ regenerate.dependsOn wrapWithPersistentChecksums(generateForDeltaUtilInternal, [+ andThenTasks: ["spotlessJava", "spotlessJavaApply"],+ mustRunBefore: [ "compileJava" ]+ ])}configure(project(":lucene:backward-codecs")) {diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 0653bec89de..4ffcfdd6f0d 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -357,8 +357,8 @@ Optimizations* GITHUB#13581: OnHeapHnswGraph no longer allocates a lock for every graph node (Mike Sokolov)-* GITHUB#13636: Optimizations to the decoding logic of blocks of postings.- (Adrien Grand, Uwe Schindler)+* GITHUB#13636, GITHUB#13658: Optimizations to the decoding logic of blocks of+ postings. (Adrien Grand, Uwe Schindler, Greg Miller)* GITHUB##13644: Improve NumericComparator competitive iterator logic by comparing the missing value with the topvalue even after the hit queue is full (Pan Guixin)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.javaindex 3804af0b167..4ebab2f4024 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@@ -21,6 +21,7 @@ import java.nio.file.Files;import java.nio.file.Path;import java.util.Random;import java.util.concurrent.TimeUnit;+import org.apache.lucene.codecs.lucene912.ForDeltaUtil;import org.apache.lucene.codecs.lucene912.ForUtil;import org.apache.lucene.codecs.lucene912.PostingIndexInput;import org.apache.lucene.store.Directory;@@ -59,9 +60,10 @@ public class PostingIndexInputBenchmark {private IndexInput in;private PostingIndexInput postingIn;private final ForUtil forUtil = new ForUtil();+ private final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();private final long[] values = new long[128];- @Param({"5", "6", "7", "8", "9", "10"})+ @Param({"2", "3", "4", "5", "6", "7", "8", "9", "10"})public int bpv;@Setup(Level.Trial)@@ -76,7 +78,7 @@ public class PostingIndexInputBenchmark {}}in = dir.openInput("docs", IOContext.DEFAULT);- postingIn = new PostingIndexInput(in, forUtil);+ postingIn = new PostingIndexInput(in, forUtil, forDeltaUtil);}@TearDown(Level.Trial)diff --git a/lucene/core/src/generated/checksums/generateForDeltaUtil.json b/lucene/core/src/generated/checksums/generateForDeltaUtil.jsonnew file mode 100644index 00000000000..6546e25c4be--- /dev/null+++ b/lucene/core/src/generated/checksums/generateForDeltaUtil.json@@ -0,0 +1,4 @@+{+ "lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForDeltaUtil.java": "5115b12ac31537ce31d73c0a279df92060749a3a",+ "lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForDeltaUtil.py": "db6154406e68b80d2c90116b5d0bfa9ba220762a"+}\ No newline at end of filediff --git a/lucene/core/src/generated/checksums/generateForUtil.json b/lucene/core/src/generated/checksums/generateForUtil.jsonindex e147f2c62f7..3a1c02b35bb 100644--- a/lucene/core/src/generated/checksums/generateForUtil.json+++ b/lucene/core/src/generated/checksums/generateForUtil.json@@ -1,4 +1,4 @@{- "lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.java": "cc7d40997e2d6500b79c19ff47461ed6e89d2268",- "lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.py": "ba029f2e374e66c6cf315b2c93f4efa6944dfbb8"+ "lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.java": "159e82388346fde147924d5e15ca65df4dd63b9a",+ "lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.py": "66dc8813160feae2a37d8b50474f5f9830b6cb22"}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForDeltaUtil.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForDeltaUtil.javaindex b53c18fa3f8..25601388a0f 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForDeltaUtil.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForDeltaUtil.java@@ -1,3 +1,5 @@+// This file has been automatically generated, DO NOT EDIT+/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with@@ -16,13 +18,32 @@*/package org.apache.lucene.codecs.lucene912;+import static org.apache.lucene.codecs.lucene912.ForUtil.*;+import java.io.IOException;-import org.apache.lucene.store.DataInput;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.DataOutput;+import org.apache.lucene.store.IndexInput;import org.apache.lucene.util.packed.PackedInts;-/** Utility class to encode/decode increasing sequences of 128 integers. */-final class ForDeltaUtil {+/**+ * Inspired from https://fulmicoton.com/posts/bitpacking/ Encodes multiple integers in a long to get+ * SIMD-like speedups. If bitsPerValue <= 4 then we pack 8 ints per long else if bitsPerValue+ * <= 11 we pack 4 ints per long else we pack 2 ints per long+ */+public final class ForDeltaUtil {++ private static final int ONE_BLOCK_SIZE_FOURTH = BLOCK_SIZE / 4;+ private static final int TWO_BLOCK_SIZE_FOURTHS = BLOCK_SIZE / 2;+ private static final int THREE_BLOCK_SIZE_FOURTHS = 3 * BLOCK_SIZE / 4;++ private static final int ONE_BLOCK_SIZE_EIGHT = BLOCK_SIZE / 8;+ private static final int TWO_BLOCK_SIZE_EIGHTS = BLOCK_SIZE / 4;+ private static final int THREE_BLOCK_SIZE_EIGHTS = 3 * BLOCK_SIZE / 8;+ private static final int FOUR_BLOCK_SIZE_EIGHTS = BLOCK_SIZE / 2;+ private static final int FIVE_BLOCK_SIZE_EIGHTS = 5 * BLOCK_SIZE / 8;+ private static final int SIX_BLOCK_SIZE_EIGHTS = 3 * BLOCK_SIZE / 4;+ private static final int SEVEN_BLOCK_SIZE_EIGHTS = 7 * BLOCK_SIZE / 8;// IDENTITY_PLUS_ONE[i] == i+1private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];@@ -41,12 +62,185 @@ final class ForDeltaUtil {}}- private final ForUtil forUtil;+ private static void prefixSum8(long[] arr, long base) {+ // When the number of bits per value is 4 or less, we can sum up all values in a block without+ // risking overflowing a 8-bits integer. This allows computing the prefix sum by summing up 8+ // values at once.+ innerPrefixSum8(arr);+ expand8(arr);+ final long l0 = base;+ final long l1 = l0 + arr[ONE_BLOCK_SIZE_EIGHT - 1];+ final long l2 = l1 + arr[TWO_BLOCK_SIZE_EIGHTS - 1];+ final long l3 = l2 + arr[THREE_BLOCK_SIZE_EIGHTS - 1];+ final long l4 = l3 + arr[FOUR_BLOCK_SIZE_EIGHTS - 1];+ final long l5 = l4 + arr[FIVE_BLOCK_SIZE_EIGHTS - 1];+ final long l6 = l5 + arr[SIX_BLOCK_SIZE_EIGHTS - 1];+ final long l7 = l6 + arr[SEVEN_BLOCK_SIZE_EIGHTS - 1];- ForDeltaUtil(ForUtil forUtil) {- this.forUtil = forUtil;+ for (int i = 0; i < ONE_BLOCK_SIZE_EIGHT; ++i) {+ arr[i] += l0;+ arr[ONE_BLOCK_SIZE_EIGHT + i] += l1;+ arr[TWO_BLOCK_SIZE_EIGHTS + i] += l2;+ arr[THREE_BLOCK_SIZE_EIGHTS + i] += l3;+ arr[FOUR_BLOCK_SIZE_EIGHTS + i] += l4;+ arr[FIVE_BLOCK_SIZE_EIGHTS + i] += l5;+ arr[SIX_BLOCK_SIZE_EIGHTS + i] += l6;+ arr[SEVEN_BLOCK_SIZE_EIGHTS + i] += l7;+ }+ }++ private static void prefixSum16(long[] arr, long base) {+ // When the number of bits per value is 11 or less, we can sum up all values in a block without+ // risking overflowing a 16-bits integer. This allows computing the prefix sum by summing up 4+ // values at once.+ innerPrefixSum16(arr);+ expand16(arr);+ final long l0 = base;+ final long l1 = l0 + arr[ONE_BLOCK_SIZE_FOURTH - 1];+ final long l2 = l1 + arr[TWO_BLOCK_SIZE_FOURTHS - 1];+ final long l3 = l2 + arr[THREE_BLOCK_SIZE_FOURTHS - 1];++ for (int i = 0; i < ONE_BLOCK_SIZE_FOURTH; ++i) {+ arr[i] += l0;+ arr[ONE_BLOCK_SIZE_FOURTH + i] += l1;+ arr[TWO_BLOCK_SIZE_FOURTHS + i] += l2;+ arr[THREE_BLOCK_SIZE_FOURTHS + i] += l3;+ }+ }++ private static void prefixSum32(long[] arr, long base) {+ arr[0] += base << 32;+ innerPrefixSum32(arr);+ expand32(arr);+ final long l = arr[BLOCK_SIZE / 2 - 1];+ for (int i = BLOCK_SIZE / 2; i < BLOCK_SIZE; ++i) {+ arr[i] += l;+ }}+ // For some reason unrolling seems to help+ private static void innerPrefixSum8(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ }++ // For some reason unrolling seems to help+ private static void innerPrefixSum16(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ arr[16] += arr[15];+ arr[17] += arr[16];+ arr[18] += arr[17];+ arr[19] += arr[18];+ arr[20] += arr[19];+ arr[21] += arr[20];+ arr[22] += arr[21];+ arr[23] += arr[22];+ arr[24] += arr[23];+ arr[25] += arr[24];+ arr[26] += arr[25];+ arr[27] += arr[26];+ arr[28] += arr[27];+ arr[29] += arr[28];+ arr[30] += arr[29];+ arr[31] += arr[30];+ }++ // For some reason unrolling seems to help+ private static void innerPrefixSum32(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ arr[16] += arr[15];+ arr[17] += arr[16];+ arr[18] += arr[17];+ arr[19] += arr[18];+ arr[20] += arr[19];+ arr[21] += arr[20];+ arr[22] += arr[21];+ arr[23] += arr[22];+ arr[24] += arr[23];+ arr[25] += arr[24];+ arr[26] += arr[25];+ arr[27] += arr[26];+ arr[28] += arr[27];+ arr[29] += arr[28];+ arr[30] += arr[29];+ arr[31] += arr[30];+ arr[32] += arr[31];+ arr[33] += arr[32];+ arr[34] += arr[33];+ arr[35] += arr[34];+ arr[36] += arr[35];+ arr[37] += arr[36];+ arr[38] += arr[37];+ arr[39] += arr[38];+ arr[40] += arr[39];+ arr[41] += arr[40];+ arr[42] += arr[41];+ arr[43] += arr[42];+ arr[44] += arr[43];+ arr[45] += arr[44];+ arr[46] += arr[45];+ arr[47] += arr[46];+ arr[48] += arr[47];+ arr[49] += arr[48];+ arr[50] += arr[49];+ arr[51] += arr[50];+ arr[52] += arr[51];+ arr[53] += arr[52];+ arr[54] += arr[53];+ arr[55] += arr[54];+ arr[56] += arr[55];+ arr[57] += arr[56];+ arr[58] += arr[57];+ arr[59] += arr[58];+ arr[60] += arr[59];+ arr[61] += arr[60];+ arr[62] += arr[61];+ arr[63] += arr[62];+ }++ private final long[] tmp = new long[BLOCK_SIZE / 2];+/*** Encode deltas of a strictly monotonically increasing sequence of integers. The provided {@code* longs} are expected to be deltas between consecutive values.@@ -62,22 +256,280 @@ final class ForDeltaUtil {assert or != 0;final int bitsPerValue = PackedInts.bitsRequired(or);out.writeByte((byte) bitsPerValue);- forUtil.encode(longs, bitsPerValue, out);++ final int primitiveSize;+ if (bitsPerValue <= 4) {+ primitiveSize = 8;+ collapse8(longs);+ } else if (bitsPerValue <= 11) {+ primitiveSize = 16;+ collapse16(longs);+ } else {+ primitiveSize = 32;+ collapse32(longs);+ }+ encode(longs, bitsPerValue, primitiveSize, out, tmp);}}/** Decode deltas, compute the prefix sum and add {@code base} to all decoded longs. */- void decodeAndPrefixSum(PostingIndexInput in, long base, long[] longs) throws IOException {- final int bitsPerValue = Byte.toUnsignedInt(in.in.readByte());+ void decodeAndPrefixSum(PostingDecodingUtil pdu, long base, long[] longs) throws IOException {+ final int bitsPerValue = Byte.toUnsignedInt(pdu.in.readByte());if (bitsPerValue == 0) {prefixSumOfOnes(longs, base);} else {- in.decodeAndPrefixSum(bitsPerValue, base, longs);+ decodeAndPrefixSum(bitsPerValue, pdu, base, longs);}}- void skip(DataInput in) throws IOException {+ void skip(IndexInput in) throws IOException {final int bitsPerValue = Byte.toUnsignedInt(in.readByte());- in.skipBytes(forUtil.numBytes(bitsPerValue));+ in.skipBytes(numBytes(bitsPerValue));+ }++ /** Delta-decode 128 integers into {@code longs}. */+ void decodeAndPrefixSum(int bitsPerValue, PostingDecodingUtil pdu, long base, long[] longs)+ throws IOException {+ switch (bitsPerValue) {+ case 1:+ decode1(pdu, tmp, longs);+ prefixSum8(longs, base);+ break;+ case 2:+ decode2(pdu, tmp, longs);+ prefixSum8(longs, base);+ break;+ case 3:+ decode3(pdu, tmp, longs);+ prefixSum8(longs, base);+ break;+ case 4:+ decode4(pdu, tmp, longs);+ prefixSum8(longs, base);+ break;+ case 5:+ decode5To16(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 6:+ decode6To16(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 7:+ decode7To16(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 8:+ decode8To16(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 9:+ decode9(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 10:+ decode10(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 11:+ decode11(pdu, tmp, longs);+ prefixSum16(longs, base);+ break;+ case 12:+ decode12To32(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 13:+ decode13To32(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 14:+ decode14To32(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 15:+ decode15To32(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 16:+ decode16To32(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 17:+ decode17(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 18:+ decode18(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 19:+ decode19(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 20:+ decode20(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 21:+ decode21(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 22:+ decode22(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 23:+ decode23(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ case 24:+ decode24(pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ default:+ decodeSlow(bitsPerValue, pdu, tmp, longs);+ prefixSum32(longs, base);+ break;+ }+ }++ private static void decode5To16(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(10, longs, 11, 5, MASK16_5, tmp, 0, MASK16_1);+ for (int iter = 0, tmpIdx = 0, longsIdx = 30; iter < 2; ++iter, tmpIdx += 5, longsIdx += 1) {+ long l0 = tmp[tmpIdx + 0] << 4;+ l0 |= tmp[tmpIdx + 1] << 3;+ l0 |= tmp[tmpIdx + 2] << 2;+ l0 |= tmp[tmpIdx + 3] << 1;+ l0 |= tmp[tmpIdx + 4] << 0;+ longs[longsIdx + 0] = l0;+ }+ }++ private static void decode6To16(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(12, longs, 10, 6, MASK16_6, tmp, 0, MASK16_4);+ for (int iter = 0, tmpIdx = 0, longsIdx = 24; iter < 4; ++iter, tmpIdx += 3, longsIdx += 2) {+ long l0 = tmp[tmpIdx + 0] << 2;+ l0 |= (tmp[tmpIdx + 1] >>> 2) & MASK16_2;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 1] & MASK16_2) << 4;+ l1 |= tmp[tmpIdx + 2] << 0;+ longs[longsIdx + 1] = l1;+ }+ }++ private static void decode7To16(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(14, longs, 9, 7, MASK16_7, tmp, 0, MASK16_2);+ for (int iter = 0, tmpIdx = 0, longsIdx = 28; iter < 2; ++iter, tmpIdx += 7, longsIdx += 2) {+ long l0 = tmp[tmpIdx + 0] << 5;+ l0 |= tmp[tmpIdx + 1] << 3;+ l0 |= tmp[tmpIdx + 2] << 1;+ l0 |= (tmp[tmpIdx + 3] >>> 1) & MASK16_1;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 3] & MASK16_1) << 6;+ l1 |= tmp[tmpIdx + 4] << 4;+ l1 |= tmp[tmpIdx + 5] << 2;+ l1 |= tmp[tmpIdx + 6] << 0;+ longs[longsIdx + 1] = l1;+ }+ }++ private static void decode8To16(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(16, longs, 8, 8, MASK16_8, longs, 16, MASK16_8);+ }++ private static void decode12To32(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(24, longs, 20, 12, MASK32_12, tmp, 0, MASK32_8);+ for (int iter = 0, tmpIdx = 0, longsIdx = 48; iter < 8; ++iter, tmpIdx += 3, longsIdx += 2) {+ long l0 = tmp[tmpIdx + 0] << 4;+ l0 |= (tmp[tmpIdx + 1] >>> 4) & MASK32_4;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 1] & MASK32_4) << 8;+ l1 |= tmp[tmpIdx + 2] << 0;+ longs[longsIdx + 1] = l1;+ }+ }++ private static void decode13To32(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(26, longs, 19, 13, MASK32_13, tmp, 0, MASK32_6);+ for (int iter = 0, tmpIdx = 0, longsIdx = 52; iter < 2; ++iter, tmpIdx += 13, longsIdx += 6) {+ long l0 = tmp[tmpIdx + 0] << 7;+ l0 |= tmp[tmpIdx + 1] << 1;+ l0 |= (tmp[tmpIdx + 2] >>> 5) & MASK32_1;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 2] & MASK32_5) << 8;+ l1 |= tmp[tmpIdx + 3] << 2;+ l1 |= (tmp[tmpIdx + 4] >>> 4) & MASK32_2;+ longs[longsIdx + 1] = l1;+ long l2 = (tmp[tmpIdx + 4] & MASK32_4) << 9;+ l2 |= tmp[tmpIdx + 5] << 3;+ l2 |= (tmp[tmpIdx + 6] >>> 3) & MASK32_3;+ longs[longsIdx + 2] = l2;+ long l3 = (tmp[tmpIdx + 6] & MASK32_3) << 10;+ l3 |= tmp[tmpIdx + 7] << 4;+ l3 |= (tmp[tmpIdx + 8] >>> 2) & MASK32_4;+ longs[longsIdx + 3] = l3;+ long l4 = (tmp[tmpIdx + 8] & MASK32_2) << 11;+ l4 |= tmp[tmpIdx + 9] << 5;+ l4 |= (tmp[tmpIdx + 10] >>> 1) & MASK32_5;+ longs[longsIdx + 4] = l4;+ long l5 = (tmp[tmpIdx + 10] & MASK32_1) << 12;+ l5 |= tmp[tmpIdx + 11] << 6;+ l5 |= tmp[tmpIdx + 12] << 0;+ longs[longsIdx + 5] = l5;+ }+ }++ private static void decode14To32(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(28, longs, 18, 14, MASK32_14, tmp, 0, MASK32_4);+ for (int iter = 0, tmpIdx = 0, longsIdx = 56; iter < 4; ++iter, tmpIdx += 7, longsIdx += 2) {+ long l0 = tmp[tmpIdx + 0] << 10;+ l0 |= tmp[tmpIdx + 1] << 6;+ l0 |= tmp[tmpIdx + 2] << 2;+ l0 |= (tmp[tmpIdx + 3] >>> 2) & MASK32_2;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 3] & MASK32_2) << 12;+ l1 |= tmp[tmpIdx + 4] << 8;+ l1 |= tmp[tmpIdx + 5] << 4;+ l1 |= tmp[tmpIdx + 6] << 0;+ longs[longsIdx + 1] = l1;+ }+ }++ private static void decode15To32(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(30, longs, 17, 15, MASK32_15, tmp, 0, MASK32_2);+ for (int iter = 0, tmpIdx = 0, longsIdx = 60; iter < 2; ++iter, tmpIdx += 15, longsIdx += 2) {+ long l0 = tmp[tmpIdx + 0] << 13;+ l0 |= tmp[tmpIdx + 1] << 11;+ l0 |= tmp[tmpIdx + 2] << 9;+ l0 |= tmp[tmpIdx + 3] << 7;+ l0 |= tmp[tmpIdx + 4] << 5;+ l0 |= tmp[tmpIdx + 5] << 3;+ l0 |= tmp[tmpIdx + 6] << 1;+ l0 |= (tmp[tmpIdx + 7] >>> 1) & MASK32_1;+ longs[longsIdx + 0] = l0;+ long l1 = (tmp[tmpIdx + 7] & MASK32_1) << 14;+ l1 |= tmp[tmpIdx + 8] << 12;+ l1 |= tmp[tmpIdx + 9] << 10;+ l1 |= tmp[tmpIdx + 10] << 8;+ l1 |= tmp[tmpIdx + 11] << 6;+ l1 |= tmp[tmpIdx + 12] << 4;+ l1 |= tmp[tmpIdx + 13] << 2;+ l1 |= tmp[tmpIdx + 14] << 0;+ longs[longsIdx + 1] = l1;+ }+ }++ private static void decode16To32(PostingDecodingUtil pdu, long[] tmp, long[] longs)+ throws IOException {+ pdu.splitLongs(32, longs, 16, 16, MASK32_16, longs, 32, MASK32_16);}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.javaindex 4408a8a3e57..ca7747b58bd 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/ForUtil.java@@ -21,7 +21,6 @@ package org.apache.lucene.codecs.lucene912;import java.io.IOException;import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.DataOutput;-import org.apache.lucene.store.IndexInput;/*** Inspired from https://fulmicoton.com/posts/bitpacking/ Encodes multiple integers in a long to get@@ -31,33 +30,33 @@ import org.apache.lucene.store.IndexInput;public final class ForUtil {public static final int BLOCK_SIZE = 128;- private static final int BLOCK_SIZE_LOG2 = 7;+ static final int BLOCK_SIZE_LOG2 = 7;- private static long expandMask32(long mask32) {+ static long expandMask32(long mask32) {return mask32 | (mask32 << 32);}- private static long expandMask16(long mask16) {+ static long expandMask16(long mask16) {return expandMask32(mask16 | (mask16 << 16));}- private static long expandMask8(long mask8) {+ static long expandMask8(long mask8) {return expandMask16(mask8 | (mask8 << 8));}- private static long mask32(int bitsPerValue) {+ static long mask32(int bitsPerValue) {return expandMask32((1L << bitsPerValue) - 1);}- private static long mask16(int bitsPerValue) {+ static long mask16(int bitsPerValue) {return expandMask16((1L << bitsPerValue) - 1);}- private static long mask8(int bitsPerValue) {+ static long mask8(int bitsPerValue) {return expandMask8((1L << bitsPerValue) - 1);}- private static void expand8(long[] arr) {+ static void expand8(long[] arr) {for (int i = 0; i < 16; ++i) {long l = arr[i];arr[i] = (l >>> 56) & 0xFFL;@@ -71,17 +70,7 @@ public final class ForUtil {}}- private static void expand8To32(long[] arr) {- for (int i = 0; i < 16; ++i) {- long l = arr[i];- arr[i] = (l >>> 24) & 0x000000FF000000FFL;- arr[16 + i] = (l >>> 16) & 0x000000FF000000FFL;- arr[32 + i] = (l >>> 8) & 0x000000FF000000FFL;- arr[48 + i] = l & 0x000000FF000000FFL;- }- }-- private static void collapse8(long[] arr) {+ static void collapse8(long[] arr) {for (int i = 0; i < 16; ++i) {arr[i] =(arr[i] << 56)@@ -95,7 +84,7 @@ public final class ForUtil {}}- private static void expand16(long[] arr) {+ static void expand16(long[] arr) {for (int i = 0; i < 32; ++i) {long l = arr[i];arr[i] = (l >>> 48) & 0xFFFFL;@@ -105,21 +94,13 @@ public final class ForUtil {}}- private static void expand16To32(long[] arr) {- for (int i = 0; i < 32; ++i) {- long l = arr[i];- arr[i] = (l >>> 16) & 0x0000FFFF0000FFFFL;- arr[32 + i] = l & 0x0000FFFF0000FFFFL;- }- }-- private static void collapse16(long[] arr) {+ static void collapse16(long[] arr) {for (int i = 0; i < 32; ++i) {arr[i] = (arr[i] << 48) | (arr[32 + i] << 32) | (arr[64 + i] << 16) | arr[96 + i];}}- private static void expand32(long[] arr) {+ static void expand32(long[] arr) {for (int i = 0; i < 64; ++i) {long l = arr[i];arr[i] = l >>> 32;@@ -127,123 +108,37 @@ public final class ForUtil {}}- private static void collapse32(long[] arr) {+ static void collapse32(long[] arr) {for (int i = 0; i < 64; ++i) {arr[i] = (arr[i] << 32) | arr[64 + i];}}- private static void prefixSum8(long[] arr, long base) {- expand8To32(arr);- prefixSum32(arr, base);- }-- private static void prefixSum16(long[] arr, long base) {- // We need to move to the next primitive size to avoid overflows- expand16To32(arr);- prefixSum32(arr, base);- }-- private static void prefixSum32(long[] arr, long base) {- arr[0] += base << 32;- innerPrefixSum32(arr);- expand32(arr);- final long l = arr[BLOCK_SIZE / 2 - 1];- for (int i = BLOCK_SIZE / 2; i < BLOCK_SIZE; ++i) {- arr[i] += l;- }- }-- // For some reason unrolling seems to help- private static void innerPrefixSum32(long[] arr) {- arr[1] += arr[0];- arr[2] += arr[1];- arr[3] += arr[2];- arr[4] += arr[3];- arr[5] += arr[4];- arr[6] += arr[5];- arr[7] += arr[6];- arr[8] += arr[7];- arr[9] += arr[8];- arr[10] += arr[9];- arr[11] += arr[10];- arr[12] += arr[11];- arr[13] += arr[12];- arr[14] += arr[13];- arr[15] += arr[14];- arr[16] += arr[15];- arr[17] += arr[16];- arr[18] += arr[17];- arr[19] += arr[18];- arr[20] += arr[19];- arr[21] += arr[20];- arr[22] += arr[21];- arr[23] += arr[22];- arr[24] += arr[23];- arr[25] += arr[24];- arr[26] += arr[25];- arr[27] += arr[26];- arr[28] += arr[27];- arr[29] += arr[28];- arr[30] += arr[29];- arr[31] += arr[30];- arr[32] += arr[31];- arr[33] += arr[32];- arr[34] += arr[33];- arr[35] += arr[34];- arr[36] += arr[35];- arr[37] += arr[36];- arr[38] += arr[37];- arr[39] += arr[38];- arr[40] += arr[39];- arr[41] += arr[40];- arr[42] += arr[41];- arr[43] += arr[42];- arr[44] += arr[43];- arr[45] += arr[44];- arr[46] += arr[45];- arr[47] += arr[46];- arr[48] += arr[47];- arr[49] += arr[48];- arr[50] += arr[49];- arr[51] += arr[50];- arr[52] += arr[51];- arr[53] += arr[52];- arr[54] += arr[53];- arr[55] += arr[54];- arr[56] += arr[55];- arr[57] += arr[56];- arr[58] += arr[57];- arr[59] += arr[58];- arr[60] += arr[59];- arr[61] += arr[60];- arr[62] += arr[61];- arr[63] += arr[62];- }-private final long[] tmp = new long[BLOCK_SIZE / 2];/** Encode 128 integers from {@code longs} into {@code out}. */void encode(long[] longs, int bitsPerValue, DataOutput out) throws IOException {final int nextPrimitive;- final int numLongs;if (bitsPerValue <= 8) {nextPrimitive = 8;- numLongs = BLOCK_SIZE / 8;collapse8(longs);} else if (bitsPerValue <= 16) {nextPrimitive = 16;- numLongs = BLOCK_SIZE / 4;collapse16(longs);} else {nextPrimitive = 32;- numLongs = BLOCK_SIZE / 2;collapse32(longs);}+ encode(longs, bitsPerValue, nextPrimitive, out, tmp);+ }++ static void encode(long[] longs, int bitsPerValue, int primitiveSize, DataOutput out, long[] tmp)+ throws IOException {+ final int numLongs = BLOCK_SIZE * primitiveSize / Long.SIZE;final int numLongsPerShift = bitsPerValue * 2;int idx = 0;- int shift = nextPrimitive - bitsPerValue;+ int shift = primitiveSize - bitsPerValue;for (int i = 0; i < numLongsPerShift; ++i) {tmp[i] = longs[idx++] << shift;}@@ -255,9 +150,9 @@ public final class ForUtil {final int remainingBitsPerLong = shift + bitsPerValue;final long maskRemainingBitsPerLong;- if (nextPrimitive == 8) {+ if (primitiveSize == 8) {maskRemainingBitsPerLong = MASKS8[remainingBitsPerLong];- } else if (nextPrimitive == 16) {+ } else if (primitiveSize == 16) {maskRemainingBitsPerLong = MASKS16[remainingBitsPerLong];} else {maskRemainingBitsPerLong = MASKS32[remainingBitsPerLong];@@ -275,10 +170,10 @@ public final class ForUtil {}} else {final long mask1, mask2;- if (nextPrimitive == 8) {+ if (primitiveSize == 8) {mask1 = MASKS8[remainingBitsPerValue];mask2 = MASKS8[remainingBitsPerLong - remainingBitsPerValue];- } else if (nextPrimitive == 16) {+ } else if (primitiveSize == 16) {mask1 = MASKS16[remainingBitsPerValue];mask2 = MASKS16[remainingBitsPerLong - remainingBitsPerValue];} else {@@ -297,27 +192,20 @@ public final class ForUtil {}/** Number of bytes required to encode 128 integers of {@code bitsPerValue} bits per value. */- int numBytes(int bitsPerValue) {+ static int numBytes(int bitsPerValue) {return bitsPerValue << (BLOCK_SIZE_LOG2 - 3);}- private static void decodeSlow(- int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)+ static void decodeSlow(int bitsPerValue, PostingDecodingUtil pdu, long[] tmp, long[] longs)throws IOException {final int numLongs = bitsPerValue << 1;final long mask = MASKS32[bitsPerValue];- pdu.splitLongs(numLongs, longs, 32 - bitsPerValue, mask, tmp, 0, -1L);- int longsIdx = numLongs;- int shift = 32 - 2 * bitsPerValue;- for (; shift >= 0; shift -= bitsPerValue) {- shiftLongs(tmp, numLongs, longs, longsIdx, shift, mask);- longsIdx += numLongs;- }- final int remainingBitsPerLong = shift + bitsPerValue;+ pdu.splitLongs(numLongs, longs, 32 - bitsPerValue, 32, mask, tmp, 0, -1L);+ final int remainingBitsPerLong = 32 - bitsPerValue;final long mask32RemainingBitsPerLong = MASKS32[remainingBitsPerLong];int tmpIdx = 0;int remainingBits = remainingBitsPerLong;- for (; longsIdx < BLOCK_SIZE / 2; ++longsIdx) {+ for (int longsIdx = numLongs; longsIdx < BLOCK_SIZE / 2; ++longsIdx) {int b = bitsPerValue - remainingBits;long l = (tmp[tmpIdx++] & MASKS32[remainingBits]) << b;while (b >= remainingBitsPerLong) {@@ -334,26 +222,9 @@ public final class ForUtil {}}- /**- * The pattern that this shiftLongs method applies is recognized by the C2 compiler, which- * generates SIMD instructions for it in order to shift multiple longs at once.- */- private static void shiftLongs(long[] a, int count, long[] b, int bi, int shift, long mask) {- for (int i = 0; i < count; ++i) {- b[bi + i] = (a[i] >>> shift) & mask;- }- }-- /** Likewise, but for a simple mask. */- private static void maskLongs(long[] a, int count, long[] b, int bi, long mask) {- for (int i = 0; i < count; ++i) {- b[bi + i] = a[i] & mask;- }- }-- private static final long[] MASKS8 = new long[8];- private static final long[] MASKS16 = new long[16];- private static final long[] MASKS32 = new long[32];+ static final long[] MASKS8 = new long[8];+ static final long[] MASKS16 = new long[16];+ static final long[] MASKS32 = new long[32];static {for (int i = 0; i < 8; ++i) {@@ -369,291 +240,169 @@ public final class ForUtil {// mark values in array as final longs to avoid the cost of reading array, arrays should only be// used when the idx is a variable- private static final long MASK8_1 = MASKS8[1];- private static final long MASK8_2 = MASKS8[2];- private static final long MASK8_3 = MASKS8[3];- private static final long MASK8_4 = MASKS8[4];- private static final long MASK8_5 = MASKS8[5];- private static final long MASK8_6 = MASKS8[6];- private static final long MASK8_7 = MASKS8[7];- private static final long MASK16_1 = MASKS16[1];- private static final long MASK16_2 = MASKS16[2];- private static final long MASK16_3 = MASKS16[3];- private static final long MASK16_4 = MASKS16[4];- private static final long MASK16_5 = MASKS16[5];- private static final long MASK16_6 = MASKS16[6];- private static final long MASK16_7 = MASKS16[7];- private static final long MASK16_9 = MASKS16[9];- private static final long MASK16_10 = MASKS16[10];- private static final long MASK16_11 = MASKS16[11];- private static final long MASK16_12 = MASKS16[12];- private static final long MASK16_13 = MASKS16[13];- private static final long MASK16_14 = MASKS16[14];- private static final long MASK16_15 = MASKS16[15];- private static final long MASK32_1 = MASKS32[1];- private static final long MASK32_2 = MASKS32[2];- private static final long MASK32_3 = MASKS32[3];- private static final long MASK32_4 = MASKS32[4];- private static final long MASK32_5 = MASKS32[5];- private static final long MASK32_6 = MASKS32[6];- private static final long MASK32_7 = MASKS32[7];- private static final long MASK32_8 = MASKS32[8];- private static final long MASK32_9 = MASKS32[9];- private static final long MASK32_10 = MASKS32[10];- private static final long MASK32_11 = MASKS32[11];- private static final long MASK32_12 = MASKS32[12];- private static final long MASK32_13 = MASKS32[13];- private static final long MASK32_14 = MASKS32[14];- private static final long MASK32_15 = MASKS32[15];- private static final long MASK32_17 = MASKS32[17];- private static final long MASK32_18 = MASKS32[18];- private static final long MASK32_19 = MASKS32[19];- private static final long MASK32_20 = MASKS32[20];- private static final long MASK32_21 = MASKS32[21];- private static final long MASK32_22 = MASKS32[22];- private static final long MASK32_23 = MASKS32[23];- private static final long MASK32_24 = MASKS32[24];+ static final long MASK8_1 = MASKS8[1];+ static final long MASK8_2 = MASKS8[2];+ static final long MASK8_3 = MASKS8[3];+ static final long MASK8_4 = MASKS8[4];+ static final long MASK8_5 = MASKS8[5];+ static final long MASK8_6 = MASKS8[6];+ static final long MASK8_7 = MASKS8[7];+ static final long MASK16_1 = MASKS16[1];+ static final long MASK16_2 = MASKS16[2];+ static final long MASK16_3 = MASKS16[3];+ static final long MASK16_4 = MASKS16[4];+ static final long MASK16_5 = MASKS16[5];+ static final long MASK16_6 = MASKS16[6];+ static final long MASK16_7 = MASKS16[7];+ static final long MASK16_8 = MASKS16[8];+ static final long MASK16_9 = MASKS16[9];+ static final long MASK16_10 = MASKS16[10];+ static final long MASK16_11 = MASKS16[11];+ static final long MASK16_12 = MASKS16[12];+ static final long MASK16_13 = MASKS16[13];+ static final long MASK16_14 = MASKS16[14];+ static final long MASK16_15 = MASKS16[15];+ static final long MASK32_1 = MASKS32[1];+ static final long MASK32_2 = MASKS32[2];+ static final long MASK32_3 = MASKS32[3];+ static final long MASK32_4 = MASKS32[4];+ static final long MASK32_5 = MASKS32[5];+ static final long MASK32_6 = MASKS32[6];+ static final long MASK32_7 = MASKS32[7];+ static final long MASK32_8 = MASKS32[8];+ static final long MASK32_9 = MASKS32[9];+ static final long MASK32_10 = MASKS32[10];+ static final long MASK32_11 = MASKS32[11];+ static final long MASK32_12 = MASKS32[12];+ static final long MASK32_13 = MASKS32[13];+ static final long MASK32_14 = MASKS32[14];+ static final long MASK32_15 = MASKS32[15];+ static final long MASK32_16 = MASKS32[16];+ static final long MASK32_17 = MASKS32[17];+ static final long MASK32_18 = MASKS32[18];+ static final long MASK32_19 = MASKS32[19];+ static final long MASK32_20 = MASKS32[20];+ static final long MASK32_21 = MASKS32[21];+ static final long MASK32_22 = MASKS32[22];+ static final long MASK32_23 = MASKS32[23];+ static final long MASK32_24 = MASKS32[24];/** Decode 128 integers into {@code longs}. */- void decode(int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long[] longs)- throws IOException {+ void decode(int bitsPerValue, PostingDecodingUtil pdu, long[] longs) throws IOException {switch (bitsPerValue) {case 1:- decode1(in, pdu, tmp, longs);+ decode1(pdu, tmp, longs);expand8(longs);break;case 2:- decode2(in, pdu, tmp, longs);+ decode2(pdu, tmp, longs);expand8(longs);break;case 3:- decode3(in, pdu, tmp, longs);+ decode3(pdu, tmp, longs);expand8(longs);break;case 4:- decode4(in, pdu, tmp, longs);+ decode4(pdu, tmp, longs);expand8(longs);break;case 5:- decode5(in, pdu, tmp, longs);+ decode5(pdu, tmp, longs);expand8(longs);break;case 6:- decode6(in, pdu, tmp, longs);+ decode6(pdu, tmp, longs);expand8(longs);break;case 7:- decode7(in, pdu, tmp, longs);+ decode7(pdu, tmp, longs);expand8(longs);break;case 8:- decode8(in, pdu, tmp, longs);+ decode8(pdu, tmp, longs);expand8(longs);break;case 9:- decode9(in, pdu, tmp, longs);+ decode9(pdu, tmp, longs);expand16(longs);break;case 10:- decode10(in, pdu, tmp, longs);+ decode10(pdu, tmp, longs);expand16(longs);break;case 11:- decode11(in, pdu, tmp, longs);+ decode11(pdu, tmp, longs);expand16(longs);break;case 12:- decode12(in, pdu, tmp, longs);+ decode12(pdu, tmp, longs);expand16(longs);break;case 13:- decode13(in, pdu, tmp, longs);+ decode13(pdu, tmp, longs);expand16(longs);break;case 14:- decode14(in, pdu, tmp, longs);+ decode14(pdu, tmp, longs);expand16(longs);break;case 15:- decode15(in, pdu, tmp, longs);+ decode15(pdu, tmp, longs);expand16(longs);break;case 16:- decode16(in, pdu, tmp, longs);+ decode16(pdu, tmp, longs);expand16(longs);break;case 17:- decode17(in, pdu, tmp, longs);+ decode17(pdu, tmp, longs);expand32(longs);break;case 18:- decode18(in, pdu, tmp, longs);+ decode18(pdu, tmp, longs);expand32(longs);break;case 19:- decode19(in, pdu, tmp, longs);+ decode19(pdu, tmp, longs);expand32(longs);break;case 20:- decode20(in, pdu, tmp, longs);+ decode20(pdu, tmp, longs);expand32(longs);break;case 21:- decode21(in, pdu, tmp, longs);+ decode21(pdu, tmp, longs);expand32(longs);break;case 22:- decode22(in, pdu, tmp, longs);+ decode22(pdu, tmp, longs);expand32(longs);break;case 23:- decode23(in, pdu, tmp, longs);+ decode23(pdu, tmp, longs);expand32(longs);break;case 24:- decode24(in, pdu, tmp, longs);+ decode24(pdu, tmp, longs);expand32(longs);break;default:- decodeSlow(bitsPerValue, in, pdu, tmp, longs);+ decodeSlow(bitsPerValue, pdu, tmp, longs);expand32(longs);break;}}- /** Delta-decode 128 integers into {@code longs}. */- void decodeAndPrefixSum(- int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long base, long[] longs)- throws IOException {- switch (bitsPerValue) {- case 1:- decode1(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 2:- decode2(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 3:- decode3(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 4:- decode4(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 5:- decode5(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 6:- decode6(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 7:- decode7(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 8:- decode8(in, pdu, tmp, longs);- prefixSum8(longs, base);- break;- case 9:- decode9(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 10:- decode10(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 11:- decode11(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 12:- decode12(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 13:- decode13(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 14:- decode14(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 15:- decode15(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 16:- decode16(in, pdu, tmp, longs);- prefixSum16(longs, base);- break;- case 17:- decode17(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 18:- decode18(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 19:- decode19(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 20:- decode20(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 21:- decode21(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 22:- decode22(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 23:- decode23(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- case 24:- decode24(in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- default:- decodeSlow(bitsPerValue, in, pdu, tmp, longs);- prefixSum32(longs, base);- break;- }- }-- private static void decode1(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(2, longs, 7, MASK8_1, tmp, 0, MASK8_7);- shiftLongs(tmp, 2, longs, 2, 6, MASK8_1);- shiftLongs(tmp, 2, longs, 4, 5, MASK8_1);- shiftLongs(tmp, 2, longs, 6, 4, MASK8_1);- shiftLongs(tmp, 2, longs, 8, 3, MASK8_1);- shiftLongs(tmp, 2, longs, 10, 2, MASK8_1);- shiftLongs(tmp, 2, longs, 12, 1, MASK8_1);- maskLongs(tmp, 2, longs, 14, MASK8_1);+ static void decode1(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(2, longs, 7, 1, MASK8_1, longs, 14, MASK8_1);}- private static void decode2(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(4, longs, 6, MASK8_2, tmp, 0, MASK8_6);- shiftLongs(tmp, 4, longs, 4, 4, MASK8_2);- shiftLongs(tmp, 4, longs, 8, 2, MASK8_2);- maskLongs(tmp, 4, longs, 12, MASK8_2);+ static void decode2(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(4, longs, 6, 2, MASK8_2, longs, 12, MASK8_2);}- private static void decode3(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(6, longs, 5, MASK8_3, tmp, 0, MASK8_5);- shiftLongs(tmp, 6, longs, 6, 2, MASK8_3);- maskLongs(tmp, 6, tmp, 0, MASK8_2);+ static void decode3(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(6, longs, 5, 3, MASK8_3, tmp, 0, MASK8_2);for (int iter = 0, tmpIdx = 0, longsIdx = 12; iter < 2; ++iter, tmpIdx += 3, longsIdx += 2) {long l0 = tmp[tmpIdx + 0] << 1;l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK8_1;@@ -664,14 +413,12 @@ public final class ForUtil {}}- private static void decode4(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(8, longs, 4, MASK8_4, longs, 8, MASK8_4);+ static void decode4(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(8, longs, 4, 4, MASK8_4, longs, 8, MASK8_4);}- private static void decode5(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(10, longs, 3, MASK8_5, tmp, 0, MASK8_3);+ static void decode5(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(10, longs, 3, 5, MASK8_5, tmp, 0, MASK8_3);for (int iter = 0, tmpIdx = 0, longsIdx = 10; iter < 2; ++iter, tmpIdx += 5, longsIdx += 3) {long l0 = tmp[tmpIdx + 0] << 2;l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK8_2;@@ -686,9 +433,8 @@ public final class ForUtil {}}- private static void decode6(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(12, longs, 2, MASK8_6, tmp, 0, MASK8_2);+ static void decode6(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(12, longs, 2, 6, MASK8_6, tmp, 0, MASK8_2);for (int iter = 0, tmpIdx = 0, longsIdx = 12; iter < 4; ++iter, tmpIdx += 3, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 4;l0 |= tmp[tmpIdx + 1] << 2;@@ -697,9 +443,8 @@ public final class ForUtil {}}- private static void decode7(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(14, longs, 1, MASK8_7, tmp, 0, MASK8_1);+ static void decode7(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(14, longs, 1, 7, MASK8_7, tmp, 0, MASK8_1);for (int iter = 0, tmpIdx = 0, longsIdx = 14; iter < 2; ++iter, tmpIdx += 7, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 6;l0 |= tmp[tmpIdx + 1] << 5;@@ -712,14 +457,12 @@ public final class ForUtil {}}- private static void decode8(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- in.readLongs(longs, 0, 16);+ static void decode8(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.in.readLongs(longs, 0, 16);}- private static void decode9(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(18, longs, 7, MASK16_9, tmp, 0, MASK16_7);+ static void decode9(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(18, longs, 7, 9, MASK16_9, tmp, 0, MASK16_7);for (int iter = 0, tmpIdx = 0, longsIdx = 18; iter < 2; ++iter, tmpIdx += 9, longsIdx += 7) {long l0 = tmp[tmpIdx + 0] << 2;l0 |= (tmp[tmpIdx + 1] >>> 5) & MASK16_2;@@ -746,9 +489,8 @@ public final class ForUtil {}}- private static void decode10(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(20, longs, 6, MASK16_10, tmp, 0, MASK16_6);+ static void decode10(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(20, longs, 6, 10, MASK16_10, tmp, 0, MASK16_6);for (int iter = 0, tmpIdx = 0, longsIdx = 20; iter < 4; ++iter, tmpIdx += 5, longsIdx += 3) {long l0 = tmp[tmpIdx + 0] << 4;l0 |= (tmp[tmpIdx + 1] >>> 2) & MASK16_4;@@ -763,9 +505,8 @@ public final class ForUtil {}}- private static void decode11(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(22, longs, 5, MASK16_11, tmp, 0, MASK16_5);+ static void decode11(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(22, longs, 5, 11, MASK16_11, tmp, 0, MASK16_5);for (int iter = 0, tmpIdx = 0, longsIdx = 22; iter < 2; ++iter, tmpIdx += 11, longsIdx += 5) {long l0 = tmp[tmpIdx + 0] << 6;l0 |= tmp[tmpIdx + 1] << 1;@@ -790,9 +531,8 @@ public final class ForUtil {}}- private static void decode12(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(24, longs, 4, MASK16_12, tmp, 0, MASK16_4);+ static void decode12(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(24, longs, 4, 12, MASK16_12, tmp, 0, MASK16_4);for (int iter = 0, tmpIdx = 0, longsIdx = 24; iter < 8; ++iter, tmpIdx += 3, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 8;l0 |= tmp[tmpIdx + 1] << 4;@@ -801,9 +541,8 @@ public final class ForUtil {}}- private static void decode13(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(26, longs, 3, MASK16_13, tmp, 0, MASK16_3);+ static void decode13(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(26, longs, 3, 13, MASK16_13, tmp, 0, MASK16_3);for (int iter = 0, tmpIdx = 0, longsIdx = 26; iter < 2; ++iter, tmpIdx += 13, longsIdx += 3) {long l0 = tmp[tmpIdx + 0] << 10;l0 |= tmp[tmpIdx + 1] << 7;@@ -826,9 +565,8 @@ public final class ForUtil {}}- private static void decode14(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(28, longs, 2, MASK16_14, tmp, 0, MASK16_2);+ static void decode14(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(28, longs, 2, 14, MASK16_14, tmp, 0, MASK16_2);for (int iter = 0, tmpIdx = 0, longsIdx = 28; iter < 4; ++iter, tmpIdx += 7, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 12;l0 |= tmp[tmpIdx + 1] << 10;@@ -841,9 +579,8 @@ public final class ForUtil {}}- private static void decode15(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(30, longs, 1, MASK16_15, tmp, 0, MASK16_1);+ static void decode15(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(30, longs, 1, 15, MASK16_15, tmp, 0, MASK16_1);for (int iter = 0, tmpIdx = 0, longsIdx = 30; iter < 2; ++iter, tmpIdx += 15, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 14;l0 |= tmp[tmpIdx + 1] << 13;@@ -864,14 +601,12 @@ public final class ForUtil {}}- private static void decode16(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- in.readLongs(longs, 0, 32);+ static void decode16(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.in.readLongs(longs, 0, 32);}- private static void decode17(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(34, longs, 15, MASK32_17, tmp, 0, MASK32_15);+ static void decode17(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(34, longs, 15, 17, MASK32_17, tmp, 0, MASK32_15);for (int iter = 0, tmpIdx = 0, longsIdx = 34; iter < 2; ++iter, tmpIdx += 17, longsIdx += 15) {long l0 = tmp[tmpIdx + 0] << 2;l0 |= (tmp[tmpIdx + 1] >>> 13) & MASK32_2;@@ -922,9 +657,8 @@ public final class ForUtil {}}- private static void decode18(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(36, longs, 14, MASK32_18, tmp, 0, MASK32_14);+ static void decode18(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(36, longs, 14, 18, MASK32_18, tmp, 0, MASK32_14);for (int iter = 0, tmpIdx = 0, longsIdx = 36; iter < 4; ++iter, tmpIdx += 9, longsIdx += 7) {long l0 = tmp[tmpIdx + 0] << 4;l0 |= (tmp[tmpIdx + 1] >>> 10) & MASK32_4;@@ -951,9 +685,8 @@ public final class ForUtil {}}- private static void decode19(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(38, longs, 13, MASK32_19, tmp, 0, MASK32_13);+ static void decode19(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(38, longs, 13, 19, MASK32_19, tmp, 0, MASK32_13);for (int iter = 0, tmpIdx = 0, longsIdx = 38; iter < 2; ++iter, tmpIdx += 19, longsIdx += 13) {long l0 = tmp[tmpIdx + 0] << 6;l0 |= (tmp[tmpIdx + 1] >>> 7) & MASK32_6;@@ -1002,9 +735,8 @@ public final class ForUtil {}}- private static void decode20(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(40, longs, 12, MASK32_20, tmp, 0, MASK32_12);+ static void decode20(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(40, longs, 12, 20, MASK32_20, tmp, 0, MASK32_12);for (int iter = 0, tmpIdx = 0, longsIdx = 40; iter < 8; ++iter, tmpIdx += 5, longsIdx += 3) {long l0 = tmp[tmpIdx + 0] << 8;l0 |= (tmp[tmpIdx + 1] >>> 4) & MASK32_8;@@ -1019,9 +751,8 @@ public final class ForUtil {}}- private static void decode21(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(42, longs, 11, MASK32_21, tmp, 0, MASK32_11);+ static void decode21(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(42, longs, 11, 21, MASK32_21, tmp, 0, MASK32_11);for (int iter = 0, tmpIdx = 0, longsIdx = 42; iter < 2; ++iter, tmpIdx += 21, longsIdx += 11) {long l0 = tmp[tmpIdx + 0] << 10;l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK32_10;@@ -1068,9 +799,8 @@ public final class ForUtil {}}- private static void decode22(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(44, longs, 10, MASK32_22, tmp, 0, MASK32_10);+ static void decode22(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(44, longs, 10, 22, MASK32_22, tmp, 0, MASK32_10);for (int iter = 0, tmpIdx = 0, longsIdx = 44; iter < 4; ++iter, tmpIdx += 11, longsIdx += 5) {long l0 = tmp[tmpIdx + 0] << 12;l0 |= tmp[tmpIdx + 1] << 2;@@ -1095,9 +825,8 @@ public final class ForUtil {}}- private static void decode23(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(46, longs, 9, MASK32_23, tmp, 0, MASK32_9);+ static void decode23(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(46, longs, 9, 23, MASK32_23, tmp, 0, MASK32_9);for (int iter = 0, tmpIdx = 0, longsIdx = 46; iter < 2; ++iter, tmpIdx += 23, longsIdx += 9) {long l0 = tmp[tmpIdx + 0] << 14;l0 |= tmp[tmpIdx + 1] << 5;@@ -1142,9 +871,8 @@ public final class ForUtil {}}- private static void decode24(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)- throws IOException {- pdu.splitLongs(48, longs, 8, MASK32_24, tmp, 0, MASK32_8);+ static void decode24(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {+ pdu.splitLongs(48, longs, 8, 24, MASK32_24, tmp, 0, MASK32_8);for (int iter = 0, tmpIdx = 0, longsIdx = 48; iter < 16; ++iter, tmpIdx += 3, longsIdx += 1) {long l0 = tmp[tmpIdx + 0] << 16;l0 |= tmp[tmpIdx + 1] << 8;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.javaindex 491b4507cf1..9c65078cfa9 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java@@ -45,6 +45,8 @@ import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.PostingsEnum;import org.apache.lucene.index.SegmentReadState;import org.apache.lucene.index.SlowImpactsEnum;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;+import org.apache.lucene.internal.vectorization.VectorizationProvider;import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.store.ByteArrayDataInput;import org.apache.lucene.store.ChecksumIndexInput;@@ -64,6 +66,8 @@ import org.apache.lucene.util.IOUtils;*/public final class Lucene912PostingsReader extends PostingsReaderBase {+ static final VectorizationProvider VECTORIZATION_PROVIDER = VectorizationProvider.getInstance();+private final IndexInput docIn;private final IndexInput posIn;private final IndexInput payIn;@@ -341,7 +345,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final class BlockDocsEnum extends PostingsEnum {final ForUtil forUtil = new ForUtil();- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(forUtil);+ final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();final PForUtil pforUtil = new PForUtil(forUtil);private final long[] docBuffer = new long[BLOCK_SIZE + 1];@@ -352,7 +356,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final IndexInput startDocIn;IndexInput docIn;- PostingIndexInput postingDocIn;+ PostingDecodingUtil docInUtil;final boolean indexHasFreq;final boolean indexHasPos;final boolean indexHasOffsetsOrPayloads;@@ -414,7 +418,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {if (docIn == null) {// lazy initdocIn = startDocIn.clone();- postingDocIn = new PostingIndexInput(docIn, forUtil);+ docInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(docIn);}prefetchPostings(docIn, termState);}@@ -448,7 +452,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {public int freq() throws IOException {if (freqFP != -1) {docIn.seek(freqFP);- pforUtil.decode(postingDocIn, freqBuffer);+ pforUtil.decode(docInUtil, freqBuffer);freqFP = -1;}@@ -483,7 +487,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {private void refillFullBlock() throws IOException {assert docFreq - docCountUpto >= BLOCK_SIZE;- forDeltaUtil.decodeAndPrefixSum(postingDocIn, prevDocID, docBuffer);+ forDeltaUtil.decodeAndPrefixSum(docInUtil, prevDocID, docBuffer);if (indexHasFreq) {if (needsFreq) {@@ -626,7 +630,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final class EverythingEnum extends PostingsEnum {final ForUtil forUtil = new ForUtil();- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(forUtil);+ final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();final PForUtil pforUtil = new PForUtil(forUtil);private final long[] docBuffer = new long[BLOCK_SIZE + 1];@@ -651,11 +655,11 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final IndexInput startDocIn;IndexInput docIn;- PostingIndexInput postingDocIn;+ PostingDecodingUtil docInUtil;final IndexInput posIn;- final PostingIndexInput postingPosIn;+ final PostingDecodingUtil posInUtil;final IndexInput payIn;- final PostingIndexInput postingPayIn;+ final PostingDecodingUtil payInUtil;final BytesRef payload;final boolean indexHasFreq;@@ -723,13 +727,13 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;this.posIn = Lucene912PostingsReader.this.posIn.clone();- postingPosIn = new PostingIndexInput(posIn, forUtil);+ posInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(posIn);if (indexHasOffsetsOrPayloads) {this.payIn = Lucene912PostingsReader.this.payIn.clone();- postingPayIn = new PostingIndexInput(payIn, forUtil);+ payInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(payIn);} else {this.payIn = null;- postingPayIn = null;+ payInUtil = null;}if (indexHasOffsets) {offsetStartDeltaBuffer = new long[BLOCK_SIZE];@@ -776,7 +780,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {if (docIn == null) {// lazy initdocIn = startDocIn.clone();- postingDocIn = new PostingIndexInput(docIn, forUtil);+ docInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(docIn);}prefetchPostings(docIn, termState);}@@ -839,8 +843,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {assert left >= 0;if (left >= BLOCK_SIZE) {- forDeltaUtil.decodeAndPrefixSum(postingDocIn, prevDocID, docBuffer);- pforUtil.decode(postingDocIn, freqBuffer);+ forDeltaUtil.decodeAndPrefixSum(docInUtil, prevDocID, docBuffer);+ pforUtil.decode(docInUtil, freqBuffer);docCountUpto += BLOCK_SIZE;} else if (docFreq == 1) {docBuffer[0] = singletonDocID;@@ -1119,11 +1123,11 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {}payloadByteUpto = 0;} else {- pforUtil.decode(postingPosIn, posDeltaBuffer);+ pforUtil.decode(posInUtil, posDeltaBuffer);if (indexHasPayloads) {if (needsPayloads) {- pforUtil.decode(postingPayIn, payloadLengthBuffer);+ pforUtil.decode(payInUtil, payloadLengthBuffer);int numBytes = payIn.readVInt();if (numBytes > payloadBytes.length) {@@ -1142,8 +1146,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {if (indexHasOffsets) {if (needsOffsets) {- pforUtil.decode(postingPayIn, offsetStartDeltaBuffer);- pforUtil.decode(postingPayIn, offsetLengthBuffer);+ pforUtil.decode(payInUtil, offsetStartDeltaBuffer);+ pforUtil.decode(payInUtil, offsetLengthBuffer);} else {// this works, because when writing a vint block we always force the first length to be// written@@ -1216,7 +1220,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final class BlockImpactsDocsEnum extends ImpactsEnum {final ForUtil forUtil = new ForUtil();- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(forUtil);+ final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();final PForUtil pforUtil = new PForUtil(forUtil);private final long[] docBuffer = new long[BLOCK_SIZE + 1];@@ -1227,7 +1231,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final IndexInput startDocIn;final IndexInput docIn;- final PostingIndexInput postingDocIn;+ final PostingDecodingUtil docInUtil;final boolean indexHasFreq;final boolean indexHasPos;final boolean indexHasOffsetsOrPayloads;@@ -1274,11 +1278,11 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {docFreq = termState.docFreq;if (docFreq > 1) {docIn = startDocIn.clone();- postingDocIn = new PostingIndexInput(docIn, forUtil);+ docInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(docIn);prefetchPostings(docIn, termState);} else {docIn = null;- postingDocIn = null;+ docInUtil = null;}doc = -1;@@ -1312,7 +1316,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {public int freq() throws IOException {if (freqFP != -1) {docIn.seek(freqFP);- pforUtil.decode(postingDocIn, freqBuffer);+ pforUtil.decode(docInUtil, freqBuffer);freqFP = -1;}return (int) freqBuffer[docBufferUpto - 1];@@ -1348,7 +1352,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {assert left >= 0;if (left >= BLOCK_SIZE) {- forDeltaUtil.decodeAndPrefixSum(postingDocIn, prevDocID, docBuffer);+ forDeltaUtil.decodeAndPrefixSum(docInUtil, prevDocID, docBuffer);if (indexHasFreq) {freqFP = docIn.getFilePointer();@@ -1571,7 +1575,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final class BlockImpactsPostingsEnum extends ImpactsEnum {final ForUtil forUtil = new ForUtil();- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(forUtil);+ final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();final PForUtil pforUtil = new PForUtil(forUtil);private final long[] docBuffer = new long[BLOCK_SIZE + 1];@@ -1584,9 +1588,9 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {final IndexInput startDocIn;final IndexInput docIn;- final PostingIndexInput postingDocIn;+ final PostingDecodingUtil docInUtil;final IndexInput posIn;- final PostingIndexInput postingPosIn;+ final PostingDecodingUtil posInUtil;final boolean indexHasFreq;final boolean indexHasPos;@@ -1652,7 +1656,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {indexHasOffsetsOrPayloads = indexHasOffsets || indexHasPayloads;this.posIn = Lucene912PostingsReader.this.posIn.clone();- postingPosIn = new PostingIndexInput(posIn, forUtil);+ posInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(posIn);// We set the last element of docBuffer to NO_MORE_DOCS, it helps save conditionals in// advance()@@ -1664,11 +1668,11 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {singletonDocID = termState.singletonDocID;if (docFreq > 1) {docIn = startDocIn.clone();- postingDocIn = new PostingIndexInput(docIn, forUtil);+ docInUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(docIn);prefetchPostings(docIn, termState);} else {docIn = null;- postingDocIn = null;+ docInUtil = null;}posIn.seek(posTermStartFP);level1PosEndFP = posTermStartFP;@@ -1720,8 +1724,8 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {assert left >= 0;if (left >= BLOCK_SIZE) {- forDeltaUtil.decodeAndPrefixSum(postingDocIn, prevDocID, docBuffer);- pforUtil.decode(postingDocIn, freqBuffer);+ forDeltaUtil.decodeAndPrefixSum(docInUtil, prevDocID, docBuffer);+ pforUtil.decode(docInUtil, freqBuffer);docCountUpto += BLOCK_SIZE;} else if (docFreq == 1) {docBuffer[0] = singletonDocID;@@ -1994,7 +1998,7 @@ public final class Lucene912PostingsReader extends PostingsReaderBase {}}} else {- pforUtil.decode(postingPosIn, posDeltaBuffer);+ pforUtil.decode(posInUtil, posDeltaBuffer);}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsWriter.javaindex b3c6503449a..3d493622c05 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsWriter.java@@ -143,7 +143,7 @@ public class Lucene912PostingsWriter extends PushPostingsWriterBase {CodecUtil.writeIndexHeader(docOut, DOC_CODEC, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);final ForUtil forUtil = new ForUtil();- forDeltaUtil = new ForDeltaUtil(forUtil);+ forDeltaUtil = new ForDeltaUtil();pforUtil = new PForUtil(forUtil);if (state.fieldInfos.hasProx()) {posDeltaBuffer = new long[BLOCK_SIZE];diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PForUtil.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PForUtil.javaindex 4fbe7051f3b..798101b6531 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PForUtil.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PForUtil.java@@ -18,6 +18,7 @@ package org.apache.lucene.codecs.lucene912;import java.io.IOException;import java.util.Arrays;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.DataInput;import org.apache.lucene.store.DataOutput;import org.apache.lucene.util.LongHeap;@@ -104,18 +105,18 @@ final class PForUtil {}/** Decode 128 integers into {@code ints}. */- void decode(PostingIndexInput in, long[] longs) throws IOException {- final int token = Byte.toUnsignedInt(in.in.readByte());+ void decode(PostingDecodingUtil pdu, long[] longs) throws IOException {+ final int token = Byte.toUnsignedInt(pdu.in.readByte());final int bitsPerValue = token & 0x1f;final int numExceptions = token >>> 5;if (bitsPerValue == 0) {- Arrays.fill(longs, 0, ForUtil.BLOCK_SIZE, in.in.readVLong());+ Arrays.fill(longs, 0, ForUtil.BLOCK_SIZE, pdu.in.readVLong());} else {- in.decode(bitsPerValue, longs);+ forUtil.decode(bitsPerValue, pdu, longs);}for (int i = 0; i < numExceptions; ++i) {- longs[Byte.toUnsignedInt(in.in.readByte())] |=- Byte.toUnsignedLong(in.in.readByte()) << bitsPerValue;+ longs[Byte.toUnsignedInt(pdu.in.readByte())] |=+ Byte.toUnsignedLong(pdu.in.readByte()) << bitsPerValue;}}@@ -128,7 +129,7 @@ final class PForUtil {in.readVLong();in.skipBytes((numExceptions << 1));} else {- in.skipBytes(forUtil.numBytes(bitsPerValue) + (numExceptions << 1));+ in.skipBytes(ForUtil.numBytes(bitsPerValue) + (numExceptions << 1));}}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PostingIndexInput.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PostingIndexInput.javaindex 88067b02858..19bae4770c5 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PostingIndexInput.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/PostingIndexInput.java@@ -23,26 +23,28 @@ import org.apache.lucene.store.IndexInput;/*** Wrapper around an {@link IndexInput} and a {@link ForUtil} that optionally optimizes decoding- * using vectorization.+ * using vectorization. This class mostly exists to enable benchmarking the decoding logic of+ * postings since it internally calls code that may only be called from the lucene-core JAR.*/public final class PostingIndexInput {private static final VectorizationProvider VECTORIZATION_PROVIDER =VectorizationProvider.getInstance();- public final IndexInput in;public final ForUtil forUtil;+ public final ForDeltaUtil forDeltaUtil;private final PostingDecodingUtil postingDecodingUtil;- public PostingIndexInput(IndexInput in, ForUtil forUtil) throws IOException {- this.in = in;+ public PostingIndexInput(IndexInput in, ForUtil forUtil, ForDeltaUtil forDeltaUtil)+ throws IOException {this.forUtil = forUtil;+ this.forDeltaUtil = forDeltaUtil;this.postingDecodingUtil = VECTORIZATION_PROVIDER.newPostingDecodingUtil(in);}/** Decode 128 integers stored on {@code bitsPerValues} bits per value into {@code longs}. */public void decode(int bitsPerValue, long[] longs) throws IOException {- forUtil.decode(bitsPerValue, in, postingDecodingUtil, longs);+ forUtil.decode(bitsPerValue, postingDecodingUtil, longs);}/**@@ -50,6 +52,6 @@ public final class PostingIndexInput {* and store results into {@code longs}.*/public void decodeAndPrefixSum(int bitsPerValue, long base, long[] longs) throws IOException {- forUtil.decodeAndPrefixSum(bitsPerValue, in, postingDecodingUtil, base, longs);+ forDeltaUtil.decodeAndPrefixSum(bitsPerValue, postingDecodingUtil, base, longs);}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForDeltaUtil.py b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForDeltaUtil.pynew file mode 100644index 00000000000..b3bf493c86b--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForDeltaUtil.py@@ -0,0 +1,417 @@+#! /usr/bin/env python++# Licensed to the Apache Software Foundation (ASF) under one or more+# contributor license agreements. See the NOTICE file distributed with+# this work for additional information regarding copyright ownership.+# The ASF licenses this file to You under the Apache License, Version 2.0+# (the "License"); you may not use this file except in compliance with+# the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++from math import gcd++"""Code generation for ForDeltaUtil.java"""++MAX_SPECIALIZED_BITS_PER_VALUE = 24+OUTPUT_FILE = "ForDeltaUtil.java"+PRIMITIVE_SIZE = [8, 16, 32]+HEADER = """// This file has been automatically generated, DO NOT EDIT++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.codecs.lucene912;++import java.io.IOException;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;+import org.apache.lucene.store.DataOutput;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.util.packed.PackedInts;++import static org.apache.lucene.codecs.lucene912.ForUtil.*;++/**+ * Inspired from https://fulmicoton.com/posts/bitpacking/+ * Encodes multiple integers in a long to get SIMD-like speedups.+ * If bitsPerValue <= 4 then we pack 8 ints per long+ * else if bitsPerValue <= 11 we pack 4 ints per long+ * else we pack 2 ints per long+ */+public final class ForDeltaUtil {++ private static final int ONE_BLOCK_SIZE_FOURTH = BLOCK_SIZE / 4;+ private static final int TWO_BLOCK_SIZE_FOURTHS = BLOCK_SIZE / 2;+ private static final int THREE_BLOCK_SIZE_FOURTHS = 3 * BLOCK_SIZE / 4;++ private static final int ONE_BLOCK_SIZE_EIGHT = BLOCK_SIZE / 8;+ private static final int TWO_BLOCK_SIZE_EIGHTS = BLOCK_SIZE / 4;+ private static final int THREE_BLOCK_SIZE_EIGHTS = 3 * BLOCK_SIZE / 8;+ private static final int FOUR_BLOCK_SIZE_EIGHTS = BLOCK_SIZE / 2;+ private static final int FIVE_BLOCK_SIZE_EIGHTS = 5 * BLOCK_SIZE / 8;+ private static final int SIX_BLOCK_SIZE_EIGHTS = 3 * BLOCK_SIZE / 4;+ private static final int SEVEN_BLOCK_SIZE_EIGHTS = 7 * BLOCK_SIZE / 8;++ // IDENTITY_PLUS_ONE[i] == i+1+ private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];++ static {+ for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {+ IDENTITY_PLUS_ONE[i] = i + 1;+ }+ }++ private static void prefixSumOfOnes(long[] arr, long base) {+ System.arraycopy(IDENTITY_PLUS_ONE, 0, arr, 0, ForUtil.BLOCK_SIZE);+ // This loop gets auto-vectorized+ for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {+ arr[i] += base;+ }+ }++ private static void prefixSum8(long[] arr, long base) {+ // When the number of bits per value is 4 or less, we can sum up all values in a block without+ // risking overflowing a 8-bits integer. This allows computing the prefix sum by summing up 8+ // values at once.+ innerPrefixSum8(arr);+ expand8(arr);+ final long l0 = base;+ final long l1 = l0 + arr[ONE_BLOCK_SIZE_EIGHT - 1];+ final long l2 = l1 + arr[TWO_BLOCK_SIZE_EIGHTS - 1];+ final long l3 = l2 + arr[THREE_BLOCK_SIZE_EIGHTS - 1];+ final long l4 = l3 + arr[FOUR_BLOCK_SIZE_EIGHTS - 1];+ final long l5 = l4 + arr[FIVE_BLOCK_SIZE_EIGHTS - 1];+ final long l6 = l5 + arr[SIX_BLOCK_SIZE_EIGHTS - 1];+ final long l7 = l6 + arr[SEVEN_BLOCK_SIZE_EIGHTS - 1];++ for (int i = 0; i < ONE_BLOCK_SIZE_EIGHT; ++i) {+ arr[i] += l0;+ arr[ONE_BLOCK_SIZE_EIGHT + i] += l1;+ arr[TWO_BLOCK_SIZE_EIGHTS + i] += l2;+ arr[THREE_BLOCK_SIZE_EIGHTS + i] += l3;+ arr[FOUR_BLOCK_SIZE_EIGHTS + i] += l4;+ arr[FIVE_BLOCK_SIZE_EIGHTS + i] += l5;+ arr[SIX_BLOCK_SIZE_EIGHTS + i] += l6;+ arr[SEVEN_BLOCK_SIZE_EIGHTS + i] += l7;+ }+ }++ private static void prefixSum16(long[] arr, long base) {+ // When the number of bits per value is 11 or less, we can sum up all values in a block without+ // risking overflowing a 16-bits integer. This allows computing the prefix sum by summing up 4+ // values at once.+ innerPrefixSum16(arr);+ expand16(arr);+ final long l0 = base;+ final long l1 = l0 + arr[ONE_BLOCK_SIZE_FOURTH - 1];+ final long l2 = l1 + arr[TWO_BLOCK_SIZE_FOURTHS - 1];+ final long l3 = l2 + arr[THREE_BLOCK_SIZE_FOURTHS - 1];++ for (int i = 0; i < ONE_BLOCK_SIZE_FOURTH; ++i) {+ arr[i] += l0;+ arr[ONE_BLOCK_SIZE_FOURTH + i] += l1;+ arr[TWO_BLOCK_SIZE_FOURTHS + i] += l2;+ arr[THREE_BLOCK_SIZE_FOURTHS + i] += l3;+ }+ }++ private static void prefixSum32(long[] arr, long base) {+ arr[0] += base << 32;+ innerPrefixSum32(arr);+ expand32(arr);+ final long l = arr[BLOCK_SIZE/2-1];+ for (int i = BLOCK_SIZE/2; i < BLOCK_SIZE; ++i) {+ arr[i] += l;+ }+ }++ // For some reason unrolling seems to help+ private static void innerPrefixSum8(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ }++ // For some reason unrolling seems to help+ private static void innerPrefixSum16(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ arr[16] += arr[15];+ arr[17] += arr[16];+ arr[18] += arr[17];+ arr[19] += arr[18];+ arr[20] += arr[19];+ arr[21] += arr[20];+ arr[22] += arr[21];+ arr[23] += arr[22];+ arr[24] += arr[23];+ arr[25] += arr[24];+ arr[26] += arr[25];+ arr[27] += arr[26];+ arr[28] += arr[27];+ arr[29] += arr[28];+ arr[30] += arr[29];+ arr[31] += arr[30];+ }++ // For some reason unrolling seems to help+ private static void innerPrefixSum32(long[] arr) {+ arr[1] += arr[0];+ arr[2] += arr[1];+ arr[3] += arr[2];+ arr[4] += arr[3];+ arr[5] += arr[4];+ arr[6] += arr[5];+ arr[7] += arr[6];+ arr[8] += arr[7];+ arr[9] += arr[8];+ arr[10] += arr[9];+ arr[11] += arr[10];+ arr[12] += arr[11];+ arr[13] += arr[12];+ arr[14] += arr[13];+ arr[15] += arr[14];+ arr[16] += arr[15];+ arr[17] += arr[16];+ arr[18] += arr[17];+ arr[19] += arr[18];+ arr[20] += arr[19];+ arr[21] += arr[20];+ arr[22] += arr[21];+ arr[23] += arr[22];+ arr[24] += arr[23];+ arr[25] += arr[24];+ arr[26] += arr[25];+ arr[27] += arr[26];+ arr[28] += arr[27];+ arr[29] += arr[28];+ arr[30] += arr[29];+ arr[31] += arr[30];+ arr[32] += arr[31];+ arr[33] += arr[32];+ arr[34] += arr[33];+ arr[35] += arr[34];+ arr[36] += arr[35];+ arr[37] += arr[36];+ arr[38] += arr[37];+ arr[39] += arr[38];+ arr[40] += arr[39];+ arr[41] += arr[40];+ arr[42] += arr[41];+ arr[43] += arr[42];+ arr[44] += arr[43];+ arr[45] += arr[44];+ arr[46] += arr[45];+ arr[47] += arr[46];+ arr[48] += arr[47];+ arr[49] += arr[48];+ arr[50] += arr[49];+ arr[51] += arr[50];+ arr[52] += arr[51];+ arr[53] += arr[52];+ arr[54] += arr[53];+ arr[55] += arr[54];+ arr[56] += arr[55];+ arr[57] += arr[56];+ arr[58] += arr[57];+ arr[59] += arr[58];+ arr[60] += arr[59];+ arr[61] += arr[60];+ arr[62] += arr[61];+ arr[63] += arr[62];+ }++ private final long[] tmp = new long[BLOCK_SIZE / 2];++ /**+ * Encode deltas of a strictly monotonically increasing sequence of integers. The provided {@code+ * longs} are expected to be deltas between consecutive values.+ */+ void encodeDeltas(long[] longs, DataOutput out) throws IOException {+ if (longs[0] == 1 && PForUtil.allEqual(longs)) { // happens with very dense postings+ out.writeByte((byte) 0);+ } else {+ long or = 0;+ for (long l : longs) {+ or |= l;+ }+ assert or != 0;+ final int bitsPerValue = PackedInts.bitsRequired(or);+ out.writeByte((byte) bitsPerValue);++ final int primitiveSize;+ if (bitsPerValue <= 4) {+ primitiveSize = 8;+ collapse8(longs);+ } else if (bitsPerValue <= 11) {+ primitiveSize = 16;+ collapse16(longs);+ } else {+ primitiveSize = 32;+ collapse32(longs);+ }+ encode(longs, bitsPerValue, primitiveSize, out, tmp);+ }+ }++ /** Decode deltas, compute the prefix sum and add {@code base} to all decoded longs. */+ void decodeAndPrefixSum(PostingDecodingUtil pdu, long base, long[] longs) throws IOException {+ final int bitsPerValue = Byte.toUnsignedInt(pdu.in.readByte());+ if (bitsPerValue == 0) {+ prefixSumOfOnes(longs, base);+ } else {+ decodeAndPrefixSum(bitsPerValue, pdu, base, longs);+ }+ }++ void skip(IndexInput in) throws IOException {+ final int bitsPerValue = Byte.toUnsignedInt(in.readByte());+ in.skipBytes(numBytes(bitsPerValue));+ }++"""++def primitive_size_for_bpv(bpv):+ if bpv <= 4:+ # If we have 4 bits per value or less then we can compute the prefix sum of 16 longs that store 8 4-bit values each without overflowing.+ return 8+ elif bpv <= 11:+ # If we have 11 bits per value or less then we can compute the prefix sum of 32 longs that store 4 16-bit values each without overflowing.+ return 16+ else:+ # No risk of overflow with 32 bits per value+ return 32++def next_primitive(bpv):+ if bpv <= 8:+ return 8+ elif bpv <= 16:+ return 16+ else:+ return 32++def writeRemainder(bpv, next_primitive, remaining_bits_per_long, o, num_values, f):+ iteration = 1+ num_longs = bpv * num_values / remaining_bits_per_long+ while num_longs % 2 == 0 and num_values % 2 == 0:+ num_longs /= 2+ num_values /= 2+ iteration *= 2+ f.write(' for (int iter = 0, tmpIdx = 0, longsIdx = %d; iter < %d; ++iter, tmpIdx += %d, longsIdx += %d) {\n' %(o, iteration, num_longs, num_values))+ i = 0+ remaining_bits = 0+ tmp_idx = 0+ for i in range(int(num_values)):+ b = bpv+ if remaining_bits == 0:+ b -= remaining_bits_per_long+ f.write(' long l%d = tmp[tmpIdx + %d] << %d;\n' %(i, tmp_idx, b))+ else:+ b -= remaining_bits+ f.write(' long l%d = (tmp[tmpIdx + %d] & MASK%d_%d) << %d;\n' %(i, tmp_idx, next_primitive, remaining_bits, b))+ tmp_idx += 1+ while b >= remaining_bits_per_long:+ b -= remaining_bits_per_long+ f.write(' l%d |= tmp[tmpIdx + %d] << %d;\n' %(i, tmp_idx, b))+ tmp_idx += 1+ if b > 0:+ f.write(' l%d |= (tmp[tmpIdx + %d] >>> %d) & MASK%d_%d;\n' %(i, tmp_idx, remaining_bits_per_long-b, next_primitive, b))+ remaining_bits = remaining_bits_per_long-b+ f.write(' longs[longsIdx + %d] = l%d;\n' %(i, i))+ f.write(' }\n')++def writeDecode(bpv, f):+ next_primitive = primitive_size_for_bpv(bpv)+ f.write(' private static void decode%dTo%d(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {\n' %(bpv, next_primitive))+ if bpv == next_primitive:+ f.write(' pdu.in.readLongs(longs, 0, %d);\n' %(bpv*2))+ else:+ num_values_per_long = 64 / next_primitive+ remaining_bits = next_primitive % bpv+ num_iters = (next_primitive - 1) // bpv+ o = 2 * bpv * num_iters+ if remaining_bits == 0:+ f.write(' pdu.splitLongs(%d, longs, %d, %d, MASK%d_%d, longs, %d, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, bpv, next_primitive, bpv, o, next_primitive, next_primitive - num_iters * bpv))+ else:+ f.write(' pdu.splitLongs(%d, longs, %d, %d, MASK%d_%d, tmp, 0, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, bpv, next_primitive, bpv, next_primitive, next_primitive - num_iters * bpv))+ writeRemainder(bpv, next_primitive, remaining_bits, o, 128/num_values_per_long - o, f)+ f.write(' }\n')++if __name__ == '__main__':+ f = open(OUTPUT_FILE, 'w')+ f.write(HEADER)+ f.write("""+ /**+ * Delta-decode 128 integers into {@code longs}.+ */+ void decodeAndPrefixSum(int bitsPerValue, PostingDecodingUtil pdu, long base, long[] longs) throws IOException {+ switch (bitsPerValue) {+""")+ for bpv in range(1, MAX_SPECIALIZED_BITS_PER_VALUE+1):+ primitive_size = primitive_size_for_bpv(bpv)+ f.write(' case %d:\n' %bpv)+ if next_primitive(bpv) == primitive_size:+ f.write(' decode%d(pdu, tmp, longs);\n' %bpv)+ else:+ f.write(' decode%dTo%d(pdu, tmp, longs);\n' %(bpv, primitive_size))+ f.write(' prefixSum%d(longs, base);\n' %primitive_size)+ f.write(' break;\n')+ f.write(' default:\n')+ f.write(' decodeSlow(bitsPerValue, pdu, tmp, longs);\n')+ f.write(' prefixSum32(longs, base);\n')+ f.write(' break;\n')+ f.write(' }\n')+ f.write(' }\n')++ f.write('\n')+ for bpv in range(1, MAX_SPECIALIZED_BITS_PER_VALUE+1):+ if next_primitive(bpv) != primitive_size_for_bpv(bpv):+ writeDecode(bpv, f)+ if bpv < MAX_SPECIALIZED_BITS_PER_VALUE:+ f.write('\n')++ f.write('}\n')diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.py b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.pyindex 5e993e26555..1049608ca19 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.py+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/gen_ForUtil.py@@ -57,33 +57,33 @@ import org.apache.lucene.store.IndexInput;public final class ForUtil {public static final int BLOCK_SIZE = 128;- private static final int BLOCK_SIZE_LOG2 = 7;+ static final int BLOCK_SIZE_LOG2 = 7;- private static long expandMask32(long mask32) {+ static long expandMask32(long mask32) {return mask32 | (mask32 << 32);}- private static long expandMask16(long mask16) {+ static long expandMask16(long mask16) {return expandMask32(mask16 | (mask16 << 16));}- private static long expandMask8(long mask8) {+ static long expandMask8(long mask8) {return expandMask16(mask8 | (mask8 << 8));}- private static long mask32(int bitsPerValue) {+ static long mask32(int bitsPerValue) {return expandMask32((1L << bitsPerValue) - 1);}- private static long mask16(int bitsPerValue) {+ static long mask16(int bitsPerValue) {return expandMask16((1L << bitsPerValue) - 1);}- private static long mask8(int bitsPerValue) {+ static long mask8(int bitsPerValue) {return expandMask8((1L << bitsPerValue) - 1);}- private static void expand8(long[] arr) {+ static void expand8(long[] arr) {for (int i = 0; i < 16; ++i) {long l = arr[i];arr[i] = (l >>> 56) & 0xFFL;@@ -97,17 +97,7 @@ public final class ForUtil {}}- private static void expand8To32(long[] arr) {- for (int i = 0; i < 16; ++i) {- long l = arr[i];- arr[i] = (l >>> 24) & 0x000000FF000000FFL;- arr[16 + i] = (l >>> 16) & 0x000000FF000000FFL;- arr[32 + i] = (l >>> 8) & 0x000000FF000000FFL;- arr[48 + i] = l & 0x000000FF000000FFL;- }- }-- private static void collapse8(long[] arr) {+ static void collapse8(long[] arr) {for (int i = 0; i < 16; ++i) {arr[i] =(arr[i] << 56)@@ -121,7 +111,7 @@ public final class ForUtil {}}- private static void expand16(long[] arr) {+ static void expand16(long[] arr) {for (int i = 0; i < 32; ++i) {long l = arr[i];arr[i] = (l >>> 48) & 0xFFFFL;@@ -131,21 +121,13 @@ public final class ForUtil {}}- private static void expand16To32(long[] arr) {- for (int i = 0; i < 32; ++i) {- long l = arr[i];- arr[i] = (l >>> 16) & 0x0000FFFF0000FFFFL;- arr[32 + i] = l & 0x0000FFFF0000FFFFL;- }- }-- private static void collapse16(long[] arr) {+ static void collapse16(long[] arr) {for (int i = 0; i < 32; ++i) {arr[i] = (arr[i] << 48) | (arr[32 + i] << 32) | (arr[64 + i] << 16) | arr[96 + i];}}- private static void expand32(long[] arr) {+ static void expand32(long[] arr) {for (int i = 0; i < 64; ++i) {long l = arr[i];arr[i] = l >>> 32;@@ -153,123 +135,36 @@ public final class ForUtil {}}- private static void collapse32(long[] arr) {+ static void collapse32(long[] arr) {for (int i = 0; i < 64; ++i) {arr[i] = (arr[i] << 32) | arr[64 + i];}}- private static void prefixSum8(long[] arr, long base) {- expand8To32(arr);- prefixSum32(arr, base);- }-- private static void prefixSum16(long[] arr, long base) {- // We need to move to the next primitive size to avoid overflows- expand16To32(arr);- prefixSum32(arr, base);- }-- private static void prefixSum32(long[] arr, long base) {- arr[0] += base << 32;- innerPrefixSum32(arr);- expand32(arr);- final long l = arr[BLOCK_SIZE/2-1];- for (int i = BLOCK_SIZE/2; i < BLOCK_SIZE; ++i) {- arr[i] += l;- }- }-- // For some reason unrolling seems to help- private static void innerPrefixSum32(long[] arr) {- arr[1] += arr[0];- arr[2] += arr[1];- arr[3] += arr[2];- arr[4] += arr[3];- arr[5] += arr[4];- arr[6] += arr[5];- arr[7] += arr[6];- arr[8] += arr[7];- arr[9] += arr[8];- arr[10] += arr[9];- arr[11] += arr[10];- arr[12] += arr[11];- arr[13] += arr[12];- arr[14] += arr[13];- arr[15] += arr[14];- arr[16] += arr[15];- arr[17] += arr[16];- arr[18] += arr[17];- arr[19] += arr[18];- arr[20] += arr[19];- arr[21] += arr[20];- arr[22] += arr[21];- arr[23] += arr[22];- arr[24] += arr[23];- arr[25] += arr[24];- arr[26] += arr[25];- arr[27] += arr[26];- arr[28] += arr[27];- arr[29] += arr[28];- arr[30] += arr[29];- arr[31] += arr[30];- arr[32] += arr[31];- arr[33] += arr[32];- arr[34] += arr[33];- arr[35] += arr[34];- arr[36] += arr[35];- arr[37] += arr[36];- arr[38] += arr[37];- arr[39] += arr[38];- arr[40] += arr[39];- arr[41] += arr[40];- arr[42] += arr[41];- arr[43] += arr[42];- arr[44] += arr[43];- arr[45] += arr[44];- arr[46] += arr[45];- arr[47] += arr[46];- arr[48] += arr[47];- arr[49] += arr[48];- arr[50] += arr[49];- arr[51] += arr[50];- arr[52] += arr[51];- arr[53] += arr[52];- arr[54] += arr[53];- arr[55] += arr[54];- arr[56] += arr[55];- arr[57] += arr[56];- arr[58] += arr[57];- arr[59] += arr[58];- arr[60] += arr[59];- arr[61] += arr[60];- arr[62] += arr[61];- arr[63] += arr[62];- }-private final long[] tmp = new long[BLOCK_SIZE / 2];/** Encode 128 integers from {@code longs} into {@code out}. */void encode(long[] longs, int bitsPerValue, DataOutput out) throws IOException {final int nextPrimitive;- final int numLongs;if (bitsPerValue <= 8) {nextPrimitive = 8;- numLongs = BLOCK_SIZE / 8;collapse8(longs);} else if (bitsPerValue <= 16) {nextPrimitive = 16;- numLongs = BLOCK_SIZE / 4;collapse16(longs);} else {nextPrimitive = 32;- numLongs = BLOCK_SIZE / 2;collapse32(longs);}+ encode(longs, bitsPerValue, nextPrimitive, out, tmp);+ }++ static void encode(long[] longs, int bitsPerValue, int primitiveSize, DataOutput out, long[] tmp) throws IOException {+ final int numLongs = BLOCK_SIZE * primitiveSize / Long.SIZE;final int numLongsPerShift = bitsPerValue * 2;int idx = 0;- int shift = nextPrimitive - bitsPerValue;+ int shift = primitiveSize - bitsPerValue;for (int i = 0; i < numLongsPerShift; ++i) {tmp[i] = longs[idx++] << shift;}@@ -281,9 +176,9 @@ public final class ForUtil {final int remainingBitsPerLong = shift + bitsPerValue;final long maskRemainingBitsPerLong;- if (nextPrimitive == 8) {+ if (primitiveSize == 8) {maskRemainingBitsPerLong = MASKS8[remainingBitsPerLong];- } else if (nextPrimitive == 16) {+ } else if (primitiveSize == 16) {maskRemainingBitsPerLong = MASKS16[remainingBitsPerLong];} else {maskRemainingBitsPerLong = MASKS32[remainingBitsPerLong];@@ -301,10 +196,10 @@ public final class ForUtil {}} else {final long mask1, mask2;- if (nextPrimitive == 8) {+ if (primitiveSize == 8) {mask1 = MASKS8[remainingBitsPerValue];mask2 = MASKS8[remainingBitsPerLong - remainingBitsPerValue];- } else if (nextPrimitive == 16) {+ } else if (primitiveSize == 16) {mask1 = MASKS16[remainingBitsPerValue];mask2 = MASKS16[remainingBitsPerLong - remainingBitsPerValue];} else {@@ -323,26 +218,20 @@ public final class ForUtil {}/** Number of bytes required to encode 128 integers of {@code bitsPerValue} bits per value. */- int numBytes(int bitsPerValue) {+ static int numBytes(int bitsPerValue) {return bitsPerValue << (BLOCK_SIZE_LOG2 - 3);}- private static void decodeSlow(int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs)+ static void decodeSlow(int bitsPerValue, PostingDecodingUtil pdu, long[] tmp, long[] longs)throws IOException {final int numLongs = bitsPerValue << 1;final long mask = MASKS32[bitsPerValue];- pdu.splitLongs(numLongs, longs, 32 - bitsPerValue, mask, tmp, 0, -1L);- int longsIdx = numLongs;- int shift = 32 - 2 * bitsPerValue;- for (; shift >= 0; shift -= bitsPerValue) {- shiftLongs(tmp, numLongs, longs, longsIdx, shift, mask);- longsIdx += numLongs;- }- final int remainingBitsPerLong = shift + bitsPerValue;+ pdu.splitLongs(numLongs, longs, 32 - bitsPerValue, 32, mask, tmp, 0, -1L);+ final int remainingBitsPerLong = 32 - bitsPerValue;final long mask32RemainingBitsPerLong = MASKS32[remainingBitsPerLong];int tmpIdx = 0;int remainingBits = remainingBitsPerLong;- for (; longsIdx < BLOCK_SIZE / 2; ++longsIdx) {+ for (int longsIdx = numLongs; longsIdx < BLOCK_SIZE / 2; ++longsIdx) {int b = bitsPerValue - remainingBits;long l = (tmp[tmpIdx++] & MASKS32[remainingBits]) << b;while (b >= remainingBitsPerLong) {@@ -359,25 +248,6 @@ public final class ForUtil {}}- /**- * The pattern that this shiftLongs method applies is recognized by the C2 compiler, which- * generates SIMD instructions for it in order to shift multiple longs at once.- */- private static void shiftLongs(long[] a, int count, long[] b, int bi, int shift, long mask) {- for (int i = 0; i < count; ++i) {- b[bi + i] = (a[i] >>> shift) & mask;- }- }-- /**- * Likewise, but for a simple mask.- */- private static void maskLongs(long[] a, int count, long[] b, int bi, long mask) {- for (int i = 0; i < count; ++i) {- b[bi + i] = a[i] & mask;- }- }-"""def writeRemainder(bpv, next_primitive, remaining_bits_per_long, o, num_values, f):@@ -417,38 +287,26 @@ def writeDecode(bpv, f):next_primitive = 8elif bpv <= 16:next_primitive = 16- f.write(' private static void decode%d(IndexInput in, PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {\n' %bpv)+ f.write(' static void decode%d(PostingDecodingUtil pdu, long[] tmp, long[] longs) throws IOException {\n' %bpv)if bpv == next_primitive:- f.write(' in.readLongs(longs, 0, %d);\n' %(bpv*2))- elif bpv * 2 == next_primitive:- f.write(' pdu.splitLongs(%d, longs, %d, MASK%d_%d, longs, %d, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, next_primitive, bpv, bpv*2, next_primitive, next_primitive - bpv))+ f.write(' pdu.in.readLongs(longs, 0, %d);\n' %(bpv*2))else:num_values_per_long = 64 / next_primitive- f.write(' pdu.splitLongs(%d, longs, %d, MASK%d_%d, tmp, 0, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, next_primitive, bpv, next_primitive, next_primitive - bpv))-- shift = next_primitive - 2 * bpv- o = 2 * bpv- while shift >= 0:- if shift == 0:- f.write(' maskLongs(tmp, %d, longs, %d, MASK%d_%d);\n' %(bpv*2, o, next_primitive, bpv))- else:- f.write(' shiftLongs(tmp, %d, longs, %d, %d, MASK%d_%d);\n' %(bpv*2, o, shift, next_primitive, bpv))- o += bpv*2- shift -= bpv- remaining_bits = shift + bpv- if remaining_bits > 0:- if remaining_bits != next_primitive - bpv:- # values in tmp still have more bits per value than remaining_bits, clear the higher bits now- f.write(' maskLongs(tmp, %d, tmp, 0, MASK%d_%d);\n' %(bpv*2, next_primitive, remaining_bits))+ remaining_bits = next_primitive % bpv+ num_iters = (next_primitive - 1) // bpv+ o = 2 * bpv * num_iters+ if remaining_bits == 0:+ f.write(' pdu.splitLongs(%d, longs, %d, %d, MASK%d_%d, longs, %d, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, bpv, next_primitive, bpv, o, next_primitive, next_primitive - num_iters * bpv))+ else:+ f.write(' pdu.splitLongs(%d, longs, %d, %d, MASK%d_%d, tmp, 0, MASK%d_%d);\n' %(bpv*2, next_primitive - bpv, bpv, next_primitive, bpv, next_primitive, next_primitive - num_iters * bpv))writeRemainder(bpv, next_primitive, remaining_bits, o, 128/num_values_per_long - o, f)f.write(' }\n')-if __name__ == '__main__':f = open(OUTPUT_FILE, 'w')f.write(HEADER)for primitive_size in PRIMITIVE_SIZE:- f.write(' private static final long[] MASKS%d = new long[%d];\n' %(primitive_size, primitive_size))+ f.write(' static final long[] MASKS%d = new long[%d];\n' %(primitive_size, primitive_size))f.write('\n')f.write(' static {\n')for primitive_size in PRIMITIVE_SIZE:@@ -462,12 +320,11 @@ if __name__ == '__main__':""")for primitive_size in PRIMITIVE_SIZE:for bpv in range(1, min(MAX_SPECIALIZED_BITS_PER_VALUE + 1, primitive_size)):- if bpv * 2 != primitive_size or primitive_size == 8:- f.write(' private static final long MASK%d_%d = MASKS%d[%d];\n' %(primitive_size, bpv, primitive_size, bpv))+ f.write(' static final long MASK%d_%d = MASKS%d[%d];\n' %(primitive_size, bpv, primitive_size, bpv))f.write("""/** Decode 128 integers into {@code longs}. */- void decode(int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long[] longs) throws IOException {+ void decode(int bitsPerValue, PostingDecodingUtil pdu, long[] longs) throws IOException {switch (bitsPerValue) {""")for bpv in range(1, MAX_SPECIALIZED_BITS_PER_VALUE+1):@@ -477,41 +334,16 @@ if __name__ == '__main__':elif bpv <= 16:next_primitive = 16f.write(' case %d:\n' %bpv)- f.write(' decode%d(in, pdu, tmp, longs);\n' %bpv)+ f.write(' decode%d(pdu, tmp, longs);\n' %bpv)f.write(' expand%d(longs);\n' %next_primitive)f.write(' break;\n')f.write(' default:\n')- f.write(' decodeSlow(bitsPerValue, in, pdu, tmp, longs);\n')+ f.write(' decodeSlow(bitsPerValue, pdu, tmp, longs);\n')f.write(' expand32(longs);\n')f.write(' break;\n')f.write(' }\n')f.write(' }\n')- f.write("""- /**- * Delta-decode 128 integers into {@code longs}.- */- void decodeAndPrefixSum(int bitsPerValue, IndexInput in, PostingDecodingUtil pdu, long base, long[] longs) throws IOException {- switch (bitsPerValue) {-""")- for bpv in range(1, MAX_SPECIALIZED_BITS_PER_VALUE+1):- next_primitive = 32- if bpv <= 8:- next_primitive = 8- elif bpv <= 16:- next_primitive = 16- f.write(' case %d:\n' %bpv)- f.write(' decode%d(in, pdu, tmp, longs);\n' %bpv)- f.write(' prefixSum%d(longs, base);\n' %next_primitive)- f.write(' break;\n')- f.write(' default:\n')- f.write(' decodeSlow(bitsPerValue, in, pdu, tmp, longs);\n')- f.write(' prefixSum32(longs, base);\n')- f.write(' break;\n')- f.write(' }\n')- f.write(' }\n')-- f.write('\n')for i in range(1, MAX_SPECIALIZED_BITS_PER_VALUE+1):writeDecode(i, f)if i < MAX_SPECIALIZED_BITS_PER_VALUE:diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultPostingDecodingUtil.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultPostingDecodingUtil.javadeleted file mode 100644index 8c68e87109c..00000000000--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultPostingDecodingUtil.java+++ /dev/null@@ -1,42 +0,0 @@-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package org.apache.lucene.internal.vectorization;--import java.io.IOException;-import org.apache.lucene.store.IndexInput;--final class DefaultPostingDecodingUtil extends PostingDecodingUtil {-- protected final IndexInput in;-- public DefaultPostingDecodingUtil(IndexInput in) {- this.in = in;- }-- @Override- public void splitLongs(- int count, long[] b, int bShift, long bMask, long[] c, int cIndex, long cMask)- throws IOException {- assert count <= 64;- in.readLongs(c, cIndex, count);- // The below loop is auto-vectorized- for (int i = 0; i < count; ++i) {- b[i] = (c[cIndex + i] >>> bShift) & bMask;- c[cIndex + i] &= cMask;- }- }-}diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorizationProvider.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorizationProvider.javaindex 2127a594117..c5e9301e9bc 100644--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorizationProvider.java+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorizationProvider.java@@ -42,6 +42,6 @@ final class DefaultVectorizationProvider extends VectorizationProvider {@Overridepublic PostingDecodingUtil newPostingDecodingUtil(IndexInput input) {- return new DefaultPostingDecodingUtil(input);+ return new PostingDecodingUtil(input);}}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.javaindex d5928959e28..4dfa89422d0 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@@ -17,16 +17,42 @@package org.apache.lucene.internal.vectorization;import java.io.IOException;+import org.apache.lucene.store.IndexInput;/** Utility class to decode postings. */-public abstract class PostingDecodingUtil {+public class PostingDecodingUtil {++ /** The wrapper {@link IndexInput}. */+ public final IndexInput in;++ /** Sole constructor, called by sub-classes. */+ protected PostingDecodingUtil(IndexInput in) {+ this.in = in;+ }/**- * Read {@code count} longs. This number must not exceed 64. Apply shift {@code bShift} and mask- * {@code bMask} and store the result in {@code b} starting at offset 0. Apply mask {@code cMask}- * and store the result in {@code c} starting at offset {@code cIndex}.+ * Core methods for decoding blocks of docs / freqs / positions / offsets.+ *+ * <ul>+ * <li>Read {@code count} longs.+ * <li>For all {@code i} >= 0 so that {@code bShift - i * dec} > 0, apply shift {@code+ * bShift - i * dec} and store the result in {@code b} at offset {@code count * i}.+ * <li>Apply mask {@code cMask} and store the result in {@code c} starting at offset {@code+ * cIndex}.+ * </ul>*/- public abstract void splitLongs(- int count, long[] b, int bShift, long bMask, long[] c, int cIndex, long cMask)- throws IOException;+ public void splitLongs(+ int count, long[] b, int bShift, int dec, long bMask, long[] c, int cIndex, long cMask)+ throws IOException {+ // Default implementation, which takes advantage of the C2 compiler's loop unrolling and+ // auto-vectorization.+ in.readLongs(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;+ }+ c[cIndex + i] &= cMask;+ }+ }}diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.javaindex eeb1830fc91..46dabb3427e 100644--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java@@ -190,6 +190,7 @@ public abstract class VectorizationProvider {Set.of("org.apache.lucene.codecs.hnsw.FlatVectorScorerUtil","org.apache.lucene.util.VectorUtil",+ "org.apache.lucene.codecs.lucene912.Lucene912PostingsReader","org.apache.lucene.codecs.lucene912.PostingIndexInput");private static void ensureCaller() {diff --git a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/MemorySegmentPostingDecodingUtil.java b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/MemorySegmentPostingDecodingUtil.javaindex 7b4bc32bccf..582fab392c7 100644--- a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/MemorySegmentPostingDecodingUtil.java+++ b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/MemorySegmentPostingDecodingUtil.java@@ -29,57 +29,57 @@ final class MemorySegmentPostingDecodingUtil extends PostingDecodingUtil {private static final VectorSpecies<Long> LONG_SPECIES =PanamaVectorConstants.PRERERRED_LONG_SPECIES;- private final IndexInput in;private final MemorySegment memorySegment;MemorySegmentPostingDecodingUtil(IndexInput in, MemorySegment memorySegment) {- this.in = in;+ super(in);this.memorySegment = memorySegment;}+ private static void shift(+ LongVector vector, int bShift, int dec, int maxIter, long bMask, long[] b, int count, int i) {+ for (int j = 0; j <= maxIter; ++j) {+ vector+ .lanewise(VectorOperators.LSHR, bShift - j * dec)+ .lanewise(VectorOperators.AND, bMask)+ .intoArray(b, count * j + i);+ }+ }+@Overridepublic void splitLongs(- int count, long[] b, int bShift, long bMask, long[] c, int cIndex, long cMask)+ int count, long[] b, int bShift, int dec, long bMask, long[] c, int cIndex, long cMask)throws IOException {if (count < LONG_SPECIES.length()) {// Not enough data to vectorize without going out-of-bounds. In practice, this branch is never// used if the bit width is 256, and is used for 2 and 3 bits per value if the bit width is// 512.- in.readLongs(c, cIndex, count);- for (int i = 0; i < count; ++i) {- b[i] = (c[cIndex + i] >>> bShift) & bMask;- c[cIndex + i] &= cMask;- }- } else {- long offset = in.getFilePointer();- long endOffset = offset + count * Long.BYTES;- int loopBound = LONG_SPECIES.loopBound(count - 1);- for (int i = 0;- i < loopBound;- i += LONG_SPECIES.length(), offset += LONG_SPECIES.length() * Long.BYTES) {- LongVector vector =- LongVector.fromMemorySegment(- LONG_SPECIES, memorySegment, offset, ByteOrder.LITTLE_ENDIAN);- vector- .lanewise(VectorOperators.LSHR, bShift)- .lanewise(VectorOperators.AND, bMask)- .intoArray(b, i);- vector.lanewise(VectorOperators.AND, cMask).intoArray(c, cIndex + i);- }+ super.splitLongs(count, b, bShift, dec, bMask, c, cIndex, cMask);+ return;+ }- // Handle the tail by reading a vector that is aligned with with `count` on the right side.- int i = count - LONG_SPECIES.length();- offset = endOffset - LONG_SPECIES.length() * Long.BYTES;+ int maxIter = (bShift - 1) / dec;+ long offset = in.getFilePointer();+ long endOffset = offset + count * Long.BYTES;+ int loopBound = LONG_SPECIES.loopBound(count - 1);+ for (int i = 0;+ i < loopBound;+ i += LONG_SPECIES.length(), offset += LONG_SPECIES.length() * Long.BYTES) {LongVector vector =LongVector.fromMemorySegment(LONG_SPECIES, memorySegment, offset, ByteOrder.LITTLE_ENDIAN);- vector- .lanewise(VectorOperators.LSHR, bShift)- .lanewise(VectorOperators.AND, bMask)- .intoArray(b, i);+ shift(vector, bShift, dec, maxIter, bMask, b, count, i);vector.lanewise(VectorOperators.AND, cMask).intoArray(c, cIndex + i);-- in.seek(endOffset);}++ // Handle the tail by reading a vector that is aligned with `count` on the right side.+ int i = count - LONG_SPECIES.length();+ offset = endOffset - LONG_SPECIES.length() * Long.BYTES;+ LongVector vector =+ LongVector.fromMemorySegment(LONG_SPECIES, memorySegment, offset, ByteOrder.LITTLE_ENDIAN);+ shift(vector, bShift, dec, maxIter, bMask, b, count, i);+ vector.lanewise(VectorOperators.AND, cMask).intoArray(c, cIndex + i);++ in.seek(endOffset);}}diff --git a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.javaindex 0e060586c2a..df9d35bc439 100644--- a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java+++ b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java@@ -95,6 +95,6 @@ final class PanamaVectorizationProvider extends VectorizationProvider {return new MemorySegmentPostingDecodingUtil(input, ms);}}- return new DefaultPostingDecodingUtil(input);+ return new PostingDecodingUtil(input);}}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForDeltaUtil.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForDeltaUtil.javaindex 93ad6b3b6b2..3363b8a2202 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForDeltaUtil.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForDeltaUtil.java@@ -19,6 +19,7 @@ package org.apache.lucene.codecs.lucene912;import com.carrotsearch.randomizedtesting.generators.RandomNumbers;import java.io.IOException;import java.util.Arrays;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.ByteBuffersDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;@@ -48,7 +49,7 @@ public class TestForDeltaUtil extends LuceneTestCase {{// encodeIndexOutput out = d.createOutput("test.bin", IOContext.DEFAULT);- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(new ForUtil());+ final ForDeltaUtil forDeltaUtil = new ForDeltaUtil();for (int i = 0; i < iterations; ++i) {long[] source = new long[ForUtil.BLOCK_SIZE];@@ -64,13 +65,13 @@ public class TestForDeltaUtil extends LuceneTestCase {{// decodeIndexInput in = d.openInput("test.bin", IOContext.READONCE);- ForUtil forUtil = new ForUtil();- PostingIndexInput postingIn = new PostingIndexInput(in, forUtil);- final ForDeltaUtil forDeltaUtil = new ForDeltaUtil(forUtil);+ PostingDecodingUtil pdu =+ Lucene912PostingsReader.VECTORIZATION_PROVIDER.newPostingDecodingUtil(in);+ ForDeltaUtil forDeltaUtil = new ForDeltaUtil();for (int i = 0; i < iterations; ++i) {long base = 0;final long[] restored = new long[ForUtil.BLOCK_SIZE];- forDeltaUtil.decodeAndPrefixSum(postingIn, base, restored);+ forDeltaUtil.decodeAndPrefixSum(pdu, base, restored);final long[] expected = new long[ForUtil.BLOCK_SIZE];for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) {expected[j] = values[i * ForUtil.BLOCK_SIZE + j];diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForUtil.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForUtil.javaindex dec60fc6762..df78e045fbb 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForUtil.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestForUtil.java@@ -19,6 +19,7 @@ package org.apache.lucene.codecs.lucene912;import com.carrotsearch.randomizedtesting.generators.RandomNumbers;import java.io.IOException;import java.util.Arrays;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.ByteBuffersDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;@@ -69,13 +70,14 @@ public class TestForUtil extends LuceneTestCase {{// decodeIndexInput in = d.openInput("test.bin", IOContext.READONCE);+ PostingDecodingUtil pdu =+ Lucene912PostingsReader.VECTORIZATION_PROVIDER.newPostingDecodingUtil(in);ForUtil forUtil = new ForUtil();- PostingIndexInput postingIn = new PostingIndexInput(in, forUtil);for (int i = 0; i < iterations; ++i) {final int bitsPerValue = in.readByte();final long currentFilePointer = in.getFilePointer();final long[] restored = new long[ForUtil.BLOCK_SIZE];- postingIn.decode(bitsPerValue, restored);+ forUtil.decode(bitsPerValue, pdu, restored);int[] ints = new int[ForUtil.BLOCK_SIZE];for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) {ints[j] = Math.toIntExact(restored[j]);@@ -84,7 +86,7 @@ public class TestForUtil extends LuceneTestCase {Arrays.toString(ints),ArrayUtil.copyOfSubArray(values, i * ForUtil.BLOCK_SIZE, (i + 1) * ForUtil.BLOCK_SIZE),ints);- assertEquals(forUtil.numBytes(bitsPerValue), in.getFilePointer() - currentFilePointer);+ assertEquals(ForUtil.numBytes(bitsPerValue), in.getFilePointer() - currentFilePointer);}assertEquals(endPointer, in.getFilePointer());in.close();diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestPForUtil.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestPForUtil.javaindex d185aff7645..0740a8a708b 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestPForUtil.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene912/TestPForUtil.java@@ -19,6 +19,7 @@ package org.apache.lucene.codecs.lucene912;import com.carrotsearch.randomizedtesting.generators.RandomNumbers;import java.io.IOException;import java.util.Arrays;+import org.apache.lucene.internal.vectorization.PostingDecodingUtil;import org.apache.lucene.store.ByteBuffersDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;@@ -40,7 +41,8 @@ public class TestPForUtil extends LuceneTestCase {ForUtil forUtil = new ForUtil();IndexInput in = d.openInput("test.bin", IOContext.READONCE);- PostingIndexInput postingIn = new PostingIndexInput(in, forUtil);+ PostingDecodingUtil pdu =+ Lucene912PostingsReader.VECTORIZATION_PROVIDER.newPostingDecodingUtil(in);final PForUtil pforUtil = new PForUtil(forUtil);for (int i = 0; i < iterations; ++i) {if (random().nextInt(5) == 0) {@@ -48,7 +50,7 @@ public class TestPForUtil extends LuceneTestCase {continue;}final long[] restored = new long[ForUtil.BLOCK_SIZE];- pforUtil.decode(postingIn, restored);+ pforUtil.decode(pdu, restored);int[] ints = new int[ForUtil.BLOCK_SIZE];for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) {ints[j] = Math.toIntExact(restored[j]);diff --git a/lucene/core/src/test/org/apache/lucene/internal/vectorization/TestPostingDecodingUtil.java b/lucene/core/src/test/org/apache/lucene/internal/vectorization/TestPostingDecodingUtil.javaindex 64d1c23930e..517b3ec32aa 100644--- a/lucene/core/src/test/org/apache/lucene/internal/vectorization/TestPostingDecodingUtil.java+++ b/lucene/core/src/test/org/apache/lucene/internal/vectorization/TestPostingDecodingUtil.java@@ -51,8 +51,10 @@ public class TestPostingDecodingUtil extends LuceneTestCase {expectedC[i] = random().nextLong();actualC[i] = expectedC[i];}- int count = TestUtil.nextInt(random(), 1, 64);int bShift = TestUtil.nextInt(random(), 1, 31);+ int dec = TestUtil.nextInt(random(), 1, bShift);+ int numIters = (bShift + dec - 1) / dec;+ int count = TestUtil.nextInt(random(), 1, 64 / numIters);long bMask = random().nextLong();int cIndex = random().nextInt(64);long cMask = random().nextLong();@@ -63,14 +65,14 @@ public class TestPostingDecodingUtil extends LuceneTestCase {// padding bytes.IndexInput slice = in.slice("test", 0, startFP + count * Long.BYTES);- PostingDecodingUtil defaultUtil = new DefaultPostingDecodingUtil(slice);+ PostingDecodingUtil defaultUtil = new PostingDecodingUtil(slice);PostingDecodingUtil optimizedUtil = vectorizationProvider.newPostingDecodingUtil(slice);slice.seek(startFP);- defaultUtil.splitLongs(count, expectedB, bShift, bMask, expectedC, cIndex, cMask);+ defaultUtil.splitLongs(count, expectedB, bShift, dec, bMask, expectedC, cIndex, cMask);long expectedEndFP = slice.getFilePointer();slice.seek(startFP);- optimizedUtil.splitLongs(count, actualB, bShift, bMask, actualC, cIndex, cMask);+ optimizedUtil.splitLongs(count, actualB, bShift, dec, bMask, actualC, cIndex, cMask);assertEquals(expectedEndFP, slice.getFilePointer());assertArrayEquals(expectedB, actualB);assertArrayEquals(expectedC, actualC);