Skip to content

Introduce a thread pool for batch execution of parallel CPU-bound tasks. - #5354

Merged
graydon merged 1 commit into
stellar:masterfrom
dmkozh:apply_thread_pool
Jul 24, 2026
Merged

graydon merged 1 commit into
stellar:masterfrom
dmkozh:apply_thread_pool

Conversation

@dmkozh

@dmkozh dmkozh commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Description

This introduced a new BatchExecutor class that uses a pool of physical CPU-pinned (if possible) workers to execute CPU-heavy workloads in parallel. Currently this is only used for the Soroban apply stage (which is already parallel), but in the followups we'll be able to use this for more apply flow stages (pre/post apply), as well as tasks outside of apply (like parallel tx set building).

Pinning the threads helps a lot with making per-thread runtime more even on machines with hyper-threading (which is probably pretty much every CPU out there). Pooling the threads is less impactful, but it helps a bit with the allocator churn.

While this is adding 'yet another' pool of workers, I've tried to make the interface invariants simple - the executor may only be used by a single thread at once, and the execution itself is blocking. Basically this is meant to be used in a similar fashion to one-off execution threads that we have now, but in a managed fashion.

Checklist

  • Reviewed the contributing document
  • Rebased on top of master (no merge commits)
  • Ran clang-format v8.0.0 (via make format or the Visual Studio extension)
  • Compiles
  • Ran all tests
  • If change impacts performance, include supporting evidence per the performance document

Copilot AI review requested due to automatic review settings July 16, 2026 21:21
@dmkozh
dmkozh force-pushed the apply_thread_pool branch 2 times, most recently from b8a33cd to 3a8b7c1 Compare July 16, 2026 21:25

Copilot AI 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.

Pull request overview

Introduces a reusable CPU-pinned thread pool for parallel workloads, initially replacing per-stage Soroban apply threads.

Changes:

  • Adds BatchExecutor with worker reuse, affinity pinning, ordering, and exception handling.
  • Exposes the executor through application interfaces.
  • Migrates parallel Soroban cluster execution and adds unit tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/util/BatchExecutor.h Defines the batch-execution API.
src/util/BatchExecutor.cpp Implements workers, synchronization, and affinity.
src/util/test/BatchExecutorTests.cpp Tests execution, concurrency, errors, and reuse.
src/main/Application.h Exposes the shared executor.
src/main/ApplicationImpl.h Adds executor ownership and accessor.
src/main/ApplicationImpl.cpp Constructs and returns the executor.
src/main/AppConnector.h Declares executor forwarding.
src/main/AppConnector.cpp Implements executor forwarding.
src/ledger/LedgerManagerImpl.cpp Uses the pool for Soroban clusters.
src/transactions/ParallelApplyUtils.cpp Adjusts apply-thread assertions.

Comment thread src/util/BatchExecutor.h Resolved
Comment thread src/util/BatchExecutor.cpp Resolved
Comment thread src/main/ApplicationImpl.cpp Resolved
Comment thread src/util/BatchExecutor.h Resolved
Comment thread src/main/ApplicationImpl.cpp Resolved
@dmkozh
dmkozh force-pushed the apply_thread_pool branch 3 times, most recently from 560f4f7 to 2050b6d Compare July 17, 2026 19:21
This introduced a new `BatchExecutor` class that uses a pool of physical CPU-pinned (if possible) workers to execute CPU-heavy workloads in parallel. Currently this is only used for the Soroban apply stage (which is already parallel), but in the followups we'll be able to use this for more apply flow stages (pre/post apply), as well as tasks outside of apply (like parallel tx set building).

Pinning the threads helps a lot with making per-thread runtime more even on machines with hyper-threading (which is probably pretty much every CPU out there). Pooling the threads is less impactful, but it helps a bit with the allocator churn.
@dmkozh
dmkozh force-pushed the apply_thread_pool branch from 2050b6d to 902cefa Compare July 22, 2026 20:03

@graydon graydon 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.

Looks fine to me. The robot flagged a couple things you might or might-not care about:

  1. Starting at logical CPU 0 when there are more cores than you need will be a little suboptimal, because the OS tends to schedule a lot of random stuff on that CPU ("housekeeping and unsteered IRQs", it claims). So you could check to see if you have more cores than needed and if so skip core 0.
  2. If you have fewer cores than needed, you wrap around to co-schedule on an already-used one, while skipping hyperthread siblings. Which is strictly worse than letting it land on hyperthread siblings. Shouldn't happen much in practice, but shrug might want to degrade a bit more gracefully?

Neither of these are dealbreakers to me so I'll approve as-is but if you want to fix either of them and go around again, ping me when you're ready and I'll re-approve.

@graydon
graydon added this pull request to the merge queue Jul 24, 2026
Merged via the queue into stellar:master with commit 9a2afa5 Jul 24, 2026
68 of 69 checks passed
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.

3 participants