6. Where piscina 5.3.2 spends per-task time (profile, phase 2a)
These are CPU-profile shares, not benchmark results. Nothing here is a throughput
claim. Tools: tools/profile-piscina/run.cjs (drives empty tasks) and
tools/profile-piscina/analyze.cjs (self-time aggregation of a .cpuprofile).
Setup: piscina 5.3.2 from the npm tarball, Node 20.20.2, Apple M3 Max (16 cores),
minThreads = maxThreads = 2, empty handler (n) => n, node --cpu-prof (one
profile per thread). Machine 1-minute load 7.5–8.4 during the runs (shared machine;
shares are robust to that, absolute times are not).
Reproduce:
node --cpu-prof --cpu-prof-dir=<out> tools/profile-piscina/run.cjs <piscina-dir> 200000 2 1
node --cpu-prof --cpu-prof-dir=<out> tools/profile-piscina/run.cjs <piscina-dir> 400000 2 64
node tools/profile-piscina/analyze.cjs <out>/<file>.cpuprofile 20
6.1 One task in flight (latency regime), 200 000 tasks
| Thread | Busy | Largest self-time items |
|---|---|---|
| main | 39 % (61 % idle) | postTask 11.6 %, get interface 5.5 %, (program) 6.5 %, GC 4.0 %, postMessage 1.5 % |
| worker 1 | 21 % (79 % in Atomics.wait) |
postMessage 9.4 %, receiveMessageOnPort 5.2 %, onMessage 2.2 %, getHandler 2.1 % |
| worker 2 | 0 % | never given a task: the balancer always picks the first idle worker |
Reading: a round trip is about 14 µs of wall time on this machine, of which the two threads together are busy for well under half; the rest is wake-up latency in both directions (futex wake of the worker, libuv/port wake of the main thread). This is the regime techniques T3 (spin-then-park) and the completion-wake choice in T2 address.
6.2 64 tasks in flight (throughput regime), 400 000 tasks
| Thread | Busy | Largest self-time items |
|---|---|---|
| main | 80 % (20 % idle) | postTask 21.9 %, get interface 8.9 %, (program) 6.0 %, GC 5.5 %, postMessage 3.8 %, receiveMessageOnPort 4.6 %, microtask machinery ~5 %, async_hooks 1.6 % |
| workers 1, 2 | 15 % each (85 % in Atomics.wait) |
postMessage ~4 %, receiveMessageOnPort ~4–6 %, onMessage ~3 %, getHandler ~1.7 % |
Line-level ticks inside postTask (main thread): 528 of ~660 samples are on the
Atomics.notify line, 121 on port.postMessage. piscina issues a futex wake syscall
for every task, whether or not the worker is waiting.
Reading:
- The main thread is the bottleneck; workers starve. With
concurrentTasksPerWorker = 1each worker finishes, posts, and then waits until the main thread has processed the result and dispatched the next task: a strict ping-pong through the busiest thread. - Unconditional
Atomics.notifyis the single largest item (~17 % of main-thread time). - Per-task worker-list materialisation (
get interface, plus the spread/map that calls it) is ~9 %. - Serialisation proper (
postMessage+receiveMessageOnPortnative) is a smaller share than the scheduling overhead around it.
6.3 Consequences for the design (applied to chapters 2 and 3)
- Workers must be able to take their next task without a main-thread round trip.
With the default FIFO queue, queued tasks are published to the shared ring as soon
as they are submitted (up to arena capacity), and still count as queued in
queueSizeuntil a worker claims them; abort of a ring-resident task is a slot CASQueued → Cancelledthat the claiming worker honours. With a customtaskQueuethe user's queue decides order, so the ring holds only what piscina would already have dispatched (no prefetch); otherwise a later high-priority task could not overtake ring-resident ones. memory.atomic.notifyis issued only when the parked-worker count is non-zero (T3); the producer never pays a syscall when every worker is busy or spinning.- No per-task allocation proportional to the number of workers in default mode (T1).
- The completion side batches (T2); the 6.1 regime decides whether the main-thread
wake uses
Atomics.waitAsyncor a port event, by measurement in phase 3.