util: add fast path for text-decoder fatal flag · nodejs/node@ae842a4 · GitHub
Skip to content

Commit ae842a4

Browse files
anonrigRafaelGSS
authored andcommitted
util: add fast path for text-decoder fatal flag
PR-URL: #45803 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 344c5ec commit ae842a4

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

benchmark/util/text-decoder.js

Lines changed: 8 additions & 3 deletions

lib/internal/encoding.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const kFlags = Symbol('flags');
2929
const kEncoding = Symbol('encoding');
3030
const kDecoder = Symbol('decoder');
3131
const kEncoder = Symbol('encoder');
32+
const kFatal = Symbol('kFatal');
3233
const kUTF8FastPath = Symbol('kUTF8FastPath');
3334
const kIgnoreBOM = Symbol('kIgnoreBOM');
3435

@@ -396,17 +397,16 @@ function makeTextDecoderICU() {
396397
flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0;
397398
}
398399

399-
// Only support fast path for UTF-8 without FATAL flag
400-
const fastPathAvailable = enc === 'utf-8' && !(options?.fatal);
401-
402400
this[kDecoder] = true;
403401
this[kFlags] = flags;
404402
this[kEncoding] = enc;
405403
this[kIgnoreBOM] = Boolean(options?.ignoreBOM);
406-
this[kUTF8FastPath] = fastPathAvailable;
404+
this[kFatal] = Boolean(options?.fatal);
405+
// Only support fast path for UTF-8.
406+
this[kUTF8FastPath] = enc === 'utf-8';
407407
this[kHandle] = undefined;
408408

409-
if (!fastPathAvailable) {
409+
if (!this[kUTF8FastPath]) {
410410
this.#prepareConverter();
411411
}
412412
}
@@ -425,7 +425,7 @@ function makeTextDecoderICU() {
425425
this[kUTF8FastPath] &&= !(options?.stream);
426426

427427
if (this[kUTF8FastPath]) {
428-
return decodeUTF8(input, this[kIgnoreBOM]);
428+
return decodeUTF8(input, this[kIgnoreBOM], this[kFatal]);
429429
}
430430

431431
this.#prepareConverter();

src/node_buffer.cc

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)