Repository navigation
Description
Describe the bug
We are using index_gt with a custom vector store, implementing the similar pattern such as index_dense_gt. When a node is soft-deleted(like index_dense_gt) and then re-inserted via update(), form_reverse_links_() can produce duplicate entries(slots) in a neighbor list through its refinement(refine_) path(the code path taken when close_header.size() >= connectivity_max).
The fast path (close_header.size() < connectivity_max) already contains a duplicate check and is not affected. (ref. #698)
Cause
When the neighbor list of close_slot is already full, form_reverse_links_() enters the refinement path:
top_for_refine.clear();
top_for_refine.insert_reserved({distance(value, close_slot), new_slot}); // (A)
for (compressed_slot_t successor_slot : close_header)
top_for_refine.insert_reserved({distance(close_slot, successor_slot), successor_slot}); // (B)
close_header.clear();
candidates_view_t top_view = refine_(..., top_for_refine, ...);
for (std::size_t idx = 0; idx != top_view.size(); idx++)
close_header.push_back(top_view[idx].slot);If new_slot is already present in close_header from a prior insertion(but deleted), it gets added to top_for_refine twice: once explicitly at (A), and once as a successor_slot at (B). refine_() cannot deduplicate its input, so the final close_header can contain the same slot more than once. The same bug applies to index_dense_gt as well.
Fix
Skip the entire reverse-link iteration for close_slot when new_slot is already present in its neighbor list. This guard should be placed before both the fast path and the refinement path:
// Skip to prevent duplicate entries in the neighbor list.
if (std::find_if(close_header.begin(), close_header.end(),
[new_slot](compressed_slot_t slot) { return slot == new_slot; }) != close_header.end()) {
continue;
}USearch/include/usearch/index.hpp
Lines 3903 to 3909 in 6995246
| if (close_header.size() < connectivity_max) { | |
| if (std::find_if(close_header.begin(), close_header.end(), | |
| [new_slot](compressed_slot_t slot) { return slot == new_slot; }) == close_header.end()) { | |
| close_header.push_back(new_slot); | |
| } | |
| continue; | |
| } |
This is an O(M) linear scan per neighbor, but connectivity_max is typically small (16–64), so the overhead is negligible compared to the distance computations already performed in the same function.
Steps to reproduce
When many nodes share identical vectors, deleting a node and re-inserting it via update() can cause duplicate slots in neighbor lists because form_reverse_links_() does not check for existing entries before appending reverse links.
A duplicate slot typically gets pruned during the heuristic refinement step, since its distance to the pivot is 0 and it competes with its identical twin. However, when multiple candidate neighbors share the same vector, several distance-0 slots coexist in top_for_refine. Because each refinement iteration eliminates only one candidate (the M+1-th), a duplicate slot isn't guaranteed to be pruned. Eventually, the surviving duplicates end up in the final neighbor list.
The following is a reproducible test case. ('Details')
Details
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <random>
#include <unordered_map>
#include <vector>
#include <gtest/gtest.h>
#include <usearch/index.hpp>
#include <usearch/index_plugins.hpp>
using namespace unum::usearch;
// ---------------------------------------------------------------------------
// Mock vector store
// ---------------------------------------------------------------------------
template <typename vector_element_at = float> class mock_vector_store_gt {
public:
using element_t = vector_element_at;
mock_vector_store_gt() = default;
mock_vector_store_gt(std::size_t count, std::size_t dims, unsigned seed = 2025) : dimensions_(dims) {
std::default_random_engine gen(seed);
std::uniform_real_distribution<float> dist{0.f, 1.f};
vectors_.resize(count);
for (auto& v : vectors_) {
v.resize(dims);
std::generate(v.begin(), v.end(), [&] { return static_cast<element_t>(dist(gen)); });
}
}
std::size_t size() const noexcept { return vectors_.size(); }
std::size_t dimensions() const noexcept { return dimensions_; }
element_t const* row(std::size_t slot) const noexcept { return vectors_[slot].data(); }
std::vector<element_t> const& vector(std::size_t slot) const noexcept { return vectors_[slot]; }
/// @brief Overwrite the vector at @p slot with a copy of @p src (must have `dimensions_` elements).
void set_row(std::size_t slot, element_t const* src) { vectors_[slot].assign(src, src + dimensions_); }
private:
std::size_t dimensions_ = 0;
std::vector<std::vector<element_t>> vectors_;
};
// ---------------------------------------------------------------------------
// TEST: Duplicate neighbors after soft-delete + update() with identical vectors.
//
// Pairwise-identical vectors (distance = 0 within each pair) are used in
// both add() and update() phases. form_reverse_links_() can then insert
// the same slot into a neighbor list more than once.
// ---------------------------------------------------------------------------
TEST(index_gt_basic, duplicate_neighbors_with_identical_vectors_on_reuse) {
constexpr std::size_t n = 1024, d = 16, m = 8;
constexpr std::size_t n_delete = 512;
static_assert(n_delete % 2 == 0, "n_delete must be even for pair-wise reuse");
using vector_key_t = std::size_t;
using compressed_slot_t = std::size_t;
using index_t = index_gt<float, vector_key_t, compressed_slot_t>;
using metric_t = cosine_metric_gt<float, vector_key_t, compressed_slot_t>;
constexpr vector_key_t sentinel_key = std::numeric_limits<vector_key_t>::max();
mock_vector_store_gt<float> store(n, d, 42);
metric_t metric{&store, d};
index_config_t config(m);
aligned_index_gt<index_t> aligned(config);
auto& index = *aligned.index;
index.reserve(n);
index_update_config_t update_config;
update_config.expansion = 32;
// Callback that copies the even-slot vector into its odd-slot partner,
// producing pairwise-identical vectors: (0,1), (2,3), …
auto make_pair = [&store](auto const& member) noexcept {
std::size_t s = member.slot;
if (s % 2 == 1)
store.set_row(s, store.row(s - 1));
};
// --- 1. add() n vectors with pairwise-identical data --------------------
for (std::size_t i = 0; i < n; ++i) {
auto res = index.add(i, store.row(i), metric, update_config, make_pair);
ASSERT_TRUE(static_cast<bool>(res)) << "add failed at i=" << i;
}
ASSERT_EQ(index.size(), n);
// --- 2. Soft-delete n_delete evenly-spaced slots ------------------------
ring_gt<compressed_slot_t> free_slots;
ASSERT_TRUE(free_slots.reserve(n_delete));
for (std::size_t i = 0; i < n_delete; ++i) {
compressed_slot_t slot = static_cast<compressed_slot_t>(i * (n / n_delete));
index.at(slot).key = sentinel_key;
free_slots.push(slot);
}
// --- 3. update() reusing deleted slots, again pairwise-identical --------
// Pop pairs from the free list. For each pair (slot_a, slot_b):
// callback_a writes a fresh vector into slot_a,
// callback_b copies slot_a's vector into slot_b (making them identical).
vector_key_t next_key = n;
while (!free_slots.empty()) {
compressed_slot_t slot_a, slot_b;
ASSERT_TRUE(free_slots.try_pop(slot_a));
ASSERT_TRUE(free_slots.try_pop(slot_b));
const float* pair_vector = store.row(n / 2 + next_key % (n / 2));
auto callback_a = [&store, pair_vector](auto const& mb) noexcept { store.set_row(mb.slot, pair_vector); };
auto callback_b = [&store, slot_a](auto const& mb) noexcept { store.set_row(mb.slot, store.row(slot_a)); };
auto res_a =
index.update(index.iterator_at(slot_a), next_key++, store.row(slot_a), metric, update_config, callback_a);
ASSERT_TRUE(static_cast<bool>(res_a)) << "update failed at slot=" << slot_a;
auto res_b =
index.update(index.iterator_at(slot_b), next_key++, store.row(slot_b), metric, update_config, callback_b);
ASSERT_TRUE(static_cast<bool>(res_b)) << "update failed at slot=" << slot_b;
}
// --- 4. Verify: no duplicate slots in any neighbor list -----------------
std::size_t total_dups = 0;
auto dup_counter = [&](auto begin, auto end) noexcept {
std::size_t seen[m * 2 + 4];
std::size_t count = 0;
for (auto it = begin; it != end; ++it) {
std::size_t slot = get_slot(it);
for (std::size_t j = 0; j < count; ++j)
if (seen[j] == slot) {
++total_dups;
break;
}
if (count < m * 2 + 4)
seen[count++] = slot;
}
};
index_search_config_t scan_config;
scan_config.expansion = n;
for (std::size_t i = 0; i < n; ++i)
index.search(store.row(i), 1, metric, scan_config, dummy_predicate_t{}, dup_counter);
EXPECT_EQ(total_dups, 0u) << "found " << total_dups
<< " duplicate neighbor entries after reusing slots with identical vectors";
}Expected behavior
The same slot must not appear more than once in a neighbor list."
USearch version
v2.24.0
Operating System
Red Hat Enterprise Linux release 8.6
Hardware architecture
x86
Which interface are you using?
C++ implementation
Contact Details
Are you open to being tagged as a contributor?
- I am open to being mentioned in the project
.githistory as a contributor
Is there an existing issue for this?
- I have searched the existing issues
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
yoonseok-kim commentedon Mar 16, 2026
Normal
Abnormal
If s2 and s4 have the same vector value, …
yoonseok-kim commentedon Apr 7, 2026
Thank you for approving this fix. 😄