mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
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:
1 parent
99c29eb261
commit
ceaa200d16
2 files changed
+21
No files matched your search
@@ -186,6 +186,8 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
|
||||
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
|
||||
env->isolate(), "The encoded data was not valid for encoding utf-8");
|
||||
}
|
||||
|
||||
// TODO(chalker): save on utf8 validity recheck in StringBytes::Encode()
|
||||
}
|
||||
|
||||
if (length == 0) return args.GetReturnValue().SetEmptyString();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user