Repository navigation
perf(string-table): write UTF-8 bytes directly into the output buffer - #125
Merged
Merged
Conversation
…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
marked this pull request as ready for review
November 18, 2026 00:32
szegedi
approved these changes
Nov 18, 2026
Merged
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.