You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments