From 2e1e509c8c291c532993352041367e25676f4d5f Mon Sep 17 00:00:00 2001 From: pranav2007 Date: Wed, 28 Oct 2026 06:35:50 +0530 Subject: [PATCH] use merge_and_prune() only when needed --- crates/index/src/hnsw/search.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/index/src/hnsw/search.rs b/crates/index/src/hnsw/search.rs index 8be71db..a93b069 100644 --- a/crates/index/src/hnsw/search.rs +++ b/crates/index/src/hnsw/search.rs @@ -197,6 +197,20 @@ impl HnswIndex { if n == p { continue; } + + self.ensure_level(n, level); + let node = self.index.nodes.get_mut(&n).unwrap(); + let list = &mut node.neighbors[level]; + + // The list has room: just add p, no need to compute distances or sort + if list.len() < m { + if !list.contains(&p) { + list.push(p); + } + continue; + } + + // The list is full: keep only the closest m self.merge_and_prune(n, level, &[p], m)?; } Ok(())