Repository navigation
Conversation
StreamBase::WriteString() created a String::ValueView twice, the first time in StringBytes::StorageSize() only to read the length. A ValueView flattens the string, so writing a string built by concatenation, such as the header of an HTTP response, allocated a flat copy on the V8 heap before copying it again into the write buffer. Read the length from the string in StorageSize(), and copy one-byte strings that fit the stack buffer with String::WriteOneByte(), which does not flatten. For UTF-8 the characters go to the last third of the buffer and are converted into the rest of it: a Latin-1 character takes at most two bytes. Assisted-by: Claude Signed-off-by: Guilherme AraΓΊjo <arauujogui@gmail.com>
araujogui
force-pushed
the
src-stream-write-no-flatten
branch
from
November 12, 2026 14:21
cb1b06e to
f89198f
Compare
Signed-off-by: Guilherme AraΓΊjo <arauujogui@gmail.com>
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67435 +/- ##
==========================================
+ Coverage 90.69% 90.70% +0.01%
==========================================
Files 801 801
Lines 281875 281884 +9
Branches 54317 54323 +6
==========================================
+ Hits 255652 255695 +43
+ Misses 16348 16312 -36
- Partials 9875 9877 +2
π New features to boost your workflow:
|
Member
Author
|
https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1904/ benchmark/http/end-string.js |
Also skip the flatten in Writev() and for strings larger than the stack buffer. Assisted-by: Claude Signed-off-by: Guilherme AraΓΊjo <arauujogui@gmail.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.
StreamBase::WriteString()created aString::ValueViewtwice, the first time inStringBytes::StorageSize()only to read the length. AValueViewflattens the string. So a string built by concatenation, like an HTTP response header, was copied to a new flat string on the V8 heap, then copied again into the write buffer.StreamBase::Writev()did the same for each string chunk.Now
StorageSize()reads the length from the string, and one-byte strings are copied withString::WriteOneByte(), which does not flatten. For UTF-8, the characters go to the last third of the buffer and are converted into the rest: a Latin-1 character takes at most two bytes. Two-byte strings, and UTF-8 strings whose buffer has no room for that, useStringBytes::Write()as before.WriteString()andWritev()share this, so it covers every HTTP/1.1 response header, with a string body or a Buffer body, and TLS sockets too.Flat strings written as UTF-8 pay one extra
memcpyof at most 65535 bytes. OtherStringBytes::Write()callers (fs,Buffer) are not changed.