Skip to content

Apache Lucene open-source search software

Use FixedBitSet#cardinality for counting liveDocs in CheckIndex (#15045)

Zhang Chao039e7874f7c8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index b37ebf747db..3181ef0061e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -264,6 +264,8 @@ Optimizations
* GITHUB#15083: Use DocValuesSkippers in SortedNumericDocValuesRangeQuery#rewrite(). (Alan Woodward)
+* GITHUB#15045: Use FixedBitSet#cardinality for counting liveDocs in CheckIndex (Zhang Chao)
+
Changes in Runtime Behavior
---------------------
* GITHUB#14823: Decrease TieredMergePolicy's default number of segments per
diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
index f45c3fae98d..9b7ee2ff5d0 100644
--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
@@ -1298,12 +1298,7 @@ public final class CheckIndex implements Closeable {
if (liveDocs == null) {
throw new CheckIndexException("segment should have deletions, but liveDocs is null");
} else {
- int numLive = 0;
- for (int j = 0; j < liveDocs.length(); j++) {
- if (liveDocs.get(j)) {
- numLive++;
- }
- }
+ int numLive = bitsCardinality(liveDocs);
if (numLive != numDocs) {
throw new CheckIndexException(
"liveDocs count mismatch: info=" + numDocs + ", vs bits=" + numLive);
@@ -1349,6 +1344,28 @@ public final class CheckIndex implements Closeable {
return status;
}
+ /**
+ * Returns the cardinality of the given {@code Bits}.
+ *
+ * <p>This method processes bits in batches of 1024 using {@link Bits#applyMask} and {@link
+ * FixedBitSet#cardinality}, which is faster than checking bits one by one.
+ */
+ static int bitsCardinality(Bits bits) {
+ int cardinality = 0;
+ FixedBitSet copy = new FixedBitSet(1024);
+ for (int offset = 0; offset < bits.length(); offset += copy.length()) {
+ int numBitsToCopy = Math.min(bits.length() - offset, copy.length());
+ copy.set(0, copy.length());
+ if (numBitsToCopy < copy.length()) {
+ // Clear ghost bits
+ copy.clear(numBitsToCopy, copy.length());
+ }
+ bits.applyMask(copy, offset);
+ cardinality += copy.cardinality();
+ }
+ return cardinality;
+ }
+
/** Test field infos. */
public static Status.FieldInfoStatus testFieldInfos(
CodecReader reader, PrintStream infoStream, boolean failFast) throws IOException {