mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
string_decoder: decode UTF-8 via StringBytes::Encode
StringDecoder used v8::String::NewFromUtf8() for UTF-8, while
Buffer#toString() goes through StringBytes::Encode(), which has
simdutf-backed ASCII, Latin-1 and UTF-16 paths and only falls back
to NewFromUtf8() for input that contains invalid sequences. Route
the decoder through the same function, so streams with
setEncoding('utf8') and readline decode at the same speed as
Buffer#toString(). U+FFFD replacement is unchanged because invalid
input still ends up in NewFromUtf8(), and the ERR_STRING_TOO_LONG
check is kept explicit so over-long input fails as before.
benchmark/string_decoder/string-decoder.js (encoding=utf8) and a
readline-over-pipe workload improve by 2-3x for chunks >= 1 KiB;
64 KiB newline-delimited JSON round trips over child stdio improve
by ~30% on the reading side alone.
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65324
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
29a083cffb
commit
e19c13b87c
2 files changed
+94
-15
No files matched your search
+13
-15
@@ -28,23 +28,21 @@ MaybeLocal<String> MakeString(Isolate* isolate,
|
||||
const char* data,
|
||||
size_t length,
|
||||
enum encoding encoding) {
|
||||
MaybeLocal<Value> ret;
|
||||
if (encoding == UTF8) {
|
||||
MaybeLocal<String> utf8_string;
|
||||
if (length <= static_cast<size_t>(v8::String::kMaxLength)) {
|
||||
utf8_string = String::NewFromUtf8(
|
||||
isolate, data, v8::NewStringType::kNormal, length);
|
||||
}
|
||||
if (utf8_string.IsEmpty()) {
|
||||
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
|
||||
return MaybeLocal<String>();
|
||||
} else {
|
||||
return utf8_string;
|
||||
}
|
||||
} else {
|
||||
ret = StringBytes::Encode(isolate, data, length, encoding);
|
||||
// StringBytes::Encode() would report an over-long UTF-8 input as
|
||||
// ERR_BUFFER_TOO_LARGE (or clamp it); keep reporting it the way this
|
||||
// decoder always has.
|
||||
if (encoding == UTF8 && length > static_cast<size_t>(v8::String::kMaxLength))
|
||||
[[unlikely]] {
|
||||
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
|
||||
return MaybeLocal<String>();
|
||||
}
|
||||
|
||||
// For UTF-8 this takes the simdutf-backed ASCII / Latin-1 / UTF-16 paths and
|
||||
// only falls back to v8::String::NewFromUtf8() (the previous unconditional
|
||||
// path here) for input containing invalid sequences, so U+FFFD replacement
|
||||
// is unchanged.
|
||||
MaybeLocal<Value> ret = StringBytes::Encode(isolate, data, length, encoding);
|
||||
|
||||
if (ret.IsEmpty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user