Skip to content

Apache Lucene open-source search software

Use TernaryLongHeap in NeighborQueue for better HNSW performance (#15163)

Ramakrishna Chilaka1a448f1cfd60
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1c1b6450494..342c9998d58 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -129,6 +129,8 @@ Optimizations
---------------------
* GITHUB#15140: Optimize TopScoreDocCollector with TernaryLongHeap for improved performance over Binary-LongHeap. (Ramakrishna Chilaka)
+* GITHUB#15163: Use TernaryLongHeap in NeighborQueue for faster vector search performance. (Ramakrishna Chilaka)
+
* GITHUB#14998: Speed up flushing of live docs. (Adrien Grand)
* GITHUB#15165: Optimize PForUtil.encode() with histogram-based bit selection. (Ramakrishna Chilaka)
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborQueue.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborQueue.java
index 6a473704a85..86f65ee065e 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborQueue.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborQueue.java
@@ -17,13 +17,13 @@
package org.apache.lucene.util.hnsw;
-import org.apache.lucene.util.LongHeap;
import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.TernaryLongHeap;
/**
- * NeighborQueue uses a {@link LongHeap} to store lists of arcs in an HNSW graph, represented as a
- * neighbor node id with an associated score packed together as a sortable long, which is sorted
- * primarily by score. The queue provides both fixed-size and unbounded operations via {@link
+ * NeighborQueue uses a {@link TernaryLongHeap} to store lists of arcs in an HNSW graph, represented
+ * as a neighbor node id with an associated score packed together as a sortable long, which is
+ * sorted primarily by score. The queue provides both fixed-size and unbounded operations via {@link
* #insertWithOverflow(int, float)} and {@link #add(int, float)}, and provides MIN and MAX heap
* subclasses.
*/
@@ -48,7 +48,7 @@ public class NeighborQueue {
abstract long apply(long v);
}
- private final LongHeap heap;
+ private final TernaryLongHeap heap;
private final Order order;
// Used to track the number of neighbors visited during a single graph traversal
@@ -57,7 +57,7 @@ public class NeighborQueue {
private boolean incomplete;
public NeighborQueue(int initialSize, boolean maxHeap) {
- this.heap = new LongHeap(initialSize);
+ this.heap = new TernaryLongHeap(initialSize);
this.order = maxHeap ? Order.MAX_HEAP : Order.MIN_HEAP;
}