Skip to content

Pass batch operations to the binding as a structure of arrays - #129

Draft
danielAsaboro wants to merge 2 commits into
holepunchto:mainfrom
danielAsaboro:perf/batch-soa
Draft

danielAsaboro wants to merge 2 commits into
holepunchto:mainfrom
danielAsaboro:perf/batch-soa

Conversation

@danielAsaboro

Copy link
Copy Markdown

Each batch operation was a JS object the binding read with four named-property lookups (type was a prototype getter, so a call back into JS) plus a typed array lookup per buffer. Operations now live in one Uint32Array per batch: word 0 packs the type into the upper 8 bits and the column family index into the lower 24, then (buffer index, offset, length) per slice, next to a deduplicated list of backing ArrayBuffers. The binding resolves each backing buffer once per batch and bounds-checks every slice.

The bounds check also fixes a silent corruption: on main, a value whose ArrayBuffer is detached between put() and flush() is written as an empty value. It now rejects the op and the flush with INVALID_BATCH (second commit, with 2 tests that fail on main).

Numbers, thread CPU, two builds of the same toolchain alternating in one process, 21 rounds:

  • native submit per op, writes / reads: 411 → 107 ns / 296 → 67 ns
  • hypercore-storage write of 16 blocks + 31 tree nodes: 60,595 → 39,081 ns (0.64, 21/21)
  • hypercore append() of 16 blocks: 194,540 → 161,737 ns (0.83, 18/21)
  • hyperdrive batch of 2000 puts: 112.9 → 105.7 ms CPU (0.94, 10/11)

Method: two builds from the same toolchain (main and this branch) were loaded into one process and run alternately (AB, then BA) for 21 rounds after a warm-up round. The numbers are medians of process.threadCpuUsage. The hyperdrive row is 11 rounds through drive.batch(). Run with Node 24.15 on an Apple M3 Pro (darwin-arm64), which is the only platform I built.

The full suite passes on Node and Bare (91/91). hypercore-storage (79/79) passes on this branch. hypercore (457/457) passes on the first commit; the second commit only changes the JS error path.

This changes binding.cc, so the JS and the binary have to ship together with fresh prebuilds for every platform.

Each operation used to be a JS object that the native side read with four named
property lookups plus a typed array lookup per buffer. Operations now live in a
flat Uint32Array (type and column family packed into word 0, then buffer index,
offset and length per slice) next to a list of distinct backing ArrayBuffers, so
the binding resolves each backing buffer once per batch and does only pointer
arithmetic per operation. try* writes no longer push a null promise per op.
A batch op whose buffer was detached between put/get and flush made the
binding throw INVALID_BATCH from inside _flush, which nobody awaited: the
rejection went unhandled and flush() and the op promises never settled.
Reject every op and the flush with the error instead. On main the same op
was silently written as an empty value.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant