src: improve StringBytes::Encode perf on UTF8

Co-authored-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: https://github.com/nodejs/node/pull/61131
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Nikita SkovorodaandGürgün Dayıoğlu authored and GitHub committed 2026-01-21 18:40:17 +00:00
1 parent 98d23317f0
commit 77e8d443ac
2 files changed
+21

No files matched your search

+19
View File
@@ -539,6 +539,25 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
}
if (buflen >= 32 && simdutf::validate_utf8(buf, buflen)) {
// We know that we are non-ASCII (and are unlikely Latin1), use 2-byte
// In the most likely case of valid UTF-8, we can use this fast impl
// For very short input, it is slower, so we limit min size
size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);
if (u16size > static_cast<size_t>(v8::String::kMaxLength)) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
return MaybeLocal<Value>();
}
uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size);
if (u16size != 0 && dst == nullptr) {
THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
return MaybeLocal<Value>();
}
size_t utf16len = simdutf::convert_valid_utf8_to_utf16(
buf, buflen, reinterpret_cast<char16_t*>(dst));
return ExternTwoByteString::New(isolate, dst, utf16len);
}
val =
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
Local<String> str;