Skip to content

[MOD-16696] Use one-byte HNSW node locks#983

Open
GuyAv46 wants to merge 2 commits into
mainfrom
codex/hnsw-one-byte-node-locks
Open

[MOD-16696] Use one-byte HNSW node locks#983
GuyAv46 wants to merge 2 commits into
mainfrom
codex/hnsw-one-byte-node-locks

Conversation

@GuyAv46

@GuyAv46 GuyAv46 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Revives the old one-byte mutex idea on top of current main, but keeps the graph-data layout natural by moving per-node HNSW locks into an index-owned lock vector.

Changes:

  • Adds vecsim_stl::one_byte_mutex, backed by std::atomic<uint8_t>::wait/notify_one.
  • Removes std::mutex neighborsGuard from ElementGraphData.
  • Adds elementLocks, a per-internal-id lock vector owned by HNSWIndex and resized with index capacity.
  • Updates HNSW lock sites to lock by internal id instead of graph-data pointer.
  • Updates HNSW memory estimation and allocator test accounting for the new 1-byte-per-node lock vector.

Expected memory effect:

  • ElementGraphData no longer carries a std::mutex per vector.
  • The replacement storage is one byte per capacity slot, in a separate vector.
  • This avoids extending packed struct layout over graph-data pointers/flexible link arrays.

Validation

Local validation in a fresh worktree from origin/main:

  • make build
  • ctest --output-on-failure -R 'test_allocator|test_hnsw|test_hnsw_parallel' -j$(nproc)
  • ./test_hnsw_parallel
  • ./test_allocator --gtest_filter='*hnsw*'
  • ROOT=/home/guy/Code/VectorSimilarity-hnsw-one-byte-locks ./test_hnsw --gtest_brief=1

Note: make check-format currently reports pre-existing unrelated formatting violations in files outside this change (for example batch_iterator.h, benchmark headers, and existing constructors in test_allocator.cpp). The files touched in this PR were formatted selectively.

Benchmark request

Please run benchmark comparison for this PR. I am adding the benchmarks-all label to trigger the benchmark workflow.


Note

Medium Risk
Touches core HNSW concurrency on graph mutations and parallel search; correctness depends on the new lock primitive and id-indexed locking staying aligned with existing deadlock ordering.

Overview
HNSW neighbor-link locking moves out of packed graph nodes into an index-owned elementLocks vector keyed by internal id, with a new compact vecsim_stl::one_byte_mutex (atomic wait/notify) instead of std::mutex on each ElementGraphData.

lockNodeLinks / unlockNodeLinks now take only idType; search, insert, delete/repair, batch iteration, and debug paths were updated accordingly. elementLocks is initialized on construct/load and grows with resizeIndexCommon. Memory estimation and allocator tests count one byte per capacity slot for the lock vector.

Reviewed by Cursor Bugbot for commit e6da02b. Bugbot is set up for automated code reviews on this repo. Configure here.

@jit-ci

jit-ci Bot commented Jul 2, 2026

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@GuyAv46 GuyAv46 marked this pull request as ready for review July 6, 2026 08:36
@GuyAv46 GuyAv46 requested a review from alonre24 July 6, 2026 08:36
Comment thread src/VecSim/algorithms/hnsw/hnsw.h
Comment thread src/VecSim/utils/vecsim_stl.h
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.22642% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.10%. Comparing base (45daaf3) to head (81dc0e6).

Files with missing lines Patch % Lines
src/VecSim/algorithms/hnsw/hnsw.h 94.73% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #983      +/-   ##
==========================================
- Coverage   97.12%   97.10%   -0.03%     
==========================================
  Files         141      141              
  Lines        8164     8176      +12     
==========================================
+ Hits         7929     7939      +10     
- Misses        235      237       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GuyAv46 GuyAv46 changed the title [codex] Use one-byte HNSW node locks MOD-16696 Use one-byte HNSW node locks Jul 6, 2026
@GuyAv46 GuyAv46 changed the title MOD-16696 Use one-byte HNSW node locks [MOD-16696] Use one-byte HNSW node locks Jul 6, 2026
@GuyAv46 GuyAv46 requested review from dor-forer and ofiryanai July 6, 2026 13:03
Comment thread src/VecSim/utils/vecsim_stl.h Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 81dc0e6. Configure here.

Comment thread src/VecSim/algorithms/hnsw/hnsw.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants