Skip to main content

Crate yscv_threadpool

Crate yscv_threadpool 

Source
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 300) 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§

PersistentSection
Session-scoped parallel region. Construct via YscvPool::enter_section. Provides parallel_for for the duration of the section; the section holds all pool workers in the section worker loop.
SectionGuard
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. Not Send: TLS is per-thread.
YscvPool
Thread pool with a fixed number of worker threads. Uses per-worker Chase-Lev deques (via crossbeam-deque) + random-victim work-stealing. Use YscvPool::new to construct, YscvPool::install to run work.

Enums§

PoolError
Errors returned by YscvPool::new.

Traits§

ParallelScope
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 by yscv-kernels to route par_chunks_mut_dispatch through PersistentSection::parallel_for instead of rayon’s fork-join when a session is active.