Apache Lucene open-source search software
Optimize column batch parent field with bulk fill (#16147)
Tim Brooks4f19629ef62c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 102800ed8ff..1f9dbae99a7 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -452,6 +452,8 @@ Optimizations* GITHUB#16141: Optimize constant-score bulk scoring via intoBitSet batching. (Costin Leau)+* GITHUB#16147: Optimize column batch parent field with bulk fill. (Tim Brooks)+Bug Fixes---------------------* GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequentdiff --git a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.javaindex 396ee24e33b..3e7517fb04b 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java@@ -716,14 +716,14 @@ final class IndexingChain implements Accountable {ColumnValidation.validateColumnHasIndexingFeature(fieldName, fieldType);- if (column instanceof BinaryColumn bc) {- ColumnValidation.validateBinaryColumn(bc, fieldType);- } else if (column instanceof LongColumn lc) {- ColumnValidation.validateLongColumn(lc, fieldType);- } else if (column instanceof DictionaryColumn dc) {- ColumnValidation.validateDictionaryColumn(dc, fieldType);- } else if (column instanceof VectorColumn<?> vc) {- ColumnValidation.validateVectorColumn(vc, fieldType);+ switch (column) {+ case BinaryColumn bc -> ColumnValidation.validateBinaryColumn(bc, fieldType);+ case LongColumn lc -> ColumnValidation.validateLongColumn(lc, fieldType);+ case DictionaryColumn dc -> ColumnValidation.validateDictionaryColumn(dc, fieldType);+ case VectorColumn<?> vc -> ColumnValidation.validateVectorColumn(vc, fieldType);+ default ->+ throw new IllegalArgumentException(+ "Unknown column type: " + column.getClass().getName());}if (fieldType.stored() || fieldType.indexOptions() != IndexOptions.NONE) {@@ -750,18 +750,8 @@ final class IndexingChain implements Accountable {validateColumnSchema(fieldName, pf, fieldType);}- // Index the parent field for every document (each batch doc is an individual document,- // not part of a block, so every doc is its own parent).if (parentPf != null) {- if (parentPf.fieldInfo == null) {- initializeFieldInfo(parentPf);- parentPf.trySetValidatedFrozenFieldType();- }- final NumericDocValuesWriter parentWriter = (NumericDocValuesWriter) parentPf.docValuesWriter;- final long value = parentField.numericValue().longValue();- for (int i = 0; i < numDocs; i++) {- parentWriter.addValue(baseDocID + i, value);- }+ processParentFieldForColumnBatch(baseDocID, numDocs);}// Row-oriented pass: stored fields and term inversion only. Uses fresh tuple cursors.@@ -797,6 +787,17 @@ final class IndexingChain implements Accountable {}}+ private void processParentFieldForColumnBatch(int baseDocID, int numDocs) throws IOException {+ if (parentPf.fieldInfo == null) {+ initializeFieldInfo(parentPf);+ parentPf.trySetValidatedFrozenFieldType();+ }+ // Index the parent field for every document (each batch doc is an individual document,+ // not part of a block, so every doc is its own parent).+ final NumericDocValuesWriter parentWriter = (NumericDocValuesWriter) parentPf.docValuesWriter;+ parentWriter.addRepeatValues(baseDocID, parentField.numericValue().longValue(), numDocs);+ }+/*** Processes row-oriented features (stored fields and term inversion) for columns that have stored* or indexed fields. The outer loop iterates every batch-local doc-id in {@code [0, numDocs)} fordiff --git a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.javaindex 35216ee4f4a..18862c71423 100644--- a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java@@ -64,6 +64,20 @@ class NumericDocValuesWriter extends DocValuesWriter<NumericDocValues> {lastDocID = docID;}+ void addRepeatValues(int firstDocID, long value, int count) {+ if (count == 0) {+ return;+ }+ assert firstDocID > lastDocID;++ pending.add(value, count);+ docsWithField.addRange(firstDocID, firstDocID + count);++ updateBytesUsed();++ lastDocID = firstDocID + count - 1;+ }+void addDenseValues(int firstDocID, LongValuesCursor cursor) {int numValues = cursor.size();if (numValues == 0) {diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.javaindex 0e2c2fea1fc..f221d123f3b 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java@@ -18,6 +18,7 @@ package org.apache.lucene.util.packed;import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;+import java.util.Arrays;import org.apache.lucene.document.column.LongValuesCursor;import org.apache.lucene.util.Accountable;import org.apache.lucene.util.ArrayUtil;@@ -241,6 +242,22 @@ public class PackedLongValues extends LongValues implements Accountable {return this;}+ /** Add {@code count} copies of {@code value} in bulk. */+ public Builder add(long value, int count) {+ if (pending == null) {+ throw new IllegalStateException("Cannot be reused after build()");+ }+ while (count > 0) {+ packIfFull();+ int toFill = Math.min(count, pending.length - pendingOff);+ Arrays.fill(pending, pendingOff, pendingOff + toFill, value);+ pendingOff += toFill;+ count -= toFill;+ size += toFill;+ }+ return this;+ }+/*** Add all values produced by the given {@link LongValuesCursor} in bulk. The cursor's {@link* LongValuesCursor#size()} is used as the bounds: exactly that many values are pulled.diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex ecdb830c208..4c87a687799 100644--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@ -1073,6 +1073,67 @@ public class TestPackedInts extends LuceneTestCase {}}+ public void testPackedLongValuesAddRepeat() {+ for (DataType dataType : DataType.values()) {+ final int pageSize = 1 << TestUtil.nextInt(random(), 6, 12);+ final float acceptableOverheadRatio = random().nextFloat();+ final PackedLongValues.Builder buf;+ switch (dataType) {+ case PACKED:+ buf = PackedLongValues.packedBuilder(pageSize, acceptableOverheadRatio);+ break;+ case DELTA_PACKED:+ buf = PackedLongValues.deltaPackedBuilder(pageSize, acceptableOverheadRatio);+ break;+ case MONOTONIC:+ buf = PackedLongValues.monotonicBuilder(pageSize, acceptableOverheadRatio);+ break;+ default:+ throw new RuntimeException("added a type and forgot to add it here?");+ }++ final List<Long> expected = new ArrayList<>();+ final int numOps = atLeast(20);+ for (int op = 0; op < numOps; ++op) {+ switch (random().nextInt(3)) {+ case 0:+ final long single = random().nextLong();+ buf.add(single);+ expected.add(single);+ break;+ case 1:+ // count == 0 must be a no-op.+ buf.add(random().nextLong(), 0);+ break;+ default:+ final long value = random().nextLong();+ final int count = TestUtil.nextInt(random(), 1, pageSize * 3);+ buf.add(value, count);+ for (int i = 0; i < count; ++i) {+ expected.add(value);+ }+ break;+ }+ assertEquals(expected.size(), buf.size());+ }++ final PackedLongValues values = buf.build();+ assertEquals(expected.size(), values.size());+ for (int i = 0; i < expected.size(); ++i) {+ assertEquals((long) expected.get(i), values.get(i));+ }++ final PackedLongValues.Iterator it = values.iterator();+ for (int i = 0; i < expected.size(); ++i) {+ assertTrue(it.hasNext());+ assertEquals((long) expected.get(i), it.next());+ }+ assertFalse(it.hasNext());++ expectThrows(IllegalStateException.class, () -> buf.add(random().nextLong(), 5));+ }+ }+public void testPackedInputOutput() throws IOException {final long[] longs = new long[random().nextInt(8192)];final int[] bitsPerValues = new int[longs.length];