Skip to content

Commit

Permalink
util: improve text-decoder performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 7, 2022
1 parent 79e26b5 commit d7b5ec1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
10 changes: 1 addition & 9 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -440,7 +441,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {

ConverterObject* converter;
ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());
ArrayBufferViewContents<char> input(args[1]);
crypto::ArrayBufferOrViewContents<char> input(args[1]);
int flags = args[2]->Uint32Value(env->context()).ToChecked();

UErrorCode status = U_ZERO_ERROR;
Expand All @@ -454,9 +455,9 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
// take up to 2 UChars to encode a character
size_t limit = 2 * converter->min_char_size() *
(!flush ?
input.length() :
input.size() :
std::max(
input.length(),
input.size(),
static_cast<size_t>(
ucnv_toUCountPending(converter->conv(), &status))));
status = U_ZERO_ERROR;
Expand All @@ -473,7 +474,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& 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(),
Expand Down

0 comments on commit d7b5ec1

Please sign in to comment.