Solver context reuse#261
Draft
tameware wants to merge 4 commits into
Draft
Conversation
Avoid create/join on every multi-worker parallel_all_boards_n call so large chunked batches do not pay thread startup cost repeatedly. Co-authored-by: Cursor <cursoragent@cursor.com>
…ths. Batch workers previously created a fresh SolverContext (and transposition table) per chunk in calc and per board in solve. With the persistent worker pool, a thread_local context now survives across boards, chunks and consecutive batch calls, removing TT allocation churn. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves batch-solving performance by reusing per-worker SolverContext instances (and their transposition tables) across boards and across consecutive batch calls, enabled by switching board dispatch to a persistent (grow-only) worker thread pool.
Changes:
- Replaced spawn-per-call board dispatch with a persistent process-local worker pool for
parallel_all_boards_n. - Introduced
dds::internal::worker_solver_context()as athread_localpersistentSolverContextand wired it into batch solve/calc paths. - Added/extended system tests to validate correctness and reuse (thread reuse + context reuse) across consecutive batch calls.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| specs/system-concurrency.md | Documents the persistent thread-pool behavior for multi-worker runs. |
| library/tests/system/worker_context_reuse_test.cpp | New tests validating per-thread context reuse and correctness across repeated batch calls. |
| library/tests/system/parallel_boards_test.cpp | New tests validating multi-worker dispatch correctness, fail-fast behavior, and thread reuse across calls. |
| library/tests/system/BUILD.bazel | Registers the new worker_context_reuse_test target. |
| library/src/system/parallel_boards.hpp | Documents persistent pool behavior and adds a test seam for thread-creation count. |
| library/src/system/parallel_boards.cpp | Implements the persistent worker pool and exposes the thread-creation counter. |
| library/src/solver_context/solver_context.hpp | Declares internal per-thread persistent context accessors + counter seam. |
| library/src/solver_context/solver_context.cpp | Implements the thread_local worker context holder and creation counter. |
| library/src/solve_board.cpp | Uses solve_board(SolverContext&, ...) with worker_solver_context() in batch solve paths. |
| library/src/calc_tables.cpp | Uses worker_solver_context() for both sequential and parallel batch-calc paths (removing per-call context allocation). |
The pool tracks a single outstanding job, so two threads dispatching batches at the same time could overwrite each other's job and leave one caller waiting forever. run() now holds a run-scoped mutex; concurrent callers queue instead of deadlocking. Co-authored-by: Cursor <cursoragent@cursor.com>
EXPECT does not abort, so an out-of-range bno could crash with OOB access before the test reported a clean failure. Guard the bounds check and return an error instead of indexing. Co-authored-by: Cursor <cursoragent@cursor.com>
Comment on lines
+166
to
+170
| if (local.finished->fetch_add(1, std::memory_order_acq_rel) + 1 >= | ||
| local.workers) | ||
| { | ||
| cv_done_.notify_one(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Written by Fable after its inspection of @BSalita's DDSS fork. Gives a 1% to 4% multi-threaded performance improvement and a barely measurable 0.1% improvement on single-thread performance.
Per Fable:
ddss-fable-context-reuse implements half of the ddss port: batch workers now get a persistent thread_local SolverContext instead of a fresh one per chunk (calc) or per board (solve). Since the worker pool keeps its threads alive, each worker's transposition table now survives across boards, chunks, and consecutive batch calls — this is where the per-batch TT allocation churn was.