Skip to content

Apache Lucene open-source search software

skip the per-bit loop in MergingHnswGraphBuilder (#16155)

sasilekhaf6863337746f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1e88ad8715e..6741ae45603 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -479,6 +479,8 @@ Optimizations
* GITHUB#16119: Optimize byte array writes in OutputStreamIndexOutput to remove unnecessary overhead. (neoremind)
+* GITHUB#16155: Use BitSet#nextClearBit to iterate uninitialized nodes in MergingHnswGraphBuilder. (Sasilekha R)
+
Bug Fixes
---------------------
* GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
index 664ed67082b..f5506dbaf8d 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/MergingHnswGraphBuilder.java
@@ -128,12 +128,14 @@ public final class MergingHnswGraphBuilder extends HnswGraphBuilder {
updateGraph(graphs[i], ordMaps[i]);
}
- // TODO: optimize to iterate only over unset bits in initializedNodes
- if (initializedNodes != null) {
- for (int node = 0; node < maxOrd; node++) {
- if (initializedNodes.get(node) == false) {
- addGraphNode(node);
- }
+ if (initializedNodes != null && maxOrd > 0) {
+ for (int node = initializedNodes.nextClearBit(0, maxOrd);
+ node != NO_MORE_DOCS;
+ node =
+ (node + 1 < maxOrd)
+ ? initializedNodes.nextClearBit(node + 1, maxOrd)
+ : NO_MORE_DOCS) {
+ addGraphNode(node);
}
}