Skip to content

Optimize NativeString - #1302

Open
ValoChet wants to merge 2 commits into
uNetworking:masterfrom
ValoChet:NativeString
Open

Optimize NativeString#1302
ValoChet wants to merge 2 commits into
uNetworking:masterfrom
ValoChet:NativeString

Conversation

@ValoChet

@ValoChet ValoChet commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

String pooling optimization

For pooled strings, we can avoid the extra Utf8Length() pass by using a safe worst-case allocation size:

  • Latin-1 -> UTF-8 = 2 * size in worst case
  • UTF-16 -> UTF-8 = 3 * size in worst case

If this worst-case size fits in the pool buffer, we can call WriteUtf8() directly.
It writes the UTF-8 data into the pool and returns the exact number of bytes written.
The pool offset is then advanced by the actual number of bytes used.

This should cover most strings thanks to the generous 128 KB pool.
If the string is too large, it fallback calculating the exact size.

ArrayBuffer Data() function

Since Node.js 20 the new ArrayBuffer::Data() function can be used instead of ArrayBuffer::GetBackingStore()->Data().
It removes the need to instantiate a shared_ptr in synchronous usage.

Test case: 10 x writeHeader(32 bytes, 32 bytes)

Input type Before After Change
Binary 152k req/sec 155k req/sec +2%
Latin-1 147k req/sec 151k req/sec +3%
UTF-16 127k req/sec 146k req/sec +15%

@ValoChet ValoChet changed the title Optimize NativeString: add UTF-8 conversion fast path Optimize NativeString Sep 3, 2026
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