Expand description
Custom work-stealing thread pool with priority-aware cold-idle spin.
Designed to replace rayon::ThreadPool in yscv inference hot paths.
On sub-4ms inferences, rayon’s park-on-idle policy dominates MT overhead —
the park/unpark round-trip between back-to-back submissions costs hundreds
of nanoseconds per subtask. ORT’s Eigen ThreadPool sidesteps this by
holding workers in a cold-idle spin (~300µs) before parking.
This crate lands the replacement in phases:
A.1 — skeleton pool with shared Mutex<VecDeque<Task>>, install API.
A.2 — per-worker Chase-Lev deques (via crossbeam-deque) with
random-victim work-stealing, still parks on empty.
A.3 (current) — cold-idle spin before park. Workers spin for
YSCV_POOL_SPIN_US microseconds (default 0) watching for new
work before blocking on the condvar. Back-to-back inferences
arriving within the spin window skip the park/unpark round-trip.
A.4 — priority-aware scheduling + big/physical-core affinity.
A.5 — ParallelScope trait + drop-in into OnnxRunner.
A.6 — empirical tuning and default switch decision.
§API
use yscv_threadpool::YscvPool;
let pool = YscvPool::new(6).expect("build pool");
let result = pool.install(|| {
// Runs on one of the pool's worker threads.
42
});
assert_eq!(result, 42);§Unsafe policy
This crate opts into unsafe (overriding the workspace-wide
unsafe_code = deny lint) for the narrow case of extending closure
lifetimes in install, matching rayon::ThreadPool::install’s
design. Every unsafe block carries a SAFETY: comment documenting
the invariants that make it sound.
Modules§
- affinity
- CPU affinity pinning for workers.
Structs§
- Ambient
Rayon Scope - Zero-thread parallel scope for the rayon inference path.
- Persistent
Section - Session-scoped parallel region. Construct via
YscvPool::enter_section. Providesparallel_forfor the duration of the section; the section holds all pool workers in the section worker loop. - Section
Guard - RAII guard returned by
PersistentSection::install_tls. Drops the thread-local pointer back to its previous value (null or an outer section) on scope exit. NotSend: TLS is per-thread. - Yscv
Pool - Thread pool with a fixed number of worker threads. Uses per-worker
Chase-Lev deques (via
crossbeam-deque) + random-victim work-stealing. UseYscvPool::newto construct,YscvPool::installto run work.
Enums§
- Pool
Error - Errors returned by
YscvPool::new.
Traits§
- Parallel
Scope - Trait abstracting over thread-pool backends for the inference hot path.
Functions§
- current_
section - Returns the current thread-local section, if one is installed via
PersistentSection::install_tls. Used byyscv-kernelsto routepar_chunks_mut_dispatchthroughPersistentSection::parallel_forinstead of rayon’s fork-join when a session is active.