Skip to content

SVS Shared Thread Pool Refactor (MOD-9881)#925

Open
meiravgri wants to merge 5 commits intomainfrom
meiravg_adjustable_thpool
Open

SVS Shared Thread Pool Refactor (MOD-9881)#925
meiravgri wants to merge 5 commits intomainfrom
meiravg_adjustable_thpool

Conversation

@meiravgri
Copy link
Copy Markdown
Collaborator

@meiravgri meiravgri commented Mar 31, 2026

Step 1: Rename SVS thread pool internal methods to match shared pool terminology
Rename setNumThreads/getNumThreadssetParallelism/getParallelism and getThreadPoolCapacitygetPoolSize across VectorSimilarity. Public info API fields (numThreads, lastReservedThreads, NUM_THREADS, LAST_RESERVED_NUM_THREADS) remain unchanged. No behavioral changes.

Describe the changes in the pull request

A clear and concise description of what the PR is solving.

Which issues this PR fixes

  1. #...
  2. MOD...

Main objects this PR modified

  1. ...
  2. ...

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Note

Medium Risk
Touches SVS concurrency primitives and adds a new public C API for resizing the shared pool; mistakes could cause deadlocks/asserts or incorrect parallel execution under load.

Overview
SVS thread management is refactored to use a singleton shared thread pool (SVSIndexBase::getSharedThreadPool) with a rental model, so multiple SVS operations can borrow disjoint worker threads concurrently while allowing safe pool grow/shrink.

Per-index controls are renamed from threads to parallelism (setParallelism/getParallelism) and separated from shared pool sizing (getPoolSize), with updated asserts/semantics for single-vector vs bulk operations and tiered SVS background jobs.

Adds a new public API VecSim_UpdateThreadPoolSize() that toggles write mode (in-place for 0, async otherwise) and resizes the shared SVS pool; benchmarks/tests are updated to validate the new parallelism/pool-size behavior and stricter debug assertions.

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

…l terminology**

Rename `setNumThreads`/`getNumThreads` → `setParallelism`/`getParallelism` and `getThreadPoolCapacity` → `getPoolSize` across VectorSimilarity. Public info API fields (`numThreads`, `lastReservedThreads`, `NUM_THREADS`, `LAST_RESERVED_NUM_THREADS`) remain unchanged. No behavioral changes.
@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Mar 31, 2026

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.93%. Comparing base (f177c40) to head (c488bf0).

⚠️ Current head c488bf0 differs from pull request most recent head 3c69a48

Please upload reports for the commit 3c69a48 to get more accurate results.

Files with missing lines Patch % Lines
src/VecSim/vec_sim.cpp 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #925      +/-   ##
==========================================
- Coverage   96.98%   96.93%   -0.06%     
==========================================
  Files         129      129              
  Lines        7574     7578       +4     
==========================================
  Hits         7346     7346              
- Misses        228      232       +4     

☔ View full report in Codecov by Sentry.
📢 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.

// Assign work to rented workers (partitions 1..n-1)
for (size_t i = 0; i < num_workers; ++i) {
rented[i].assign({&f, i + 1});
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing bounds check when rent returns fewer threads

Medium Severity

The old parallel_for threw a ThreadingException when n > size_. The new code removed this guard and instead relies on rent() returning enough threads, checked only by assert. In release builds (asserts disabled), if rent() returns fewer threads than n - 1, the loop at rented[i].assign(...) accesses slots_ out of bounds — undefined behavior, likely a crash.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3445381. Configure here.

Copy link
Copy Markdown

@cursor cursor bot left a comment

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 and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

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 3c69a48. Configure here.

is_two_level_lvq{isTwoLevelLVQ(params.quantBits)},
threadpool_{std::max(size_t{SVS_VAMANA_DEFAULT_NUM_THREADS}, params.num_threads)},
impl_{nullptr} {
threadpool_{SVSIndexBase::getSharedThreadPool()}, impl_{nullptr} {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Initial parallelism zero breaks standalone SVS indexes

High Severity

VecSimSVSThreadPool initializes parallelism_ to 0, and size() now returns parallelism_ instead of the actual pool size. For standalone (non-tiered) SVS indexes created via SVSFactory::NewIndex, no code calls setParallelism() after construction. SVS internally uses pool.size() for task partitioning — with size() == 0, operations like graph building and data copying produce zero partitions, silently doing no work. The old code initialized the pool from params.num_threads, ensuring a valid default.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3c69a48. Configure here.

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.

1 participant