PureScript Aff fiber scheduling and event loop latency
19.5K reputation · 21 Nov 2022, 20:28 UTC
Concurrency in the Aff Monad
PureScript manages asynchronous operations through the Aff monad, which leverages the JavaScript event loop to handle non-blocking I/O. While primitives like parallel and race allow multiple computations to initiate concurrently, the underlying execution remains single-threaded.
Resource Contention and Scheduling
When deploying a high volume of concurrent fibers using fork, the interaction between the PureScript runtime and the JS engine's task queue can introduce variable latency. Specifically, there is uncertainty regarding how the runtime prioritizes fiber resumption when multiple Aff computations resolve nearly simultaneously.
If a system relies on heavy use of Ref for shared state across these fibers, the lack of OS-level parallelism means synchronization is handled via the event loop, but the specific ordering of fiber wake-ups is not explicitly guaranteed.
- Does the
purescript-affscheduler implement a specific priority queue for fiber resumption? - How does the runtime prevent event loop starvation when a large number of concurrent fibers are spawned?