Skip to content

Fix: Preserve graph connectivity when reusing deleted slots - #801

Closed
ofek wants to merge 1 commit into
unum-cloud:main-devfrom
ofek:fix/deleted-slot-graph-connectivity
Closed

ofek wants to merge 1 commit into
unum-cloud:main-devfrom
ofek:fix/deleted-slot-graph-connectivity

Conversation

@ofek

@ofek ofek commented Oct 15, 2026 •

Copy link
Copy Markdown

Intent

Reusing a deleted slot can make a surviving vector unreachable through graph search. Rebuilding the reused node's outgoing links can remove the only incoming path to a live node. Its key and vector remain present, and exact search still finds it.

A small reproduction uses squared L2 distance with connectivity=2, connectivity_base=4, and expansion_search=1000:

  1. Add (10, 0, 0), (0, 0, 0), and (-1, 0, 0).
  2. Remove (0, 0, 0) and add (3000, 0, 0), reusing the deleted slot.
  3. Query at (0, 0, 0). Before the fix, graph search returns (10, 0, 0) while exact search returns the surviving nearest vector, (-1, 0, 0).

Changes

The fix preserves graph reachability in the reproduced slot-reuse and direct-update cases. It reconnects the updated node's former neighbors without discarding their links to other surviving vectors. Reserving room for reverse links prevents later pruning from undoing that repair.

Existing callers and saved indexes remain compatible: the public API, serialized format, and timing of the success callback are unchanged. Updates perform additional local graph work within the existing degree limits and reuse temporary storage across calls.

Validation

The regression tests verify that every surviving vector in each fixture remains reachable, because key lookup alone cannot detect this failure. They also exercise the reverse-link step with one and two free slots and assert that existing edges survive, guarding against a second pruning pass undoing the repair. Both 32-bit and packed 40-bit slots are covered.

The full C++ suites used Release builds with NumKong, OpenMP, and JeMalloc disabled. The focused commands use a local harness generated from the committed regression tests.

ctest --test-dir build/deleted-slot-release --output-on-failure --timeout 240

Windows x64, MSVC, C++17. Full C++ suite:

Internal ctest changing into directory: C:/Users/ofek/Desktop/code/USearch/build/deleted-slot-release
Test project C:/Users/ofek/Desktop/code/USearch/build/deleted-slot-release
    Start 1: test_cpp
1/1 Test #1: test_cpp .........................   Passed   49.48 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =  49.51 sec
ctest --test-dir "$native_dir/build" --output-on-failure --timeout 240

Linux under WSL, GCC, C++17. The build directory was configured with USEARCH_BUILD_TEST_CPP=ON, USEARCH_BUILD_BENCH_CPP=OFF, and CMAKE_BUILD_TYPE=Release. Full C++ suite:

Test project /tmp/usearch-maintenance-suite.azGAaH/build
    Start 1: test_cpp
1/1 Test #1: test_cpp .........................   Passed   32.67 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =  32.67 sec
./focused.exe

Windows x64, Clang, C++17, -O1. Focused regression cases:

All reverse-link reservation, bridge reuse, and direct-update cases passed
ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 ./focused

Linux under WSL, GCC, C++11, -O1 -g1 -pthread -fsanitize=address,undefined -fno-omit-frame-pointer. Focused regression cases exited with status 0 and no sanitizer diagnostics:

All reverse-link reservation, bridge reuse, and direct-update cases passed

Disabling reverse-link capacity reservation makes each new fixture fail independently, confirming that both tests detect the intended regression.

./focused-mutated 1

Linux GCC, C++11. One free slot before repair. Expected failure, exit status 1:

C++ unit test failed at focused-tests.cpp:213
C++ unit test failed at focused-tests.cpp:213
./focused-mutated 2

Linux GCC, C++11. Two free slots before repair. Expected failure, exit status 1:

C++ unit test failed at focused-tests.cpp:213
C++ unit test failed at focused-tests.cpp:213
clang-format --dry-run --Werror -lines=3394:3415 -lines=4330:4386 include/usearch/index.hpp && clang-format --dry-run --Werror -lines=1248:1252 -lines=1277:1383 cpp/test.cpp && git diff --check

Changed-line formatting and whitespace checks exited with status 0:

Changed-line formatting and whitespace checks passed.

@ofek
ofek force-pushed the fix/deleted-slot-graph-connectivity branch from 8b766b4 to bd27024 Compare October 15, 2026 08:13
@ofek
ofek marked this pull request as ready for review October 15, 2026 19:29
@ashvardanian ashvardanian added the v3 Breaking changes planned for v3 label Oct 18, 2026
@ashvardanian

Copy link
Copy Markdown
Member

Thank you, @ofek! The direction of this work is very important, but even on toy-scale graphs of thousands of nodes, removing half, and reinserting it is penalized by 2-6x on my old Apple M2. The v3, coming later this month, changes the core algorithm, but it can still benefit from similar efforts. Let me think of the best ways to include this work there 🤗

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

@ofek, #805 now repairs slot reuse in update as well, together with pruning, the add race (#735) and equal vectors (#815). Your test_replacing_bridge and test_replacing_saturated_bridge are in it unchanged and credited to you. All 12 of your variants pass.

On #821's benchmark, a remove followed by an add costs 281 µs at M=16 and 551 µs at M=64 with #805, against 274 and 553 on main-dev, and 353 and 2,365 with this PR. #805's description compares all four open PRs on each other's reproducers. If #805 is merged, this PR would no longer be needed; that is the maintainers' call.

@ashvardanian

Copy link
Copy Markdown
Member

@ofek, as promised, here's where v3 lands: #846 proposes removal as an exclusive call that renumbers the survivors instead of reusing deleted slots, so the bridges your tests guard are re-linked as part of the call. Your test_replacing_bridge cases would make good checks for it, so I'm closing this in favor of #846. cc @swasik

@ofek

ofek commented Nov 15, 2026

Copy link
Copy Markdown
Author

That's awesome, thanks!

@ofek
ofek deleted the fix/deleted-slot-graph-connectivity branch November 15, 2026 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v3 Breaking changes planned for v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants