From 8349faa8ca102d62b6a2c2c3c7046832d5853963 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 7 Nov 2022 09:07:34 -0500 Subject: [PATCH] util: improve text-decoder performance --- lib/internal/encoding.js | 10 +--------- src/node_i18n.cc | 15 +++++++-------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 2ab85d9d9acb06..40c87a0a8719ff 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -411,15 +411,7 @@ function makeTextDecoderICU() { decode(input = empty, options = kEmptyObject) { validateDecoder(this); - if (isAnyArrayBuffer(input)) { - try { - input = lazyBuffer().from(input); - } catch { - // If the buffer is detached, - // use an empty Uint8Array to avoid TypeError - input = empty; - } - } else if (!isArrayBufferView(input)) { + if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) { throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'ArrayBufferView'], input); diff --git a/src/node_i18n.cc b/src/node_i18n.cc index ed7b72c31f975e..276d3b416e52f3 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -46,6 +46,7 @@ #if defined(NODE_HAVE_I18N_SUPPORT) #include "base_object-inl.h" +#include "crypto/crypto_util.h" #include "node.h" #include "node_buffer.h" #include "node_errors.h" @@ -440,7 +441,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { ConverterObject* converter; ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As()); - ArrayBufferViewContents input(args[1]); + crypto::ArrayBufferOrViewContents input(args[1]); int flags = args[2]->Uint32Value(env->context()).ToChecked(); UErrorCode status = U_ZERO_ERROR; @@ -453,12 +454,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { // characters times the min char size, multiplied by 2 as unicode may // take up to 2 UChars to encode a character size_t limit = 2 * converter->min_char_size() * - (!flush ? - input.length() : - std::max( - input.length(), - static_cast( - ucnv_toUCountPending(converter->conv(), &status)))); + (!flush ? input.size() + : std::max(input.size(), + static_cast(ucnv_toUCountPending( + converter->conv(), &status)))); status = U_ZERO_ERROR; if (limit > 0) @@ -473,7 +472,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo& args) { }); const char* source = input.data(); - size_t source_length = input.length(); + size_t source_length = input.size(); UChar* target = *result; ucnv_toUnicode(converter->conv(),