Apache Lucene open-source search software
Sometimes use `FixedBitSet` when doing HNSW searches (#14836)
Benjamin Trent685049ffb68b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e453c697790..ea219e79898 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -174,6 +174,9 @@ Optimizations* GITHUB#14774: Use IntArrayList/IntHashSet to replace usages of List/Set of Integer. (Zhang Chao)+* GITHUB#14836: Sometimes use FixedBitSet when doing HNSW searches. This slightly improves HNSW+ search performance on smaller graphs. (Ben Trent)+Bug Fixes---------------------* GITHUB#14654: ValueSource.fromDoubleValuesSource(dvs).getSortField() would throw errors whendiff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.javaindex c950edc0f2d..a83b1635032 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java@@ -331,7 +331,7 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader// Take into account if quantized? E.g. some scorer cost?int filteredDocCount = 0;// The approximate number of vectors that would be visited if we did not filter- int unfilteredVisit = (int) (Math.log(graph.size()) * knnCollector.k());+ int unfilteredVisit = HnswGraphSearcher.expectedVisitedNodes(knnCollector.k(), graph.size());if (acceptDocs instanceof BitSet bitSet) {// Use approximate cardinality as this is good enough, but ensure we don't exceed the graph// size as that is illogicaldiff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.javaindex 6f16ae70b53..74d5c1541af 100644--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java@@ -20,6 +20,7 @@ package org.apache.lucene.util.hnsw;import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;import java.io.IOException;+import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.KnnCollector;import org.apache.lucene.search.TopKnnCollector;import org.apache.lucene.search.knn.KnnSearchStrategy;@@ -41,6 +42,32 @@ public class HnswGraphSearcher extends AbstractHnswGraphSearcher {protected BitSet visited;+ /**+ * HNSW search is roughly logarithmic. This doesn't take maxConn into account, but it is a pretty+ * good approximation.+ *+ * @param k neighbors to find+ * @param graphSize size of the graph+ * @return expected number of visited nodes+ */+ public static int expectedVisitedNodes(int k, int graphSize) {+ return (int) (Math.log(graphSize) * k);+ }++ /**+ * Follows similar logic to {@link FixedBitSet#of(DocIdSetIterator, int)} to determine the best+ * bit set given the expected number of visited nodes vs total graph size.+ *+ * @param k neighbors to find+ * @param graphSize size of the graph+ * @return a bit set appropriate for the expected number of visited nodes+ */+ static BitSet createBitSet(int k, int graphSize) {+ return expectedVisitedNodes(k, graphSize) < (graphSize >>> 7)+ ? new SparseFixedBitSet(graphSize)+ : new FixedBitSet(graphSize);+ }+/*** Creates a new graph searcher.*@@ -117,7 +144,7 @@ public class HnswGraphSearcher extends AbstractHnswGraphSearcher {innerSearcher =new HnswGraphSearcher(new NeighborQueue(knnCollector.k(), true),- new SparseFixedBitSet(getGraphSize(graph)));+ createBitSet(knnCollector.k(), getGraphSize(graph)));}// Then, check if we the search strategy is seededfinal AbstractHnswGraphSearcher graphSearcher;