Skip to content

Commit 7a1aaa5

Browse files
ChALkeRgurgunday
andcommitted
src: improve StringBytes::Encode perf on UTF8
Co-authored-by: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 70ec5c0 commit 7a1aaa5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/encoding_binding.cc

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

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

src/string_bytes.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,24 @@ 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+
if (u16size > static_cast<size_t>(v8::String::kMaxLength)) {
547+
THROW_ERR_STRING_TOO_LONG(isolate);
548+
return MaybeLocal<Value>();
549+
}
550+
uint16_t* dst = node::UncheckedMalloc<uint16_t>(u16size);
551+
if (u16size != 0 && dst == nullptr) {
552+
THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
553+
return MaybeLocal<Value>();
554+
}
555+
size_t utf16len = simdutf::convert_valid_utf8_to_utf16(
556+
buf, buflen, reinterpret_cast<char16_t*>(dst));
557+
return ExternTwoByteString::New(isolate, dst, utf16len);
558+
}
559+
542560
val =
543561
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
544562
Local<String> str;

0 commit comments

Comments
 (0)