Skip to content

Commit 866781f

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

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/encoding_binding.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,24 @@ 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? Then we won't have to check for UTF-8
368+
if (simdutf::validate_ascii(data, length)) {
369+
Local<Value> ret;
370+
if (StringBytes::Encode(env->isolate(), data, length, LATIN1)
371+
.ToLocal(&ret)) {
372+
args.GetReturnValue().Set(ret);
373+
}
374+
return;
375+
}
376+
360377
auto result = simdutf::validate_utf8_with_errors(data, length);
361378

362379
if (result.error) {
@@ -365,13 +382,6 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
365382
}
366383
}
367384

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

377387
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(chalker): remove when String::NewFromUtf8 is fast enough itself
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)