Skip to content

Fix: remove() + add() slot reuse no longer strands the removed node's neighbors - #821

Closed
cpegeric wants to merge 1 commit into
unum-cloud:main-devfrom
cpegeric:fix-remove-add-reuse
Closed

cpegeric wants to merge 1 commit into
unum-cloud:main-devfrom
cpegeric:fix-remove-add-reuse

Conversation

@cpegeric

Copy link
Copy Markdown
Contributor

Problem

remove() followed by add() can leave live keys permanently unreachable by search().

index_dense_gt::remove() only marks a node as removed and queues its slot in free_keys_. The next add() pops that slot and calls index_gt::update(), which rewrites the node in place at the new vector's position. update() clears the node's neighbor lists and links it at the new position, but nothing repairs the graph around the node's old position. There is a // TODO: Go through existing neighbors removing reverse links at that spot. Any part of the graph that was reachable only through the removed node is cut off. Neither saving and reloading nor further adds bring it back; only a full rebuild does.

Minimal repro (L2, 3-D, default config, single thread):

for (i = 1..1000) index.add(i, {i, 1, 2});   // points on a line
index.remove(100);
index.add(2001, {777.2, 1, 2});              // reuses slot of key 100
index.search({777.2, 1, 2}, 1000);           // returns 901 keys: 1..99 are unreachable

Points on a line make each node link only to its two neighbors on the line, so the removed node is the only bridge and a whole range is lost. With remove(500) the search returns 501 keys and 1..499 are lost. Re-adding at the same position loses nothing. On ordinary data the effect is partial. With 2000 keys, after 20 rounds of removing 10% and adding new keys, the number of live keys that an exhaustive self-search cannot find is:

config before after
M=8, uniform 16-D 13 1
M=8, clustered 16-D 32 1
M=16, uniform 16-D 0 0
M=16, clustered 16-D 19 0

Fix

  • index_gt::update() first calls a new relink_neighbors_of_(), which visits every level of the node being reused. For each of the node's old neighbors, it drops that neighbor's link to the reused slot and re-picks the neighbor's links with the existing refine_ heuristic. The candidates are the neighbor's own remaining links plus the removed node's other neighbors.
    • It locks one node at a time, as form_reverse_links_ does.
    • The snapshot of the old neighbors goes into a reused per-thread context_t buffer.
    • All allocation happens before the first change, so an allocation failure is returned via result.failed() with the graph unchanged.
  • index_dense_gt::add_() serializes the adds that reuse a slot with a new mutex. update() is not safe against concurrent updates of neighboring nodes, and the relink makes that window wider. Fresh adds and searches stay concurrent.

Slots are still reused, so memory use doesn't grow.

Cost

The relink recomputes each old neighbor's links, so a remove+add costs more than before. This is a single-threaded micro-benchmark with 128-D vectors, 20k keys and 2000 remove+add operations:

M remove+add before after plain add
16 265 µs 420 µs 183 µs
64 398 µs 1973 µs 333 µs

A cheaper variant would replace only each neighbor's link to the reused slot with the removed node's closest other neighbor. That is O(M²) per reuse instead of O(M³), but it repairs less. I'm happy to switch to it if you prefer.

Tests

New tests in cpp/test.cpp:

  • test_remove_add_line_reachability, for slot32_t and uint40_t: the repro above. It fails on current main-dev/main (v2.26.5) and passes with this change.
  • test_remove_add_churn_reachability: repeated remove/add rounds, single- and multi-threaded. It asserts every live key finds itself in an exhaustive search.

The full test_cpp suite passes on macOS arm64 (Apple clang) in Release, and also with NDEBUG undefined so the internal assertions are active. The default Debug configuration enables ASan, whose runtime crashes at startup on this macOS, even on an empty program, so it could not be run locally. git clang-format reports no changes.

The fix is also in use in MatrixOne's vendored usearch. There the bug showed up as an HNSW index returning 901 of 1000 rows after a single DELETE + INSERT (matrixorigin/matrixone#30660).

… neighbors

remove() only tombstones a node and queues its slot; the next add() pops that slot
and update() rewrites the node in place at the new vector's position. update()
cleared the node's neighbor lists without repairing anything, so the part of the
graph that was reachable only through the removed node became unreachable. On
points on a line (each node linked only to its two line neighbors), removing key k
and adding a far vector left every key below k unreachable: 1000 keys, remove 100,
add one far key -> search returns 901.

Fix:
  - index_gt::update() first relinks the removed node's old neighbors on every
    level: each neighbor drops its link to the reused slot and re-selects its
    neighbors (heuristic refine) from its own list plus the removed node's other
    neighbors. Nodes are locked one at a time, as in form_reverse_links_. The
    snapshot uses a reused per-thread context buffer and reports OOM via
    result.failed().
  - index_dense_gt::add_() serializes slot-reusing adds, since update() is not
    safe against concurrent updates of neighboring nodes. Fresh adds and searches
    stay concurrent.

Tests (cpp/test.cpp): remove + far add on a line for slot32_t/uint40_t, and
remove/add churn rounds (single- and multi-threaded) asserting every live key
finds itself in an exhaustive search. The line test fails without the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
swasik added a commit to swasik/USearch that referenced this pull request Nov 4, 2026
`remove` followed by `add` reuses the removed node's slot: `update`
rewrites the node at its new position. Whatever was reachable only
through its old position is cut off. On points on a line, a search from
the far side then misses the whole other half.

Before the node moves, search around its old position without ever
entering it, nor any node that a concurrent `update` is moving. Each old
base-level neighbor that the search doesn't reach gets a link from the
nodes it did reach, and a link back, as the moving node gave both ways.
The move then drops only the links this bridge accounted for, and keeps
those that concurrent insertions added meanwhile. Of two overlapping
updates, at least one sees the other's node as moving.

The bridge runs only if a neighbor would be left with at most one
incoming link, the same threshold as for pruned links, so a move costs
about as much as before.

unum-cloud#801 by Ofek Lev and unum-cloud#821 by cpegeric fix the same cut. Their tests
are here: unum-cloud#821's points on a line and churn rounds, and unum-cloud#801's two
bridge tests.
swasik added a commit to swasik/USearch that referenced this pull request Nov 4, 2026
`remove` followed by `add` reuses the removed node's slot: `update`
rewrites the node at its new position. Whatever was reachable only
through its old position is cut off. On points on a line, a search from
the far side then misses the whole other half.

Before the node moves, search around its old position without ever
entering it, nor any node that a concurrent `update` is moving. Each old
base-level neighbor that the search doesn't reach gets a link from the
nodes it did reach, and a link back, as the moving node gave both ways.
The move then drops only the links this bridge accounted for, and keeps
those that concurrent insertions added meanwhile. Of two overlapping
updates, at least one sees the other's node as moving.

The bridge runs only if a neighbor would be left with at most one
incoming link, the same threshold as for pruned links, so a move costs
about as much as before.

unum-cloud#801 by Ofek Lev and unum-cloud#821 by cpegeric fix the same cut. Their tests
are here: unum-cloud#821's points on a line and churn rounds, and unum-cloud#801's two
bridge tests.
@swasik

swasik commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@cpegeric, #805 now repairs slot reuse in update as well, together with pruning, the add race (#735) and equal vectors (#815). Your points-on-a-line reproducer and churn rounds are in its tests, credited to you, and it passes all of them. That includes the multi-threaded churn, with no mutex around the adds that reuse a slot.

The cost table in its description uses your benchmark. A remove followed by an add stays at main-dev's level: 281 µs at M=16 and 551 µs at M=64, against 274 and 553 on main-dev, and 526 and 3,261 with this PR. If #805 is merged, this PR would no longer be needed; that is the maintainers' call.

@ashvardanian

Copy link
Copy Markdown
Member

@cpegeric, thanks for this one too! 3.0 has no remove yet, so this path is gone for now. I'd ask for a little more patience with us: we're about to settle and publish how v3 will handle removals and whether they're on the agenda.

@ashvardanian

Copy link
Copy Markdown
Member

@cpegeric, as promised, here's how v3 will handle removals: #846 proposes an exclusive call that removes a batch of keys, renumbers the survivors and re-links the graph around them, so slots are never reused in place. I'm closing this in its favor, and I'd value your eyes on the design. cc @swasik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants