fallow_engine/thread_pool.rs
1//! Shared Rayon pool construction for analysis workloads.
2
3/// Worker stack size for analysis thread pools.
4///
5/// Recursive AST extraction and analysis overflow the default Rayon worker
6/// stack on deeply nested sources, so every pool that runs analysis work must
7/// be built with this size. The CLI and LSP global pools and the programmatic
8/// per-call pools share this builder so embedders cannot drift. A thread that
9/// runs analysis outside these pools, such as a Tokio blocking thread in a
10/// server, needs this size too.
11pub const WORKER_STACK_SIZE: usize = 16 * 1024 * 1024;
12
13/// Rayon pool builder preconfigured with the analysis worker stack size.
14///
15/// Each worker registers its stack size, so a bounded parse of an ordinary
16/// file runs on the worker and only a file that needs more stack gets its own
17/// thread (`fallow_extract::parse_bounded`).
18#[must_use]
19pub fn worker_pool_builder(threads: usize) -> rayon::ThreadPoolBuilder {
20 rayon::ThreadPoolBuilder::new()
21 .num_threads(threads)
22 .stack_size(WORKER_STACK_SIZE)
23 .start_handler(|_| fallow_extract::register_thread_stack(WORKER_STACK_SIZE))
24}
25
26/// Record that the current thread has `size` bytes of stack, so a bounded
27/// parse on it stays on it when the stack is large enough. A process calls
28/// this for its main thread. Pool workers from [`worker_pool_builder`] do it
29/// themselves.
30pub fn register_thread_stack(size: usize) {
31 fallow_extract::register_thread_stack(size);
32}