Skip to content

Optimize NativeString - #1302

Closed
ValoChet wants to merge 3 commits into
uNetworking:masterfrom
ValoChet:NativeString
Closed

ValoChet wants to merge 3 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 calculating Utf8Length() by using the 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 replace ArrayBuffer::GetBackingStore()->Data().
It removes the need to instantiate the shared_ptr and increase performance.

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
@uNetworkingAB

Copy link
Copy Markdown
Contributor

There is a critical integer underflow vulnerability in the NativeString class that leads to a heap buffer overflow. This occurs because the pool allocation check does not account for the 8-byte alignment padding applied when committing the offset.

@uNetworkingAB

Copy link
Copy Markdown
Contributor

Also to be frank with you, this release https://github.com/uNetworking/uWebSockets.js/releases/tag/v20.63.0 has put me very wary of your PRs, esp. touching NativeString.

@ValoChet

ValoChet commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor Author

There is a critical integer underflow vulnerability

I know, AI flagged that for me too, but it can't happen if the pool size is a multiple of 8. That's why I added a comment about it.

Regarding the actual implementation, it may contain an overflow issue in if (pool_offset + size > pool.size()) {.
Also, it does not handle malloc failures. It will set data to nullptr but length will not be 0. I think it would be great to either throw, or set length to 0 and display a warning.

About the #1262 ValueView event, I am sorry for what happened, I misread the V8 documentation (which is hard to find and AI was not as powerful as now) and thought strings were given as UTF-8 or UTF-16. But to be fair my PR was in draft status and I would have liked more time to test and validate it before it was merged.

@ValoChet
ValoChet deleted the NativeString branch October 1, 2026 11:59
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.

2 participants