Skip to content

Commit 10bb1cc

Browse files
anonrigaddaleax
andcommitted
util: add fast path for utf8 encoding
Co-authored-by: Anna Henningsen <[email protected]>
1 parent d6c10e1 commit 10bb1cc

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

lib/internal/encoding.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// https://encoding.spec.whatwg.org
55

66
const {
7+
Boolean,
78
ObjectCreate,
89
ObjectDefineProperties,
910
ObjectGetOwnPropertyDescriptors,
@@ -28,6 +29,8 @@ const kFlags = Symbol('flags');
2829
const kEncoding = Symbol('encoding');
2930
const kDecoder = Symbol('decoder');
3031
const kEncoder = Symbol('encoder');
32+
const kUTF8FastPath = Symbol('kUTF8FastPath');
33+
const kIgnoreBOM = Symbol('kIgnoreBOM');
3134

3235
const {
3336
getConstructorOf,
@@ -49,7 +52,8 @@ const {
4952

5053
const {
5154
encodeInto,
52-
encodeUtf8String
55+
encodeUtf8String,
56+
decodeUTF8,
5357
} = internalBinding('buffer');
5458

5559
let Buffer;
@@ -397,19 +401,40 @@ function makeTextDecoderICU() {
397401
flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0;
398402
}
399403

400-
const handle = getConverter(enc, flags);
401-
if (handle === undefined)
402-
throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
404+
// Only support fast path for UTF-8 without FATAL flag
405+
const fastPathAvailable = enc === 'utf-8' && !(options?.fatal);
403406

404407
this[kDecoder] = true;
405-
this[kHandle] = handle;
406408
this[kFlags] = flags;
407409
this[kEncoding] = enc;
410+
this[kIgnoreBOM] = Boolean(options?.ignoreBOM);
411+
this[kUTF8FastPath] = fastPathAvailable;
412+
this[kHandle] = undefined;
413+
414+
if (!fastPathAvailable) {
415+
this.#prepareConverter();
416+
}
408417
}
409418

419+
#prepareConverter() {
420+
if (this[kHandle] !== undefined) return;
421+
const handle = getConverter(this[kEncoding], this[kFlags]);
422+
if (handle === undefined)
423+
throw new ERR_ENCODING_NOT_SUPPORTED(this[kEncoding]);
424+
this[kHandle] = handle;
425+
}
410426

411427
decode(input = empty, options = kEmptyObject) {
412428
validateDecoder(this);
429+
430+
this[kUTF8FastPath] &&= !(options?.stream);
431+
432+
if (this[kUTF8FastPath]) {
433+
return decodeUTF8(input, this[kIgnoreBOM]);
434+
}
435+
436+
this.#prepareConverter();
437+
413438
validateObject(options, 'options', {
414439
nullable: true,
415440
allowArray: true,

src/node_buffer.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "node_blob.h"
2525
#include "node_errors.h"
2626
#include "node_external_reference.h"
27+
#include "node_i18n.h"
2728
#include "node_internals.h"
2829

2930
#include "env-inl.h"
@@ -565,6 +566,53 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
565566
args.GetReturnValue().Set(ret);
566567
}
567568

569+
// Convert the input into an encoded string
570+
void DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
571+
Environment* env = Environment::GetCurrent(args); // list, flags
572+
573+
if (!(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer() ||
574+
args[0]->IsArrayBufferView())) {
575+
return node::THROW_ERR_INVALID_ARG_TYPE(
576+
env->isolate(),
577+
"The \"list\" argument must be an instance of SharedArrayBuffer, "
578+
"ArrayBuffer or ArrayBufferView.");
579+
}
580+
581+
ArrayBufferViewContents<char> buffer(args[0]);
582+
583+
CHECK(args[1]->IsBoolean());
584+
bool ignore_bom = args[1]->BooleanValue(env->isolate());
585+
586+
const char* data = buffer.data();
587+
size_t beginning = 0;
588+
size_t length = buffer.length();
589+
590+
if (length == 0) return args.GetReturnValue().SetEmptyString();
591+
592+
if (!ignore_bom) {
593+
char bom[] = "\xEF\xBB\xBF";
594+
595+
if (strncmp(data, bom, 3) == 0) {
596+
beginning += 3;
597+
length -= 3;
598+
}
599+
}
600+
601+
auto output = data + beginning;
602+
603+
Local<Value> error;
604+
MaybeLocal<Value> maybe_ret =
605+
StringBytes::Encode(env->isolate(), output, length, UTF8, &error);
606+
Local<Value> ret;
607+
608+
if (!maybe_ret.ToLocal(&ret)) {
609+
CHECK(!error.IsEmpty());
610+
env->isolate()->ThrowException(error);
611+
return;
612+
}
613+
614+
args.GetReturnValue().Set(ret);
615+
}
568616

569617
// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
570618
void Copy(const FunctionCallbackInfo<Value> &args) {
@@ -1282,6 +1330,7 @@ void Initialize(Local<Object> target,
12821330

12831331
SetMethod(context, target, "setBufferPrototype", SetBufferPrototype);
12841332
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
1333+
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
12851334

12861335
SetMethodNoSideEffect(context, target, "byteLengthUtf8", ByteLengthUtf8);
12871336
SetMethod(context, target, "copy", Copy);
@@ -1339,6 +1388,7 @@ void Initialize(Local<Object> target,
13391388
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
13401389
registry->Register(SetBufferPrototype);
13411390
registry->Register(CreateFromString);
1391+
registry->Register(DecodeUTF8);
13421392

13431393
registry->Register(ByteLengthUtf8);
13441394
registry->Register(Copy);

test/parallel/test-whatwg-encoding-custom-textdecoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ if (common.hasIntl) {
113113
' fatal: false,\n' +
114114
' ignoreBOM: true,\n' +
115115
' [Symbol(flags)]: 4,\n' +
116-
' [Symbol(handle)]: Converter {}\n' +
116+
' [Symbol(handle)]: undefined\n' +
117117
'}'
118118
);
119119
} else {

0 commit comments

Comments
 (0)