Skip to content

Fix: Keep nodes reachable through pruning, concurrent add and slot reuse - #805

Closed
swasik wants to merge 3 commits into
unum-cloud:main-devfrom
swasik:fix-reverse-link-orphans
Closed

swasik wants to merge 3 commits into
unum-cloud:main-devfrom
swasik:fix-reverse-link-orphans

Conversation

@swasik

@swasik swasik commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #735. Refs #815.

A node with no incoming base-level link can't be reached by any search, not even a search for its own vector. Five open threads report ways a node ends up like that, and four PRs fix one way each:

This version replaces the earlier rescue with one rule that covers all of them, and keeps the cost of add and of remove + add at main-dev's level.

The rule

A base-level node left with at most one incoming link gets a link from a node that the same operation has just reached from the entry point. The link never comes from the node's own neighborhood: that neighborhood can be cut off too, which is how islands form. Each place that drops links gets the rule in its own commit:

  1. Fix: Keep every node reachable when links are pruned. Refining a full list drops the least diverse candidates.
    • refine_ now records, for each dropped member, the kept member that it found closer to it.
    • If the dropped member is left with at most one incoming link, it gets a link from that kept member, which is the path the pruning assumed. Failing that, the link comes from any member the list kept.
    • Failing that, it comes from the candidates the insertion's own search reached from the entry point, kept past the refine. A new node that no neighbor kept gets the same.
    • No link is ever evicted, and the links are formed once no other lock is held.
    • The incoming-link counts are the ones from the earlier version (4 bytes per member, relaxed atomics, recounted after copy, compact, isolate and the first reserve after load).
    • The threshold of one rather than zero matters: two nodes linking only to each other each have one incoming link.
  2. Fix: Keep reverse links added to a node still being inserted. Unchanged from the earlier version. add no longer clears a new node's lists, and form_links_to_closest_ appends after the reverse links that concurrent insertions already added.
  3. Fix: Keep nodes reachable when update moves one. remove followed by add reuses the slot, and update moves the node. Before the move:
    • A search around the old position, which never enters the moving node or any node another update is moving, finds the old neighbors that would be cut off.
    • Each of them gets a link from a node that search reached, and a link back.
    • The move then drops only the links this bridge accounted for, so links that concurrent insertions added meanwhile stay.
    • Each thread publishes its moving slot in an atomic, so of two overlapping updates at least one sees the other.
    • The bridge runs only if an old neighbor would be left with at most one incoming link. This resolves the TODO in update.

The open PRs on the same reproducers

Every cell counts the live keys that an exhaustive search (count = size) misses, so it also finds islands that a key's own search could land in. The PR columns are each PR's head rebased onto main-dev 33d3ab6. "Earlier #805" is this PR's previous version, 0a8f4f0.

reproducer main-dev #772 #801 #821 earlier #805 this PR
#735: 10 threads × 100 vectors, 100 rounds 1,493 0 1,101 1,603 0 0
#801: its 12 variants, failing 2 2 0 0 2 0
#821: points on a line, remove(100) / remove(500) 99 / 499 99 / 499 0 / 0 0 / 0 99 / 499 0 / 0
clustered points, connectivity 4, 5,000 keys 90 90 92 90 12 0
#815 part 1: 1,248 keys with one vector, found 177 177 634* 177 418 1,248
#815 part 2: mixed, missed of 3,840 (mean of 10 seeds) 853 853 227* 853 887 100
#821's churn: 20 rounds of 10% remove + add, M=8 clustered 12 12 3 5 12 0

* #801's numbers for #815 come from its older base's level generator, not from its change.

All churn configurations pass: M=8 and M=16, uniform and clustered, with 1, 2, 4 and 16 threads.

Cost

This is #821's single-thread benchmark: 128-D L2, 20K keys, then 2,000 operations. The figures are medians of 3 interleaved runs, in µs per operation.

add, M=16 remove + add, M=16 add, M=64 remove + add, M=64
main-dev 282 274 591 553
#801 277 353 581 2,365
#821 280 526 578 3,261
this PR 295 281 595 551

An earlier attempt routed every dropped link instead of only those that leave a node with at most one incoming link. It densified the graph and cost 34% more per add.

Measurements

Cohere 1M, local x86-64. Setup: 768-d, cosine, f32, connectivity 16, expansion_add 128, 16 threads, 3 builds per arm.

main-dev earlier #805 this PR
unreachable nodes 5,928 (0.59%) 119 7 (0–13)
recall@10 at expansion_search 16 / 64 80.67% / 93.65% 80.67% / 93.72% 80.96% / 93.79%
recall@10 at expansion_search 512 / 4096 98.74% / 99.51% 98.79% / 99.63% 98.85% / 99.70%
self-search misses at 512 0.94% 0.61% 0.41%
build time 227 s 226 s 230 s

QPS is within run-to-run noise.

Cohere 10M inside ScyllaDB Vector Store, which embeds usearch. Setup:

  • Vector Store 1.15.0 on an AWS r8g.4xlarge (Graviton4, 16 vCPUs). Each arm is usearch 2.26.6 with that arm's index.hpp. 2.26.6's own index.hpp is the same as main-dev 33d3ab6's.
  • 768-d, cosine, f16, connectivity 16, expansion_add 128, 16 indexing threads, capacity reserved in steps of 1M.
  • Each build is a fresh index of all 10M vectors. 12 builds rotate through the arms, 4 per arm.
  • Recall@10 over Vector Store's HTTP API uses its default expansion_search 64. Graph recall searches the saved index against its own exact search.
mean (range) of 4 builds main-dev earlier #805 this PR
unreachable nodes 138,151 (1.38%) 5,516 99 (70–115)
nodes with no incoming base-level link 135,864 1,167 0
recall@10 over HTTP at expansion_search 64 90.28% (90.06–90.46) 89.82% (89.59–90.02) 90.01% (89.37–90.35)
graph recall@10 at expansion_search 512 / 2048 97.30% / 98.42% 97.26% / 98.44% 97.31% / 98.56%
index build 993 s (939–1,072) 1,033 s (931–1,129) 964 s (931–990)

Recall at 64 varies by up to 1 pp between builds of one arm, and this PR's builds overlap main-dev's. The earlier version was below main-dev in all four builds. At 2048 this PR is highest, as at 1M. Build time and QPS move with the shared host: each arm had one build in a slow window, at about 15K QPS instead of 20K.

Tests

Each new test fails on main-dev and passes from the commit that adds it, except the saturated-list test, which guards against evicting links and passes on main-dev too:

Checks run:

  • Each commit builds and passes test_cpp on its own (Release, GCC, x86-64).
  • The Debug build passes under ASan and UBSan.
  • ThreadSanitizer over Bug: Concurrent add() creates nodes unreachable by search() #735's pattern and multi-threaded churn reports only the race that main-dev already has: index_dense copies a reused slot's new vector while concurrent searches read it.

Credits

The analysis and reproducers in the other threads made this possible:

If this one is merged, #772, #801 and #821 would no longer be needed. That is your call.

Not covered

  • Islands of two or more links each. A group whose members each keep two or more incoming links from inside the group can still be cut off. That leaves 0–13 nodes out of 1M on Cohere 1M, and 70–115 out of 10M on Cohere 10M, in islands of at most 5.
  • Bug: Keys with identical vectors become unreachable by search(), also with one thread #815 part 2 still misses 100 of 3,840 keys, down from 853. Closing it needs a tie rule in refine_, as a separate change, tested on i8 and f16.
  • The entry point itself: if it is the node that update moves, the bridge is skipped.
  • The index_dense copy race above is separate and predates this PR.
  • The 10M run only adds. It validates the first two commits at scale. update is covered by the tests, the churn configurations and the cost benchmark.

CI

Rebased onto main-dev 33d3ab6. main-dev's own CI fails on several jobs, for reasons unrelated to this PR: Windows JS linking against NumKong, setup-java dropping adopt, the VS 2026 runner image, and a Docker job without its Dockerfile. #833 fixes those. The same jobs fail here, each at the same step. Test C++ passes in every job that gets to build: Ubuntu GCC 13 and Clang 20, macOS and Windows x86. Windows ARM stops earlier, because CMake finds no Visual Studio on the VS 2026 image.

@swasik
swasik force-pushed the fix-reverse-link-orphans branch from f83e25b to e484cbd Compare October 18, 2026 18:06
@swasik
swasik marked this pull request as draft October 18, 2026 20:03
@swasik
swasik force-pushed the fix-reverse-link-orphans branch from e484cbd to 634e4ac Compare October 19, 2026 15:38
@swasik
swasik marked this pull request as ready for review October 19, 2026 17:39
@swasik

swasik commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@ashvardanian, could you review this when you have a moment? It is ready on main-dev 5d3f821. It closes #735 and keeps every node reachable: on 1M Cohere, unreachable nodes go from 0.58% to 0.007%, with recall unchanged at the default expansion and higher at large ones. The two red macOS wheel jobs fail at import usearch, from the NumKong macOS wheel issue that also affects main-dev (ashvardanian/NumKong#411).

@ashvardanian

Copy link
Copy Markdown
Member

There is a very complex piece of work I am trying to finish on an expensive B300 cluster. Give me a few days to return to you on this one 🤗

@swasik

swasik commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main-dev 33d3ab6. The macOS wheel failures are gone with NumKong 7.8.6. The remaining red jobs come from main-dev, not this PR: setup-java@v6 dropped adopt (Ubuntu GCC 13, Android), windows-2025 now has Visual Studio 2026 only (Windows ARM's pinned generator, node-gyp 11 on Windows X86, then NumKong 7.8.5's dllimport default breaking the static JavaScript link), and "Build Docker" outlived its Dockerfile. #833 fixes them.

Refining a full neighbor list keeps the most diverse candidates and
drops the rest. A node can lose its last incoming base-level link that
way, and then no search reaches it, not even one for its own vector. On
1M Cohere embeddings with connectivity 16, 0.6% of the nodes end up like
that; hnswlib leaves a similar share. Two nodes can also be left linking
only to each other, and many keys with one vector tie on every distance,
so their lists fill with each other and most of them get no incoming
link at all (unum-cloud#815).

Count the incoming base-level links of every node (4 bytes per member,
relaxed atomics). When a refine leaves a dropped member with at most
one incoming link, which may come from a cut-off part, give it a route
once no other lock is held:

- the link from the kept member that the refine found closer to it,
  which the pruning assumed, or else from any member the list kept, so
  it stays reachable through the list that dropped it;
- failing that, a link from the candidates that the insertion's own
  search reached from the entry point, kept past the refine. A new node
  that no neighbor kept gets the same.

A route never evicts a link, and never comes from the node's own
neighborhood, which can be cut off itself. The counts are recounted
after `copy`, `compact` and `isolate`, and by the first `reserve` after
`load`, which any `add` needs, so `load` itself is not slower.

The equal-vectors test is clemlesne's reproducer from unum-cloud#815.
`add` cleared the new node's neighbors on each level before linking it.
A concurrent insertion can reach that node through its upper levels,
link to it, and leave a reverse link in it; the clear erased that link
and orphaned the other node. New nodes are zeroed when allocated, so
drop the clear, and let `form_links_to_closest_` append after whatever
is there already.

cpegeric found the same `clear` in unum-cloud#772, which also defers every
reverse link until the forward links of all levels exist. Dropping the
clear is enough here: with the routes of the previous commit, the
pattern DavIvek reported in unum-cloud#735 leaves no vector unreachable in
100,000 inserts. Closes unum-cloud#735.
@swasik
swasik force-pushed the fix-reverse-link-orphans branch from 0a8f4f0 to 78b6a3d Compare November 4, 2026 00:16
@swasik swasik changed the title Fix: Keep nodes reachable when pruning links and during concurrent add Fix: Keep nodes reachable through pruning, concurrent add and slot reuse 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.
@ashvardanian

Copy link
Copy Markdown
Member

@swasik, thank you for this, and sorry for the wait after I asked for a few days! 3.0 rebuilt the graph core, so this diff no longer applies, but the problem it tackles stays with us. 3.0 adds API designed just for it: health audits how much of the graph a search can reach, refine re-selects neighbors and re-links what it finds unreachable, and cursor walks the stored links for a manual audit of any region. Reachability will be a continuous space of improvement: 3.0 solidifies that API first, and the algorithm keeps improving after it. Your one-rule approach and the reproducers you gathered with @ofek and @cpegeric would be a great start there; I'm closing this in favor of #735 and #815, which stay open for it.

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.

2 participants