vortex

10. Core specification: one layout, two cores

@avarok/vortex 0.1.0 · Updated

The shim talks to a pool's shared region only through CoreApi (js/src/core/api.ts). Two cores implement it over the layout and protocol below:

Core Option Code
JS (default) core: 'js' js/src/core/js/
Rust, compiled to wasm core: 'rust' crates/vortex-core, crates/vortex-wasm

This chapter is the single source of truth. js/test/core-conformance.test.mjs holds both cores to it:

Negative controls C26–C29 (8.8 table) show the test catches a wrong completion bit, a wrong inbox size, a staged claim while busy, and a wrong histogram bucket.

1. Configuration and limits

Every field is required; neither core has a default.

Field Range
slots 1..=0xFFF0 (tickets stay below the sentinels)
workers 1..=4095 (12-bit worker field)
workerHistograms boolean
spinCapUs u32; 0 disables the spin
slotBytes > 0, multiple of 8
stackBytes > 0, multiple of 16 (wasm shadow stacks; reserved by the JS core too, so the layouts match)

Derived sizes:

2. Region and Meta

A region starts at base (the JS core uses base 0 in its own memory; the Rust core grows its memory by whole pages and uses the first new page). Meta is 13 u32 at the base, in this order:

magic = 0x56545831, slots, workers, workerHistograms, spinCapUs, slotBytes, stackBytes, w32Off, w64Off, outOff, arenaOff, stacksOff, total.

Byte offsets from the base:

A worker attaches by reading Meta at the base and recomputing the layout.

3. Word arrays

w32 (u32), in order, each starting where the previous ends:

Span Words
header: HEAD, TAIL, DONE_SEQ, MAIN_WAITING, FREE_TOP 5
states slots
flags slots
ring (pull mode; unused by the shim, kept for layout parity) ringCapacity
done words doneWords
done summary ceil(doneWords / 32)
park mask ceil(workers / 32)
park words (futex words) workers
spin budget (µs) workers
free items slots
inbox heads, inbox tails workers each
inbox cells workers * inboxCapacity

w64 (u64):

4. Slot word and ticket

Every claim is one compare-and-swap that also records the owner:

A failed completion means the task was written off and its result is dropped.

5. Free list (main thread only)

A stack:

6. Inboxes

One single-producer, single-consumer ring per worker:

recover sets head = tail.

next(worker, busy) walks the inbox:

7. Completion set

A worker that completes a task:

  1. sets the slot's bit in its done word;
  2. sets the word's bit in the summary;
  3. notifies the main waiter (section 8), which is the only place DONE_SEQ advances.

The drain swaps each summary word to 0, then each announced word to 0. It reports the tickets in ascending slot order and writes them to the out buffer.

release_drained(n) releases the first n tickets in the out buffer (n at most slots), so the shim pays one call per drain rather than one release per task: it reads every drained result, releases them all, then delivers.

8. Parking and the main waiter

Parking (worker):

  1. read the park word;
  2. set the mask bit;
  3. re-check for work;
  4. wait on the word;
  5. clear the bit.

Futex words (DONE_SEQ and the park words) advance by WAKE_STEP = 2, so they are always even and the i32 result -1 never names a value.

Waking (producer): if the worker's bit is set and this call clears it, advance the word and notify one waiter. prepare/withdraw are the non-blocking form (the value for Atomics.waitAsync, or -1 if work is present).

Main waiter:

The idle spin (sync atomics) reads no clock:

The JS core uses only atomics as strong as or stronger than the Rust core's: JS Atomics are sequentially consistent.

9. Histograms

Each histogram is count, min (u64 max when empty), max, then 1728 buckets:

Recording truncates to an integer µs, with a minimum of 1. Ids: 0 run time, 1 wait time, 2 + w worker w.

Readings:

10. Call signatures

Every hot-path operation takes and returns only i32 values, so V8 can use its fast JS-to-wasm wrappers: a wait value comes back as an i32 (-1 means "do not wait"), and a µs count crosses as two u32 halves (lo, hi; exact below 2^53). The histogram readers (mean, variance, percentile, min, max) return f64: they read u64 values, run only when a summary is requested, and an i64 would cross as a BigInt.

11. What differs between the cores

Only the cost of an operation differs. The Rust core:

The JS core throws an Error instead. Chapter 9 measures the cost per core.