src: avoid copying source string in TextEncoder.encode · nodejs/node@eda91b6 · GitHub
Skip to content

Commit eda91b6

Browse files
anonrigrichardlau
authored andcommitted
src: avoid copying source string in TextEncoder.encode
`EncodeUtf8String`, which backs `TextEncoder.prototype.encode()`, copied the entire source string out of the V8 heap into a `MaybeStackBuffer` (via `WriteOneByteV2`/`WriteV2`) before encoding, allocating on the heap for strings larger than the stack buffer. `EncodeInto` already avoids this by reading the flat content directly through `v8::String::ValueView`. Read the flat content via `ValueView` instead. Because `ValueView` holds a `DisallowGarbageCollection` scope, the backing store cannot be allocated while it is alive, so the view is used in two short scopes: one to validate and compute the exact UTF-8 length, and one to encode directly into the backing store after allocation. Flattening is cached on the string, so re-acquiring the view is cheap. The rare unpaired surrogate path still copies into a mutable buffer for in-place `to_well_formed_utf16`. benchmark/util/text-encoder.js (op=encode, n=1e6, 12 runs each): len=256 len=1024 len=8192 ascii +14.1% +23.5% +43.9% one-byte (latin1) +14.4% +22.2% +12.3% two-byte (utf-16) +16.7% +20.4% +15.5% len=32 uses the unchanged small-string path (~noise). The untouched encodeInto path stayed flat (-2.0%..+0.5%) across all configurations. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> PR-URL: #63897 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 0342744 commit eda91b6

1 file changed

Lines changed: 55 additions & 50 deletions

File tree

src/encoding_binding.cc

Lines changed: 55 additions & 50 deletions

0 commit comments

Comments
 (0)