mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
An encoded message allocated a node array (four nodes of 184 bytes to
start, trimmed when encoding ended), a property array per object, a copy
of every key and string, and the encoder's visit, identity and stack
arrays. A port that queues many messages kept all of it until delivery:
a {id, name} object took 15 allocations and 1.9 KB, and most of
postMessage's time went into page faults on that memory.
- The encoder builds nodes, properties and visits in arrays that start on
the stack (8 entries), keeps borrowed references to keys and strings,
and copies the result into one allocation: the header, the nodes, the
properties grouped by node, the typed records and the text. A node is
40 bytes; the per-kind fields share a union. ArrayBuffer contents stay
in their own allocation.
- Decoding uses stack arrays for messages of up to 8 nodes and allocates
the storage table only when the message has ArrayBuffer contents.
- Freeing a message frees the block, and walks the nodes only when the
message holds ports, native handles or ArrayBuffer contents.
- The publication check reads the root before collecting the graph, so
a message whose root is not frozen allocates nothing for it.
The same object message now takes 4 allocations and 302 bytes, and a
number 3 allocations and 129 bytes. bench/channel-messages runs in 161 ms
instead of 250 ms (Node: 340 ms), with a peak RSS of 45 MB instead of
115 MB, and bench/worker-messages in 273 ms instead of 348 ms.