Skip to content

Commit aee5408

Browse files
committed
src: improve StringBytes::Encode perf on UTF8
1 parent 42b4ff4 commit aee5408

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/encoding_binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
380380
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
381381
env->isolate(), "The encoded data was not valid for encoding utf-8");
382382
}
383+
384+
// TODO(chalker): save on utf8 validity recheck in StringBytes::Encode()
383385
}
384386

385387
if (length == 0) return args.GetReturnValue().SetEmptyString();

src/string_bytes.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,16 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
539539
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
540540
}
541541

542+
if (simdutf::validate_utf8(buf, buflen)) {
543+
// We know that we are non-ASCII (and are unlikely Latin1), use 2-byte
544+
// In the most likely case of valid UTF-8, we can use this fast impl
545+
size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);
546+
uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size);
547+
size_t utf16len = simdutf::convert_valid_utf8_to_utf16(
548+
buf, buflen, reinterpret_cast<char16_t*>(dst));
549+
return ExternTwoByteString::New(isolate, dst, utf16len);
550+
}
551+
542552
val =
543553
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
544554
Local<String> str;

0 commit comments

Comments
 (0)