Skip to content

Optimize NativeString: add UTF-8 conversion fast path - #1302

Open
ValoChet wants to merge 1 commit into
uNetworking:masterfrom
ValoChet:NativeString
Open

Optimize NativeString: add UTF-8 conversion fast path#1302
ValoChet wants to merge 1 commit into
uNetworking:masterfrom
ValoChet:NativeString

Conversation

@ValoChet

Copy link
Copy Markdown
Contributor

JavaScript strings are stored either as Latin-1 or UTF-16, and uWS always convert them to UTF-8.

For small 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.
I the string is too large, it fallback calculating the exact size.

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

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

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