Skip to content

refactor(search): Top-up search-buffer for BatchIterator only - #498

Merged
rfsaliev merged 1 commit into
mainfrom
rfsaliev/iterator-buffer-top-up
Dec 3, 2026
Merged

rfsaliev merged 1 commit into
mainfrom
rfsaliev/iterator-buffer-top-up

Conversation

@rfsaliev

@rfsaliev rfsaliev commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

This pull request improves the handling of search buffer top-up and neighbor padding in the Vamana index, particularly for cases where the search cannot reach enough points or when using the batch iterator. It moves search-buffer top-up call to check_and_supplement_search_buffer() which was introduced in #164 from the single_search extension to BatchIterator because #483 allows partial results for simple searches.

For generic non-iterative search, top-up makes sense only for cases when the graph is 'truly disconnected', i.e. there are nodes which are not accessible from entry point.

Search buffer top-up and neighbor padding improvements:

  • The logic for padding search results with invalid-neighbor markers has been clarified and updated to handle cases where the search reaches fewer points than requested, not just cancellations. Comments and checks in both MutableVamanaIndex and related files have been updated for clarity. [1] [2] [3] [4] [5]
  • The check_and_supplement_search_buffer function now uses SFINAE to allow the index to handle its own buffer supplementation if it provides a supplement_search_buffer method, improving extensibility and correctness for dynamic indexes. [1] [2]
  • The batch iterator now explicitly calls the buffer supplementation logic if the index requires ID translation, ensuring that all valid results are found even when the candidate set is exhausted mid-iteration.
  • Signatures for extensions kept unchanged with SVS_UNUSED() macro to keep compatibility with LVQ/LeanVec extensions.

Code cleanup and simplification:

  • Removed the SupplementSuppressed wrapper and the specialized svs_invoke overload for MutableVamanaIndex, as buffer supplementation is now handled generically via the updated extension logic.
  • Simplified the result copying logic to always pad with invalid-neighbor markers if fewer results are found, regardless of cancellation status, and updated related comments for clarity.

Testing improvements:

  • Added new tests to verify correct behavior when the search cannot reach enough points (disconnected graphs) and when the batch iterator needs to top up its buffer to yield all results. These tests ensure that the new logic works as intended in edge cases. [1] [2]

@rfsaliev
rfsaliev added this pull request to stack #499 December 1, 2026 17:27
@rfsaliev
rfsaliev force-pushed the rfsaliev/iterator-buffer-top-up branch from da3d14d to 7d2fd02 Compare December 1, 2026 17:34

@yuejiaointel yuejiaointel left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR looks good, but more changes are needed to completely solve top up issue
Just to summarize all problems on top of my head:

  1. Filtered search adds points that fail the filter: fixed for uncompressed; not for LVQ/LeanVec.
  2. Cancel / early stop refills with id-order points: fixed for uncompressed; not for LVQ/LeanVec.
  3. Range search does wasted work: fixed for core range search; not for the iterator path
  4. Iterator cleanup() drops deleted points, so it needs the scan: not fixed.
  5. Fewer results after many deletes: potential new problem

auto accessor = data::GetDatumAccessor();
search(query, accessor, distance, search_buffer);

if constexpr (Index::needs_id_translation) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the idea is to change where the top-up is called rather than how it works, removing call from normal search would mean we get less results after lots of deletes + consolidate, should be benchmarked to see if we are losing performance

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The top-up is needed for the case of "iterative search" -> multiple calls to greedy_search with the same buffer sorted and "cleaned up" between iterations. In such case there is the risk of 'loosing' graph connections between iterations because of buffer capacity limit. In case of simple search, this risk is minimal - only in case if consolidation breaks graph connectivity. But if consolidation breaks graph connectivity - it means that consolidation algorithm is wrong and have to be fixed.
According to performance: the top-up process is the 'brutforce' - exhaustive enumerating all verctors in index.
Unfortunately, I do not see performance risk here. Can you please explain, in which case you see potential performance issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant recall, and your experiments already show the recall loss only happens when live points ≤ window, which is rare. PR Looks good to me!

search(query, accessor, distance, search_buffer);

if constexpr (Index::needs_id_translation) {
check_and_supplement_search_buffer(index, search_buffer, query, cancel);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also LVQ/leanvec single search still call top up, so uncompressed and compressed data behave differently,. Need a separate PR to remove top up call for compressed data + padding possible short results too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agree. This why single_search signature kept unchanged - just let LVQ/LeanVec work with current infrastrucutre.
For sure, LVQ/LeanVec code to be changed, but it can be done later and independently from the current change.

);

if constexpr (Index::needs_id_translation) {
buffer.cleanup();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like each batch still calls cleanup, which removes candidates that next batch potentially need. I thought the idea is not cleanup in MutableBuffer, and let search continue with invalid/deleted candidates, maybe needs a different PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, my local benchmarks demonstrate 10-20% batch iterator performance degradation (but better recall) in case of avoided cleanup() call.
Anyway, such fixes/optimizations are out of scope for this PR.
This PR just refactors/simplify top-up call by moving it to the BatchIterator - because topup makes sense for BatchIterator only in case of proper Vamana graph connectivity.

Base automatically changed from dev/yuejiao/vamana-cancel-partial-results to main December 2, 2026 00:28
@yuejiaointel
yuejiaointel force-pushed the rfsaliev/iterator-buffer-top-up branch from 7d2fd02 to 1b5d0db Compare December 2, 2026 00:28
@rfsaliev

rfsaliev commented Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

PR looks good, but more changes are needed to completely solve top up issue Just to summarize all problems on top of my head:

  1. Filtered search adds points that fail the filter: fixed for uncompressed; not for LVQ/LeanVec.
  2. Cancel / early stop refills with id-order points: fixed for uncompressed; not for LVQ/LeanVec.
  3. Range search does wasted work: fixed for core range search; not for the iterator path
  4. Iterator cleanup() drops deleted points, so it needs the scan: not fixed.
  5. Fewer results after many deletes: potential new problem

@yuejiaointel, thank you for the review and comments.
But this PR contains just top-up refactoring to simplify code and reduce dependencies in the code.

  1. This PR has no filtered search implementation changes.
  2. This PR does not contain cancel/early stop changes.
  3. This PR does not implement range search.
  4. Yes, this why top-up is kept for Iterator.
  5. In case of non-iterative (just one greedy_search call), this PR leads fewer results for very specific cases, when 99% deleted.

Yes, changes in this PR simplify filtered and range search optimizations code changes, because removing top-up dependencies from greedy_search, but it does not contain direct optimizations for.

This PR does not contain optimizations, just refactor top-up calls which make sense for BatchIterator only in case if Vamana graph is correct.

P.S. Next my comment contains the investigation report for non-iterative cases. Yes, it is possible that filtered search can be affected as well - in case when filter rate is less than 0.5%, but it can be addressed for filtered search specifically.

@rfsaliev

rfsaliev commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Investigation: does removing the top-up from one-shot search change results?

Conclusion: only in a very specific case. A one-shot search() on a dynamic index returns different results without the top-up only when both of these hold:

  1. the live points reachable from the entry point are fewer than the search window size, and
  2. some live points are not reachable at all.

In the measurements below this happened only once the index had shrunk to roughly the search window size or less (20–50 live points left of 10,000, window 50). In the other 30 of 36 measured configurations, including every one with 100 or more live points, the results with and without the top-up are identical.

Why the case is this narrow

The dynamic search buffer keeps every candidate until it holds search_window_size valid entries, so a one-shot graph search keeps expanding until it has either filled the window or visited everything the entry point leads to. The old top-up ran only if the buffer was still short of the window after that, which means the whole reachable part of the graph held fewer live points than the window.

The top-up was therefore never a general remedy for unreachable points. An index with more reachable live points than the window fills its buffer from the graph alone, and any unreachable points were missed before this change exactly as they are after it.

What was measured

  • Data: the repository test dataset (data/test_dataset), 10,000 float32 vectors of 128 dimensions, 1,000 queries, L2 distance.
  • Index: MutableVamanaIndex built with alpha 1.2, max degree 32, construction window 64, max candidates 1000, prune-to 28.
  • Deletion patterns, each leaving 5000, 1000, 200, 100, 50 or 20 live points:
    • mass-random: keep a uniform random subset, delete the rest, consolidate() once;
    • mass-region: keep the points nearest to one random point, delete the rest, consolidate() once;
    • gradual: the same random subset, but delete half of the index at a time with a consolidate() after every round.
  • Search: batch search() with k = 10 and search window = buffer capacity = 10 or 50.
  • Compared: headers at 29c40e63 (head of Return partial results when a Vamana search is cancelled #483, top-up in every search path) against this PR (7d2fd02f, top-up in the batch iterator only). The index is built with one thread, so both versions search the same graph.
  • Reported: live points reachable from the entry point (graph traversal), valid ids returned per query, and recall@10 against brute force over the live points (a missing id counts as a miss).

Results

The six configurations where the results differ. In each of them the reachable live points are fewer than the window.

Deletion Live Reachable Window #483: ids/query #483: recall@10 This PR: ids/query This PR: recall@10
mass-random 20 5 10 10 0.543 5 0.396
mass-random 20 5 50 10 1.000 5 0.396
mass-random 50 35 50 10 1.000 10 0.838
mass-region 50 48 50 10 1.000 10 0.994
gradual 50 43 50 10 1.000 10 0.958
gradual 20 18 50 10 1.000 10 0.953

Two effects are visible:

  • Fewer than k ids (first two rows): after deleting 99.8% of the index in one step only 5 of the 20 live points are reachable, so every row now holds 5 ids and 5 invalid-neighbor markers. This is the behaviour change stated in the PR description and covered by the test "Vamana Search Of A Disconnected Graph".
  • Lower recall with full rows (last four rows): when the live count is at most the window, the old top-up scanned every live point into the buffer, which made the search exhaustive (recall 1.000). Without it the few unreachable points are missed.

Where the window is smaller than the live count, the old top-up did not return the nearest points either: it stopped at the first points the linear scan met (first row, recall 0.543).

Configurations with unreachable points where the results are identical, because the reachable live points fill the window:

Deletion Live Reachable Window recall@10, both versions
mass-random 50 35 10 0.573
gradual 20 18 10 0.948
mass-random 100 90 50 0.922
mass-random 200 188 50 0.949
gradual 1000 961 50 0.936
mass-random 5000 4992 50 0.835

Scope

  • The affected case needs an index of about the search window size or smaller that has been through consolidate(). Larger indexes are not affected, whatever the deletion pattern.
  • Within that case, the loss depends on how fragmented the graph is: large after a single deletion of 99.5% or more of the points at random, small when the survivors are neighbours of each other or the index shrank gradually.
  • Limits of the measurement: one dataset, one random seed, uncompressed data, the non-concurrent MutableVamanaIndex.
All 36 configurations
Deletion Live Reachable Window #483: ids/query #483: recall@10 This PR: ids/query This PR: recall@10 Differs
mass-random 5000 4992 10 10 0.496 10 0.496
mass-random 5000 4992 50 10 0.835 10 0.835
mass-random 1000 992 10 10 0.706 10 0.706
mass-random 1000 992 50 10 0.935 10 0.935
mass-random 200 188 10 10 0.725 10 0.725
mass-random 200 188 50 10 0.949 10 0.949
mass-random 100 90 10 10 0.642 10 0.642
mass-random 100 90 50 10 0.922 10 0.922
mass-random 50 35 10 10 0.573 10 0.573
mass-random 50 35 50 10 1.000 10 0.838 yes
mass-random 20 5 10 10 0.543 5 0.396 yes
mass-random 20 5 50 10 1.000 5 0.396 yes
mass-region 5000 4993 10 10 0.465 10 0.465
mass-region 5000 4993 50 10 0.824 10 0.824
mass-region 1000 998 10 10 0.701 10 0.701
mass-region 1000 998 50 10 0.950 10 0.950
mass-region 200 199 10 10 0.898 10 0.898
mass-region 200 199 50 10 0.994 10 0.994
mass-region 100 99 10 10 0.928 10 0.928
mass-region 100 99 50 10 0.995 10 0.995
mass-region 50 48 10 10 0.949 10 0.949
mass-region 50 48 50 10 1.000 10 0.994 yes
mass-region 20 20 10 10 0.929 10 0.929
mass-region 20 20 50 10 1.000 10 1.000
gradual 5000 4992 10 10 0.496 10 0.496
gradual 5000 4992 50 10 0.835 10 0.835
gradual 1000 961 10 10 0.744 10 0.744
gradual 1000 961 50 10 0.936 10 0.936
gradual 200 187 10 10 0.925 10 0.925
gradual 200 187 50 10 0.982 10 0.982
gradual 100 96 10 10 0.967 10 0.967
gradual 100 96 50 10 0.986 10 0.986
gradual 50 43 10 10 0.958 10 0.958
gradual 50 43 50 10 1.000 10 0.958 yes
gradual 20 18 10 10 0.948 10 0.948
gradual 20 18 50 10 1.000 10 0.953 yes

@yuejiaointel yuejiaointel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the investigation!

Follow-ups based on your findings:

  1. LVQ/LeanVec: their single_search still calls the top-up. Remove it and pad short results.
  2. Filtered search (#489): without the top-up, recall drops at very low pass rates. Add it back for that case, with the filter passed in.
  3. Range search: the C API and C++ runtime use the batch iterator, which still tops up with out-of-radius points. Switch them to the core VamanaIndex::range_search.
  4. Iterator cleanup: cleanup() removes deleted candidates, which is why the iterator needs the top-up. Maybe keeping unvisited deleted points (removing cleanup completely was slower)

@rfsaliev
rfsaliev removed this pull request from stack #499 December 3, 2026 13:07
* Do search-buffer top-up introduced in #164 for BatchIterator only because #483 allows partial results
@rfsaliev
rfsaliev force-pushed the rfsaliev/iterator-buffer-top-up branch from 1b5d0db to 696a488 Compare December 3, 2026 13:07
@rfsaliev
rfsaliev merged commit 25f6a05 into main Dec 3, 2026
16 of 17 checks passed
@rfsaliev
rfsaliev deleted the rfsaliev/iterator-buffer-top-up branch December 3, 2026 13:08
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