Skip to content

perf(string-table): write UTF-8 bytes directly into the output buffer - #125

Merged
szegedi merged 3 commits into
mainfrom
BridgeAR/2026-11-17-string-table-port
Nov 18, 2026
Merged

szegedi merged 3 commits into
mainfrom
BridgeAR/2026-11-17-string-table-port

Conversation

@BridgeAR

Copy link
Copy Markdown
Member

Port of #121 by @nuzayets, with the original commit preserved.

String tables avoid a UTF-8 buffer allocation for each new string. The follow-up copies decoded input bytes and rejects undersized output buffers.

On Node 24.20.0 (V8 13.6.233.17-node.53), building and encoding 40 synthetic tables with 4,000 strings each takes 48.4 ms instead of 72.7 ms. That is 33% less time across two fresh processes, each with seven trials and the fastest and slowest removed.

nuzayets and others added 3 commits November 17, 2026 20:37
…ch string

StringTable encoded every new string into its own Uint8Array as it was
added. A profile with thousands of distinct file paths and function names
made thousands of small allocations, and every array over V8's 64-byte
on-heap typed array limit got its own off-heap backing store, which adds
to V8's external memory.

Track each string's UTF-8 length instead and write the bytes straight into
the output buffer when the table is encoded. Strings read by decode keep
their original bytes so re-encoding stays byte-for-byte identical.
Buffer.slice() shares the caller's storage, so later input changes corrupt
re-encoded string tables. Keep an independent copy of decoded bytes.

Buffer.write() and TextEncoder.encodeInto() can truncate strings without
throwing. Validate output capacity before encoding.
For mixed ASCII names and paths, Node 22.23.4 (V8 12.4.254.21-node.57)
takes 113-115 ns per call instead of 133-143 ns. Each of two fresh
processes uses seven trials of 262,144 calls, discarding the fastest
and slowest.
@BridgeAR
BridgeAR marked this pull request as ready for review November 18, 2026 00:32
@BridgeAR
BridgeAR requested a review from a team as a code owner November 18, 2026 00:32
@szegedi
szegedi merged commit 37a6ed1 into main Nov 18, 2026
13 checks passed
@szegedi
szegedi deleted the BridgeAR/2026-11-17-string-table-port branch November 18, 2026 10:04
@szegedi szegedi mentioned this pull request Nov 19, 2026
szegedi pushed a commit that referenced this pull request Nov 19, 2026
…#125)

* perf: write string table bytes at encode time instead of buffering each string

StringTable encoded every new string into its own Uint8Array as it was
added. A profile with thousands of distinct file paths and function names
made thousands of small allocations, and every array over V8's 64-byte
on-heap typed array limit got its own off-heap backing store, which adds
to V8's external memory.

Track each string's UTF-8 length instead and write the bytes straight into
the output buffer when the table is encoded. Strings read by decode keep
their original bytes so re-encoding stays byte-for-byte identical.

* fix(string-table): preserve buffer ownership and capacity checks

Buffer.slice() shares the caller's storage, so later input changes corrupt
re-encoded string tables. Keep an independent copy of decoded bytes.

Buffer.write() and TextEncoder.encodeInto() can truncate strings without
throwing. Validate output capacity before encoding.

* perf(protobuf): pre-count code units when measuring UTF-8 strings

For mixed ASCII names and paths, Node 22.23.4 (V8 12.4.254.21-node.57)
takes 113-115 ns per call instead of 133-143 ns. Each of two fresh
processes uses seven trials of 262,144 calls, discarding the fastest
and slowest.

---------

Co-authored-by: Dmitry S. <1832518+nuzayets@users.noreply.github.com>
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.

3 participants