3. Where the speed comes from
Everything here is a hypothesis until the phase-3 harness measures it (load < 15 written beside every number). The first act of phase 2 is a per-task cost breakdown of piscina itself, so effort goes where the time actually is.
3.1 What piscina 5.3.2 pays per task (read from the source)
Main thread, run():
- a
TaskInfo(anAsyncResource: async-hooks init/destroy bookkeeping); [...this.workers.readyItems].map(w => w.interface): a fresh array plus one getter object per worker, per task, before the balancer runs (O(workers) allocation);- the balancer walks those getter objects;
MessagePort.postMessage(message, transferList): V8 serialisation of a wrapper object{task, taskId, filename, name, histogramEnabled}(the filename string is serialised every time), a mutex-protected port queue,uv_async_send;Atomics.add+Atomics.notifyon the per-worker counter;- two to three
queueMicrotask(_maybeDrain)per task, and one forclearIdleTimeout.
Worker:
- wake from
Atomics.wait,receiveMessageOnPort(deserialise the wrapper); - handler cache lookup keyed by a string built per task (
${filename}/${name}); await getHandler(...)andawait handler(task): at least two microtask hops even for a synchronous handler;- stdout/stderr
writableLengthchecks;postMessageof a response wrapper{taskId, result, error, time};Atomics.add.
Main thread, completion: either a port 'message' event (libuv async → C++ → JS,
MessageEvent-style dispatch per message) or, opportunistically, a
receiveMessageOnPort sweep over all workers on every completion
(_processPendingMessages, O(workers) per task).
tinypool is a piscina fork with the same message-per-task design. workerpool posts a message per task and per result with no shared-memory fast path. (Both from their published sources at tinypool 2.2.0 / workerpool 10.0.3; re-read in phase 3 before the comparison is published.)
3.2 Techniques, gain, risk
| # | Technique | Expected gain | Risk / cost |
|---|---|---|---|
| T1 | Workers pull from a shared MPMC ring; the main thread never routes a task to a worker in default mode | Removes the per-task worker-list allocation and balancer walk; main does O(1) per task | Must reproduce LeastBusy observable behaviour (2.5); custom balancers fall back to push mode |
| T2 | Batched completions: one Atomics.waitAsync wake drains every finished slot |
Under load, main-thread wake cost amortised over N completions instead of one event per result | Single-task latency still pays one async wake; Node's waitAsync wake latency is unmeasured and may be worse than a port event, in which case completions signal via the port and only the payload moves to shared memory |
| T3 | Adaptive spin-then-park in the worker (memory.atomic.wait32 after a budget sized from recent inter-arrival times) |
Back-to-back tasks never pay a futex sleep/wake (~5–50 µs on a loaded scheduler) | Burns CPU while spinning; distorts other tenants and utilization. Budget is capped, decays to zero when idle, and exposed as an explicit vortex option |
| T4 | No wrapper objects on the wire: task id, filename id and name id are integers in the slot header; filenames interned once per worker | Removes repeated string serialisation and the per-task cache-key string | None beyond an intern table (bounded like piscina's 1 000-entry cache) |
| T5 | Fast-lane codec for primitives/strings/small plain objects written straight into shared memory | Skips V8 serializer setup and the port queue for the common small-payload case | Exactness: every accepted value is differentially fuzzed against structuredClone; anything doubtful goes to the side lane |
| T6 | Synchronous handler fast path: if the handler returns a non-thenable, encode immediately without awaiting | Removes two microtask hops per task | Ordering must stay identical to piscina for observable effects (console.log flush ordering, async hooks) |
| T7 | Histograms and counters recorded by the worker in shared memory | No time field in responses, no main-thread recording per task |
Bucket precision must give piscina-shaped summaries (tests check types and min > 0, not exact values) |
| T8 | Zero-copy stays zero-copy: transferList, move(), SharedArrayBuffer go through the side lane unchanged |
Parity with piscina for large buffers; no regression | None; large-buffer benchmarks should show parity, not a win, and the report will say so |
| T9 | Idle-only memory.grow and fixed arenas |
No allocation or GC in the steady-state hot path | Arena sizing: oversize payloads overflow to the side lane |
Not used, with reasons:
- Per-worker deques with work stealing: stealing pays off when workers spawn tasks; in piscina's model the main thread is the only producer, so one bounded MPMC ring has less overhead and simpler failure analysis. Revisit only if the benchmark shows head contention on the ring at high core counts.
- Running the handler inside wasm: the handler is user JS; see 2.3.
- Main-thread spinning: forbidden, it would block the user's event loop.
3.3 Honest limits
- A JS↔wasm call costs a few nanoseconds; Rust in wasm is not faster than hand-written
JS
Atomicsfor a single ring operation. The Rust core earns its place through correctness (one implementation of the concurrent algorithms, model-checked, shared by Node and browsers, no GC in the hot path) and through the batched/pull architecture, not through raw instruction speed. The phase-3 report will include an ablation (same architecture with the ring ops in plain JS) so this claim is measured, not asserted. - For CPU-bound tasks of milliseconds, every pool is within noise of every other; the wins are in the per-task overhead regime (tasks under ~100 µs) and in saturation throughput.
- "Fastest in the world" will be claimed only for the specific workloads measured, against the pinned versions, on stated hardware, with load beside each number.