Repository navigation
Description
Describe the bug
During index_gt::update, the success callback (which commits the new vector to storage) is invoked after connection reconstruction. This causes refine_ in form_reverse_links_ to read the old vector via citerator_at(updated_slot) when computing inter-neighbor distances(inter_result_dist), leading to incorrect neighbor pruning and degraded graph quality.
Cause
USearch/include/usearch/index_dense.hpp
Lines 2029 to 2050 in 40d127f
| auto on_success = [&](member_ref_t member) { | |
| if (config_.enable_key_lookups) { | |
| unique_lock_t slot_lock(slot_lookup_mutex_); | |
| slot_lookup_.try_emplace(key_and_slot_t{key, static_cast<compressed_slot_t>(member.slot)}); | |
| } | |
| if (copy_vector) { | |
| if (!reuse_node) | |
| vectors_lookup_[member.slot] = vectors_tape_allocator_.allocate(metric_.bytes_per_vector()); | |
| std::memcpy(vectors_lookup_[member.slot], vector_data, metric_.bytes_per_vector()); | |
| } else | |
| vectors_lookup_[member.slot] = (byte_t*)vector_data; | |
| }; | |
| index_update_config_t update_config; | |
| update_config.thread = lock.thread_id; | |
| update_config.expansion = config_.expansion_add; | |
| metric_proxy_t metric{*this}; | |
| return reuse_node // | |
| ? typed_->update(typed_->iterator_at(free_slot), key, vector_data, metric, update_config, on_success) | |
| : typed_->add(key, vector_data, metric, update_config, on_success); | |
| } |
USearch/include/usearch/index.hpp
Lines 3003 to 3004 in 40d127f
| callback(at(updated_slot)); | |
| return result; |
When index_dense_gt calls index_gt::update, it passes an on_success callback that writes the new vector into vectors_lookup_:
auto on_success = [&](member_ref_t member) {
// ...
std::memcpy(vectors_lookup_[member.slot], vector_data, metric_.bytes_per_vector());
};Inside index_gt::update, the execution order is:
search_for_one_: usesvaluedirectly (correct)- For each level:
search_to_update_: usesvaluedirectly (correct)form_links_to_closest_->refine_:updated_slotexcluded from candidates (correct)form_reverse_links_->refine_: readsciterator_at(updated_slot)= old vector (incorrect)
callback(at(updated_slot))— vector committed to storage (too late)
In step 2, form_reverse_links_ inserts the updated slot into top_for_refine with the correct distance (computed directly from value). However, refine_ subsequently computes inter-neighbor distances between candidates using citerator_at, which accessesvectors_lookup_[updated_slot](still holding the old vector at that point).
// form_reverse_links_(): distance to close_slot uses value directly (correct)
top_for_refine.insert_reserved({context.measure(value, citerator_at(close_slot), metric), new_slot});
// refine_(): inter-neighbor distance uses citerator_at() (stale for updated_slot)
distance_t inter_result_dist = context.measure(
citerator_at(candidate.slot), // stale if candidate.slot == updated_slot
citerator_at(submitted.slot), // stale if submitted.slot == updated_slot
metric);The HNSW diverse neighbor selection heuristic (refine_) uses inter-neighbor distances to prune candidates that are too close to already-selected neighbors. Stale distances cause incorrect pruning decisions:
- A candidate that is far from the updated node's new position may be incorrectly pruned (stale vector makes it appear close)
- A candidate that is close to the new position may be incorrectly kept
This degrades neighbor lists on nodes adjacent to the updated node, reducing search recall in update-heavy workloads.
For reference, add() does not have this issue. it calls callback before the connection loop:
USearch/include/usearch/index.hpp
Lines 2836 to 2837 in 40d127f
| compressed_slot_t new_slot = result.slot = static_cast<compressed_slot_t>(old_size); | |
| callback(at(result.slot)); |
Fix
Add optional override_slot / override_value parameters to refine_. When the updated slot is involved in an inter-neighbor distance computation, use the override value directly instead of reading through citerator_at. form_reverse_links_ already has both new_slot and value, so it forwards them to refine_.
Steps to reproduce
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>
// ---------------------------------------------------------------------------
// 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]; }
private:
std::size_t dimensions_ = 0;
std::vector<std::vector<element_t>> vectors_;
};
// ---------------------------------------------------------------------------
// TEST: refine_() during update() must use the override value for the updated
// slot, not the stale one read via citerator_at().
//
// Detection strategy:
// A metric wrapper flags any (citerator, citerator) distance call where
// either operand is the currently-updating slot. With the fix, refine_()
// bypasses citerator_at() for the override slot and dispatches through the
// (value, citerator) path instead — so the flag is never raised.
// Without the fix, refine_() calls citerator_at() for all candidates
// including the updating slot, triggering the (citerator, citerator) path.
//
// The store is updated only in the callback (mimicking index_dense_gt's
// on_success), so citerator_at(updating_slot) would read the OLD vector.
// ---------------------------------------------------------------------------
TEST(index_gt_basic, update_refine_uses_fresh_vector) {
constexpr std::size_t n = 256, d = 16, m = 4;
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 inner_metric_t = cosine_metric_gt<float, vector_key_t, compressed_slot_t>;
using member_cref_t = typename index_t::member_cref_t;
using member_citerator_t = typename index_t::member_citerator_t;
using member_ref_t = typename index_t::member_ref_t;
mock_vector_store_gt<float> store(n, d, 2025);
inner_metric_t inner{&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 = 64;
for (std::size_t i = 0; i < n; ++i) {
auto res = index.add(i, store.row(i), inner, update_config);
ASSERT_TRUE(static_cast<bool>(res));
}
// Metric wrapper: flags (citerator, citerator) calls involving the updating slot.
// Only the refine_() code path in form_reverse_links_() can trigger this for the
// updating slot; all other distance calls use the (value, citerator) overload.
struct detecting_metric_t {
inner_metric_t* inner_metric;
compressed_slot_t updating_slot;
mutable bool stale_read = false;
float operator()(member_cref_t const& a, member_cref_t const& b) const { return (*inner_metric)(a, b); }
float operator()(float const* q, member_cref_t const& m) const { return (*inner_metric)(q, m); }
float operator()(float const* q, member_citerator_t const& it) const { return (*inner_metric)(q, it); }
float operator()(member_citerator_t const& a, member_citerator_t const& b) const {
if (get_slot(*a) == updating_slot || get_slot(*b) == updating_slot)
stale_read = true;
return (*inner_metric)(a, b);
}
};
// Update half the nodes. Store is updated ONLY in callback.
constexpr std::size_t n_update = n / 2;
std::size_t stale_read_count = 0;
for (std::size_t i = 0; i < n_update; ++i) {
compressed_slot_t slot = i;
float const* new_vec = store.row(slot);
detecting_metric_t det_metric{&inner, slot, false};
auto on_success = [&store, slot, new_vec](member_ref_t) { store.set_row(slot, new_vec); };
auto res = index.update(index.iterator_at(slot), i, new_vec, det_metric, update_config, on_success);
ASSERT_TRUE(static_cast<bool>(res));
if (det_metric.stale_read)
stale_read_count++;
}
EXPECT_EQ(stale_read_count, 0u) << stale_read_count << " out of " << n_update
<< " updates read the stale vector via citerator_at() in refine_()";
}Expected behavior
When forming reverse links, the updated slot must be accessed with its valid (newly assigned) vector.
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 Apr 7, 2026
Thank you for approving this fix. :)