Skip to content

Fix: Synchronize index copy and key reindexing; add vector precision tests - #797

Closed
Tyagiquamar wants to merge 1 commit into
unum-cloud:main-devfrom
Tyagiquamar:fix/index-dense-copy-locks-and-regressions
Closed

Tyagiquamar wants to merge 1 commit into
unum-cloud:main-devfrom
Tyagiquamar:fix/index-dense-copy-locks-and-regressions

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Resolves #454 and hardens concurrency safety for index_dense_gt::copy() and reindex_keys_().

Problem & Root Cause

  1. Concurrency safety in index_dense_gt::copy():
    copy() accessed and exported free_keys_ without holding free_keys_mutex_, and copied slot_lookup_ without taking slot_lookup_mutex_. In multithreaded environments where concurrent insertions, deletions, or compact operations occur on the source index, this led to data races or inconsistent copy state.

  2. Concurrency safety in reindex_keys_():
    reindex_keys_() acquired slot_lookup_mutex_ but modified free_keys_ without holding free_keys_mutex_.

  3. Precision acceptance (Bug: PersistAndRestore C# test fails on MacOS #454):
    Under mixed scalar precision setups (e.g. an f64 index queried or populated with f32 vectors), missing C++ unit test coverage allowed potential regression in vector precision handling.

Changes

  • include/usearch/index_dense.hpp:
    • In index_dense_gt::copy(): acquire std::unique_lockstd::mutex free_lock(free_keys_mutex_) when reserving and copying free_keys_, returning an error if reservation fails.
    • In index_dense_gt::copy(): acquire shared_lock_t lookup_lock(slot_lookup_mutex_) when copying slot_lookup_.
    • In reindex_keys_(): acquire std::unique_lockstd::mutex free_lock(free_keys_mutex_) alongside slot_lookup_mutex_ before clearing and populating slot_lookup_ and free_keys_.
  • cpp/test.cpp:
    • Added test_f64_index_accepts_f32_vectors() regression test for Bug: PersistAndRestore C# test fails on MacOS #454 verifying that an f64 index accepts f32 add/search vectors correctly and reconstructs dimensions as expected.
    • Added test_filtered_search_view_l2sq() regression test verifying filtered search on viewed memory-mapped index.

Verification

Docker CI parity test container (seladb/ubuntu2404:latest, GCC 13.3.0, CMake 3.28.3):

  • Configured: cmake -B build_docker -D CMAKE_BUILD_TYPE=RelWithDebInfo -D USEARCH_BUILD_TEST_CPP=1 -D USEARCH_USE_NUMKONG=1
  • Built: cmake --build build_docker (exit code 0)
  • Ran test suite: ./build_docker/test_cpp (exit code 0, all tests passed including test_f64_index_accepts_f32_vectors, test_filtered_search_view_l2sq, and key lookup churn tests).
  • Whitespace check: git diff --check clean with 0 warnings.

@ashvardanian

Copy link
Copy Markdown
Member

@Tyagiquamar, thanks for this! 3.0 deletes the v2 copy and key reindexing with the old core, and its concurrency now rests on read-modify-write protocols that avoid mutexes and compare-and-swap loops wherever possible, each covered by automated verification. I'm closing this as superseded; see the C SDK.

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