Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

DRAFT: Vectorize ForUtil encoding for the 9.0 codec (same format) #12412

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions gradle/generation/forUtil.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
configure(project(":lucene:core")) {

task generateForUtilInternal() {
description "Regenerate gen_ForUtil.py"
description "Regenerate gen_DefaultForUtil90.py"
group "generation"

def genDir = file("src/java/org/apache/lucene/codecs/lucene90")
def genScript = file("${genDir}/gen_ForUtil.py")
def genOutput = file("${genDir}/ForUtil.java")
def genDir = file("src/java/org/apache/lucene/internal/vectorization")
def genScript = file("${genDir}/gen_DefaultForUtil90.py")
def genOutput = file("${genDir}/DefaultForUtil90.java")

inputs.file genScript
outputs.file genOutput
Expand All @@ -40,6 +40,28 @@ configure(project(":lucene:core")) {
}

regenerate.dependsOn wrapWithPersistentChecksums(generateForUtilInternal, [ andThenTasks: ["spotlessJava", "spotlessJavaApply"] ])

task generatePanamaForUtilInternal() {
description "Regenerate gen_PanamaForUtil.py"
group "generation"

def genDir = file("src/java20/org/apache/lucene/internal/vectorization")
def genScript = file("${genDir}/gen_PanamaForUtil90.py")
def genOutput = file("${genDir}/PanamaForUtil90.java")

inputs.file genScript
outputs.file genOutput

doLast {
quietExec {
workingDir genDir
executable project.externalTool("python3")
args = [ '-B', genScript ]
}
}
}

regenerate.dependsOn wrapWithPersistentChecksums(generatePanamaForUtilInternal, [ andThenTasks: ["spotlessJava", "spotlessJavaApply"] ])
}

configure(project(":lucene:backward-codecs")) {
Expand Down
4 changes: 2 additions & 2 deletions lucene/core/src/generated/checksums/generateForUtil.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"lucene/core/src/java/org/apache/lucene/codecs/lucene90/ForUtil.java": "f2091e2b7284b70c740052a9b0ee389e67eb48a9",
"lucene/core/src/java/org/apache/lucene/codecs/lucene90/gen_ForUtil.py": "7a137c58f88d68247be4368122780fa9cc8dce3e"
"lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultForUtil90.java": "8760243bd670376792b272f0a5cc54acb79c3ed6",
"lucene/core/src/java/org/apache/lucene/internal/vectorization/gen_DefaultForUtil90.py": "71cef263b135816680b29dd97a90961402aa005c"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaForUtil90.java": "5db61312b5f550c0feeade7a6cdff257ad7dbc8d",
"lucene/core/src/java20/org/apache/lucene/internal/vectorization/gen_PanamaForUtil90.py": "0d412b484d6ddb269bdb91f0203dad19cd998061"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.index.TermState;
import org.apache.lucene.internal.vectorization.ForUtil90;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.packed.PackedInts;
Expand Down Expand Up @@ -356,7 +357,7 @@ public final class Lucene90PostingsFormat extends PostingsFormat {
public static final String PAY_EXTENSION = "pay";

/** Size of blocks. */
public static final int BLOCK_SIZE = ForUtil.BLOCK_SIZE;
public static final int BLOCK_SIZE = ForUtil90.BLOCK_SIZE;

/**
* Expert: The maximum number of skip levels. Smaller values result in slightly smaller indexes,
Expand Down Expand Up @@ -449,12 +450,12 @@ public static final class IntBlockTermState extends BlockTermState {
public long payStartFP;
/**
* file offset for the start of the skip list, relative to docStartFP, if there are more than
* {@link ForUtil#BLOCK_SIZE} docs; otherwise -1
* {@link ForUtil90#BLOCK_SIZE} docs; otherwise -1
*/
public long skipOffset;
/**
* file offset for the last position in the last block, if there are more than {@link
* ForUtil#BLOCK_SIZE} positions; otherwise -1
* ForUtil90#BLOCK_SIZE} positions; otherwise -1
*/
public long lastPosBlockOffset;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package org.apache.lucene.codecs.lucene90;

import static org.apache.lucene.codecs.lucene90.ForUtil.BLOCK_SIZE;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.DOC_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.MAX_SKIP_LEVELS;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.PAY_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.POS_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.TERMS_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.VERSION_CURRENT;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.VERSION_START;
import static org.apache.lucene.internal.vectorization.ForUtil90.BLOCK_SIZE;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -39,6 +39,7 @@
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SlowImpactsEnum;
import org.apache.lucene.internal.vectorization.ForUtil90;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.ArrayUtil;
Expand Down Expand Up @@ -306,7 +307,7 @@ public ImpactsEnum impacts(FieldInfo fieldInfo, BlockTermState state, int flags)

final class BlockDocsEnum extends PostingsEnum {

final PForUtil pforUtil = new PForUtil(new ForUtil());
final PForUtil pforUtil = new PForUtil();

private final long[] docBuffer = new long[BLOCK_SIZE + 1];
private final long[] freqBuffer = new long[BLOCK_SIZE];
Expand Down Expand Up @@ -394,7 +395,7 @@ public PostingsEnum reset(IntBlockTermState termState, int flags) throws IOExcep
this.needsFreq = PostingsEnum.featureRequested(flags, PostingsEnum.FREQS);
this.isFreqsRead = true;
if (indexHasFreq == false || needsFreq == false) {
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; ++i) {
freqBuffer[i] = 1;
}
}
Expand Down Expand Up @@ -563,7 +564,7 @@ public long cost() {
// Also handles payloads + offsets
final class EverythingEnum extends PostingsEnum {

final PForUtil pforUtil = new PForUtil(new ForUtil());
final PForUtil pforUtil = new PForUtil();

private final long[] docBuffer = new long[BLOCK_SIZE + 1];
private final long[] freqBuffer = new long[BLOCK_SIZE + 1];
Expand Down Expand Up @@ -1047,7 +1048,7 @@ public long cost() {

final class BlockImpactsDocsEnum extends ImpactsEnum {

final PForUtil pforUtil = new PForUtil(new ForUtil());
final PForUtil pforUtil = new PForUtil();

private final long[] docBuffer = new long[BLOCK_SIZE + 1];
private final long[] freqBuffer = new long[BLOCK_SIZE];
Expand Down Expand Up @@ -1240,7 +1241,7 @@ public long cost() {

final class BlockImpactsPostingsEnum extends ImpactsEnum {

final PForUtil pforUtil = new PForUtil(new ForUtil());
final PForUtil pforUtil = new PForUtil();

private final long[] docBuffer = new long[BLOCK_SIZE];
private final long[] freqBuffer = new long[BLOCK_SIZE];
Expand Down Expand Up @@ -1532,7 +1533,7 @@ public long cost() {

final class BlockImpactsEverythingEnum extends ImpactsEnum {

final PForUtil pforUtil = new PForUtil(new ForUtil());
final PForUtil pforUtil = new PForUtil();

private final long[] docBuffer = new long[BLOCK_SIZE];
private final long[] freqBuffer = new long[BLOCK_SIZE];
Expand Down Expand Up @@ -1696,7 +1697,7 @@ public BlockImpactsEverythingEnum(FieldInfo fieldInfo, IntBlockTermState termSta
docFreq);

if (indexHasFreq == false) {
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; ++i) {
freqBuffer[i] = 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.apache.lucene.codecs.lucene90;

import static org.apache.lucene.codecs.lucene90.ForUtil.BLOCK_SIZE;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.DOC_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.MAX_SKIP_LEVELS;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.PAY_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.POS_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.TERMS_CODEC;
import static org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat.VERSION_CURRENT;
import static org.apache.lucene.internal.vectorization.ForUtil90.BLOCK_SIZE;

import java.io.IOException;
import org.apache.lucene.codecs.BlockTermState;
Expand Down Expand Up @@ -110,7 +110,7 @@ public Lucene90PostingsWriter(SegmentWriteState state) throws IOException {
try {
CodecUtil.writeIndexHeader(
docOut, DOC_CODEC, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
pforUtil = new PForUtil(new ForUtil());
pforUtil = new PForUtil();
if (state.fieldInfos.hasProx()) {
posDeltaBuffer = new long[BLOCK_SIZE];
String posFileName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.codecs.MultiLevelSkipListReader;
import org.apache.lucene.internal.vectorization.ForUtil90;
import org.apache.lucene.store.IndexInput;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public Lucene90SkipReader(
boolean hasPos,
boolean hasOffsets,
boolean hasPayloads) {
super(skipStream, maxSkipLevels, ForUtil.BLOCK_SIZE, 8);
super(skipStream, maxSkipLevels, ForUtil90.BLOCK_SIZE, 8);
docPointer = new long[maxSkipLevels];
if (hasPos) {
posPointer = new long[maxSkipLevels];
Expand Down Expand Up @@ -95,7 +96,7 @@ public Lucene90SkipReader(
* the last block boundary 2. moving into the vInt block
*/
protected int trim(int df) {
return df % ForUtil.BLOCK_SIZE == 0 ? df - 1 : df;
return df % ForUtil90.BLOCK_SIZE == 0 ? df - 1 : df;
}

public void init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.internal.vectorization.ForUtil90;
import org.apache.lucene.internal.vectorization.VectorizationProvider;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.LongHeap;
Expand All @@ -26,35 +28,38 @@
/** Utility class to encode sequences of 128 small positive integers. */
final class PForUtil {

private static final VectorizationProvider VPROVIDER = VectorizationProvider.getInstance();

private static final int MAX_EXCEPTIONS = 7;
private static final int HALF_BLOCK_SIZE = ForUtil.BLOCK_SIZE / 2;
private static final int HALF_BLOCK_SIZE = ForUtil90.BLOCK_SIZE / 2;

// IDENTITY_PLUS_ONE[i] == i + 1
private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];
private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil90.BLOCK_SIZE];

static {
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; ++i) {
IDENTITY_PLUS_ONE[i] = i + 1;
}
}

static boolean allEqual(long[] l) {
for (int i = 1; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 1; i < ForUtil90.BLOCK_SIZE; ++i) {
if (l[i] != l[0]) {
return false;
}
}
return true;
}

private final ForUtil forUtil;
private final ForUtil90 forUtil;
// buffer for reading exception data; each exception uses two bytes (pos + high-order bits of the
// exception)
private final byte[] exceptionBuff = new byte[MAX_EXCEPTIONS * 2];

PForUtil(ForUtil forUtil) {
assert ForUtil.BLOCK_SIZE <= 256 : "blocksize must fit in one byte. got " + ForUtil.BLOCK_SIZE;
this.forUtil = forUtil;
PForUtil() {
assert ForUtil90.BLOCK_SIZE <= 256
: "blocksize must fit in one byte. got " + ForUtil90.BLOCK_SIZE;
this.forUtil = VPROVIDER.newForUtil90();
}

/** Encode 128 integers from {@code longs} into {@code out}. */
Expand All @@ -65,7 +70,7 @@ void encode(long[] longs, DataOutput out) throws IOException {
top.push(longs[i]);
}
long topValue = top.top();
for (int i = MAX_EXCEPTIONS + 1; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = MAX_EXCEPTIONS + 1; i < ForUtil90.BLOCK_SIZE; ++i) {
if (longs[i] > topValue) {
topValue = top.updateTop(longs[i]);
}
Expand All @@ -90,7 +95,7 @@ void encode(long[] longs, DataOutput out) throws IOException {
final byte[] exceptions = new byte[numExceptions * 2];
if (numExceptions > 0) {
int exceptionCount = 0;
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; ++i) {
if (longs[i] > maxUnpatchedValue) {
exceptions[exceptionCount * 2] = (byte) i;
exceptions[exceptionCount * 2 + 1] = (byte) (longs[i] >>> patchedBitsRequired);
Expand Down Expand Up @@ -122,7 +127,7 @@ void decode(DataInput in, long[] longs) throws IOException {
final int bitsPerValue = token & 0x1f;
final int numExceptions = token >>> 5;
if (bitsPerValue == 0) {
Arrays.fill(longs, 0, ForUtil.BLOCK_SIZE, in.readVLong());
Arrays.fill(longs, 0, ForUtil90.BLOCK_SIZE, in.readVLong());
} else {
forUtil.decode(bitsPerValue, in, longs);
}
Expand Down Expand Up @@ -184,9 +189,9 @@ void skip(DataInput in) throws IOException {
* there are no exceptions to apply.
*/
private static void prefixSumOfOnes(long[] longs, long base) {
System.arraycopy(IDENTITY_PLUS_ONE, 0, longs, 0, ForUtil.BLOCK_SIZE);
System.arraycopy(IDENTITY_PLUS_ONE, 0, longs, 0, ForUtil90.BLOCK_SIZE);
// This loop gets auto-vectorized
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; ++i) {
longs[i] += base;
}
}
Expand All @@ -196,7 +201,7 @@ private static void prefixSumOfOnes(long[] longs, long base) {
* this assumes there are no exceptions to apply.
*/
private static void prefixSumOf(long[] longs, long base, long val) {
for (int i = 0; i < ForUtil.BLOCK_SIZE; i++) {
for (int i = 0; i < ForUtil90.BLOCK_SIZE; i++) {
longs[i] = (i + 1) * val + base;
}
}
Expand Down Expand Up @@ -232,7 +237,7 @@ private static void prefixSum32(long[] longs, long base) {
innerPrefixSum32(longs);
expand32(longs);
final long l = longs[HALF_BLOCK_SIZE - 1];
for (int i = HALF_BLOCK_SIZE; i < ForUtil.BLOCK_SIZE; ++i) {
for (int i = HALF_BLOCK_SIZE; i < ForUtil90.BLOCK_SIZE; ++i) {
longs[i] += l;
}
}
Expand Down
Loading