vortex

3. Where the speed comes from

@avarok/vortex 0.1.0 · Updated

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():

Worker:

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:

3.3 Honest limits