You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2026/01/12 19:19:06 UTC

(lucene) branch main updated: Avoid unnecessary getGraph() call (#15560)

This is an automated email from the ASF dual-hosted git repository.

shubhamvishu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 00b9533f28f Avoid unnecessary getGraph() call (#15560)
00b9533f28f is described below

commit 00b9533f28faf7a050c547601f6f026b8eeacfa6
Author: Shubham Chaudhary <sh...@gmail.com>
AuthorDate: Mon Jan 12 14:19:00 2026 -0500

    Avoid unnecessary getGraph() call (#15560)
    
    * Avoid unnecessary getGraph() call which initializes the OffHeapHnswGraph whereas all we need is graph size
---
 lucene/CHANGES.txt                                                | 2 ++
 .../apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java  | 8 ++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6fa42526f92..fc9c93c614c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -292,6 +292,8 @@ Optimizations
 
 * GITHUB#15511: Dynamic pruning for SORTED(_SET) fields with doc values skipper (Pan Guixin)
 
+* GITHUB#15560: Avoid unnecessary getGraph() call. (Shubham Chaudhary)
+
 Bug Fixes
 ---------------------
 * GITHUB#14161: PointInSetQuery's constructor now throws IllegalArgumentException
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 d7d95d45486..e55e015652c 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
@@ -335,15 +335,15 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
     // Take into account if quantized? E.g. some scorer cost?
     // Use approximate cardinality as this is good enough, but ensure we don't exceed the graph
     // size as that is illogical
-    HnswGraph graph = getGraph(fieldEntry);
-    int filteredDocCount = Math.min(acceptDocs.cost(), graph.size());
+    int graphSize = (fieldEntry.vectorIndexLength() == 0) ? 0 : fieldEntry.size();
+    int filteredDocCount = Math.min(acceptDocs.cost(), graphSize);
     Bits accepted = acceptDocs.bits();
     final Bits acceptedOrds = scorer.getAcceptOrds(accepted);
     int numVectors = scorer.maxOrd();
     boolean doHnsw = knnCollector.k() < numVectors;
     // The approximate number of vectors that would be visited if we did not filter
-    int unfilteredVisit = HnswGraphSearcher.expectedVisitedNodes(knnCollector.k(), graph.size());
-    if (unfilteredVisit >= filteredDocCount || graph.size() == 0) {
+    int unfilteredVisit = HnswGraphSearcher.expectedVisitedNodes(knnCollector.k(), graphSize);
+    if (unfilteredVisit >= filteredDocCount || graphSize == 0) {
       doHnsw = false;
     }
     if (doHnsw) {