Skip to content

Commit e719fb7

Browse files
committed
src: improve StringBytes::Encode perf on ASCII
1 parent 2e597de commit e719fb7

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/encoding_binding.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,23 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
356356
const char* data = buffer.data();
357357
size_t length = buffer.length();
358358

359+
if (!ignore_bom && length >= 3) {
360+
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
361+
data += 3;
362+
length -= 3;
363+
}
364+
}
365+
359366
if (has_fatal) {
367+
// Are we perhaps ASCII?
368+
if (simdutf::validate_ascii(reinterpret_cast<const char*>(data), length)) {
369+
Local<Value> ret;
370+
if (StringBytes::Encode(env->isolate(), reinterpret_cast<const char*>(data), length, LATIN1).ToLocal(&ret)) {
371+
args.GetReturnValue().Set(ret);
372+
}
373+
return;
374+
}
375+
360376
auto result = simdutf::validate_utf8_with_errors(data, length);
361377

362378
if (result.error) {
@@ -365,13 +381,6 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
365381
}
366382
}
367383

368-
if (!ignore_bom && length >= 3) {
369-
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
370-
data += 3;
371-
length -= 3;
372-
}
373-
}
374-
375384
if (length == 0) return args.GetReturnValue().SetEmptyString();
376385

377386
Local<Value> ret;

src/string_bytes.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
531531

532532
case UTF8: {
533533
buflen = keep_buflen_in_range(buflen);
534+
535+
// ASCII fast path
536+
// TODO: remove when String::NewFromUtf8 becomes fast enough on its own
537+
// This is cheap compared to the benefits though
538+
if (simdutf::validate_ascii(buf, buflen)) {
539+
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
540+
}
541+
534542
val =
535543
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
536544
Local<String> str;

0 commit comments

Comments
 (0)