Skip to content
Open
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
5 changes: 4 additions & 1 deletion lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ Improvements

Optimizations
---------------------
(No changes)
* GITHUB#16282: Add fast paths to FixedBitSet.copyOf() for SparseLiveDocs and DenseLiveDocs,
avoiding the O(maxDoc) generic fallback. SparseLiveDocs now copies in O(deletedDocs) by
iterating only deleted doc IDs; DenseLiveDocs copies in O(maxDoc/64) by cloning the backing
FixedBitSet directly. (salvatorecampagna)

Bug Fixes
---------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.benchmark.jmh;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.lucene.util.DenseLiveDocs;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.SparseFixedBitSet;
import org.apache.lucene.util.SparseLiveDocs;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Benchmarks {@link FixedBitSet#copyOf(org.apache.lucene.util.Bits)} for {@link SparseLiveDocs} and
* {@link DenseLiveDocs} inputs.
*
* <p>This benchmark measures the speedup from the fast paths added to {@code copyOf()} for the
* {@link SparseLiveDocs} and {@link DenseLiveDocs} types introduced by GITHUB#15413. Without these
* fast paths, both types fall through to the generic O(maxDoc) loop. With them:
*
* <ul>
* <li>{@link SparseLiveDocs}: O(deletedDocs) by iterating only the set bits of the deleted-docs
* bitset, then clearing those positions in the result
* <li>{@link DenseLiveDocs}: O(maxDoc/64) by cloning the backing {@link FixedBitSet} directly
* </ul>
*
* <h2>Usage</h2>
*
* <p>Run all benchmarks:
*
* <pre>
* java -jar lucene-benchmark-jmh.jar "LiveDocsCopyOfBenchmark"
* </pre>
*
* @see SparseLiveDocs
* @see DenseLiveDocs
* @see LiveDocsBenchmark
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 3, time = 2)
@Measurement(iterations = 5, time = 3)
@Fork(
value = 1,
jvmArgsAppend = {"-Xmx2g", "-Xms2g"})
public class LiveDocsCopyOfBenchmark {

/** Number of documents in the segment. */
@Param({"1000000", "10000000", "100000000"})
int maxDoc;

/**
* Percentage of documents to delete.
*
* <p>Kept low to stay in the SparseLiveDocs regime ({@literal <=}1%). At these rates the
* O(deletedDocs) vs O(maxDoc) difference is most pronounced.
*/
@Param({"0.001", "0.01"})
double deletionRate;

private SparseLiveDocs sparseLiveDocs;
private DenseLiveDocs denseLiveDocs;

@Setup(Level.Trial)
public void setup() {
Random random = new Random(42);
int numDeleted = Math.max(1, (int) (maxDoc * deletionRate));

SparseFixedBitSet sparseSet = new SparseFixedBitSet(maxDoc);
FixedBitSet fixedSet = new FixedBitSet(maxDoc);
fixedSet.set(0, maxDoc);

for (int i = 0; i < numDeleted; i++) {
int doc = random.nextInt(maxDoc);
sparseSet.set(doc);
fixedSet.clear(doc);
}

sparseLiveDocs = SparseLiveDocs.builder(sparseSet, maxDoc).build();
denseLiveDocs = DenseLiveDocs.builder(fixedSet, maxDoc).build();
}

@Benchmark
public FixedBitSet copyOfSparseLiveDocs() {
return FixedBitSet.copyOf(sparseLiveDocs);
}

@Benchmark
public FixedBitSet copyOfDenseLiveDocs() {
return FixedBitSet.copyOf(denseLiveDocs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public int deletedCount() {
return deletedCount;
}

FixedBitSet toFixedBitSet() {
return liveDocs.clone();
}

/**
* Returns the memory usage in bytes.
*
Expand Down
4 changes: 4 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,10 @@ public static FixedBitSet copyOf(Bits bits) {

if (bits instanceof FixedBitSet fbs) {
return fbs.clone();
} else if (bits instanceof DenseLiveDocs denseLiveDocs) {
return denseLiveDocs.toFixedBitSet();
} else if (bits instanceof SparseLiveDocs sparseLiveDocs) {
return sparseLiveDocs.toFixedBitSet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have an interface (Have FixedBitSet, DenseLiveDocs, and SparseLiveDocs all implement it) which could be used here instead of multiple if/else if?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. The interface would make copyOf() cleaner, but the tricky part is that FixedBitSet itself would also need to implement it (to handle the case after the FixedBits unwrap at the top of the method). That means adding a toFixedBitSet() method to FixedBitSet whose only implementation is return clone(), which feels redundant and a bit odd semantically. Happy to go that route if the consensus is that the cleaner dispatch is worth it, but leaning toward keeping the instanceof chain since it mirrors the existing pattern already in the method for FixedBits/FixedBitSet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, if the interface only covers DenseLiveDocs and SparseLiveDocs (not FixedBitSet), the semantic oddity goes away. Is that what you had in mind?

@shubhamsrkdev shubhamsrkdev Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either way is fine - if it leads to a reduction of instanceof, not a huge fan of it (if it keeps on branching)

} else {
int length = bits.length();
FixedBitSet bitSet = new FixedBitSet(length);
Expand Down
11 changes: 11 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/SparseLiveDocs.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ public int deletedCount() {
return deletedCount;
}

FixedBitSet toFixedBitSet() {
FixedBitSet result = new FixedBitSet(maxDoc);
result.set(0, maxDoc);
for (int doc = deletedDocs.nextSetBit(0);
doc != DocIdSetIterator.NO_MORE_DOCS;
doc = deletedDocs.nextSetBit(doc + 1)) {
result.clear(doc);
}
return result;
}

/**
* Returns the memory usage in bytes.
*
Expand Down
37 changes: 37 additions & 0 deletions lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,41 @@ public void testOrMaskStraddling() {
}
}
}

public void testCopyOfDenseLiveDocs() {
final int maxDoc = atLeast(1000);
final int numDeleted = random().nextInt(maxDoc / 2) + 1;

FixedBitSet liveDocsBitSet = new FixedBitSet(maxDoc);
liveDocsBitSet.set(0, maxDoc);
for (int i = 0; i < numDeleted; i++) {
liveDocsBitSet.clear(random().nextInt(maxDoc));
}
DenseLiveDocs dense = DenseLiveDocs.builder(liveDocsBitSet, maxDoc).build();

FixedBitSet result = FixedBitSet.copyOf(dense);

assertEquals(maxDoc, result.length());
for (int doc = 0; doc < maxDoc; doc++) {
assertEquals("mismatch at doc " + doc, dense.get(doc), result.get(doc));
}
}

public void testCopyOfSparseLiveDocs() {
final int maxDoc = atLeast(1000);
final int numDeleted = random().nextInt(Math.max(1, maxDoc / 100)) + 1;

SparseFixedBitSet deletedDocsBitSet = new SparseFixedBitSet(maxDoc);
for (int i = 0; i < numDeleted; i++) {
deletedDocsBitSet.set(random().nextInt(maxDoc));
}
SparseLiveDocs sparse = SparseLiveDocs.builder(deletedDocsBitSet, maxDoc).build();

FixedBitSet result = FixedBitSet.copyOf(sparse);

assertEquals(maxDoc, result.length());
for (int doc = 0; doc < maxDoc; doc++) {
assertEquals("mismatch at doc " + doc, sparse.get(doc), result.get(doc));
}
}
}
Loading