You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by be...@apache.org on 2025/06/23 17:09:29 UTC
(lucene) branch branch_10x updated: Sometimes use `FixedBitSet` when doing HNSW searches (#14836)
This is an automated email from the ASF dual-hosted git repository.
benwtrent pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/branch_10x by this push:
new f93a0ed318b Sometimes use `FixedBitSet` when doing HNSW searches (#14836)
f93a0ed318b is described below
commit f93a0ed318b528229dee91dbbca98cb6607b24ae
Author: Benjamin Trent <be...@gmail.com>
AuthorDate: Mon Jun 23 12:24:05 2025 -0400
Sometimes use `FixedBitSet` when doing HNSW searches (#14836)
For smaller graphs, the overhead cost of a SparseFixedBitSet shows up in the performance metrics.
This adjusts the bitset creation logic to be more similar to how we utilize [Sparse]FixedBitSet elsewhere.
The expectedVisitedNodes is both empirical and intuitive. This could possibly be refined given the number of connections within the graph, but I think this is "good enough" for now.
---
lucene/CHANGES.txt | 3 +++
.../codecs/lucene99/Lucene99HnswVectorsReader.java | 2 +-
.../apache/lucene/util/hnsw/HnswGraphSearcher.java | 29 +++++++++++++++++++++-
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index ebf3ec40e3b..dc0b7dade9c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -90,6 +90,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 when
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
index e522c81d656..c358ebd0756 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
@@ -337,7 +337,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 illogical
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java
index 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 seeded
final AbstractHnswGraphSearcher graphSearcher;