Skip to content

Make MutableBQVectors safe for concurrent inserts - #851

Open
tlwillke wants to merge 4 commits into
mainfrom
bq-concurrent-inserts
Open

tlwillke wants to merge 4 commits into
mainfrom
bq-concurrent-inserts

Conversation

@tlwillke

Copy link
Copy Markdown
Collaborator

MutableBQVectors could lose data when several threads called encodeAndSet or setZero at once:

  • Array growth was unlocked, so a store made while another thread copied the array could be dropped.
  • The count was a plain int updated with Math.max, so updates could be lost.

This change follows MutablePQVectors: encoding stays outside the lock, then growth and the store happen together in one synchronized method, and the count is an AtomicInteger.

It also fixes setZero, which made the zero code 8× too long (it used the byte size as a count of longs). It now matches BinaryQuantization.encodeAll.

Invalid ordinals (negative or Integer.MAX_VALUE) now throw IllegalArgumentException, as in MutablePQVectors, instead of ArrayIndexOutOfBoundsException.

Testing

New TestBinaryQuantization.testConcurrentEncodeAndSet: 16 threads insert 20,000 interleaved ordinals (some zeroed), then the test checks the count and every code.
New TestBinaryQuantization.testInvalidOrdinal checks the new exception.

  • On Ice Lake (8 cores), JDK 22: passes with this change; fails 3 of 3 runs on the old code (count 19,998 / 19,999, and the wrong zero-code length).

Grow and store under one lock and count with an AtomicInteger, as MutablePQVectors does.
Also size setZero's code in longs, not bytes, and reject invalid ordinals.
@github-actions

Copy link
Copy Markdown
Contributor

Before you submit for review:

  • Does your PR follow guidelines from CONTRIBUTIONS.md?
  • Did you summarize what this PR does clearly and concisely?
  • Did you include performance data for changes which may be performance impacting?
  • Did you include useful docs for any user-facing changes or features?
  • Did you include useful javadocs for developer oriented changes, explaining new concepts or key changes?
  • Did you rebase your branch onto the latest main for regression testing and PR submission?
  • Did you trigger regression testing via Run Bench Main and review results?
  • Did you adhere to the code formatting guidelines (TBD)
  • Did you group your changes for easy review, providing meaningful descriptions for each commit?
  • Did you ensure that all files contain the correct copyright header?
  • Did you add documentation for this feature to the release notes directory?

If you did not complete any of these, then please explain below.

@tlwillke tlwillke self-assigned this Nov 28, 2026
@tlwillke tlwillke added the bug Something isn't working label Nov 28, 2026

@r-devulap r-devulap 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.

This looks fine to me. Should we be concerned about measuring any performance regressions? It looks like we dont have anything in Jvector that I can run directly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.1.0-rc.2 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants