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 8349faa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
10 changes: 1 addition & 9 deletions lib/internal/encoding.js
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
15 changes: 7 additions & 8 deletions src/node_i18n.cc
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 @@ -453,12 +454,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& 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<size_t>(
ucnv_toUCountPending(converter->conv(), &status))));
(!flush ? input.size()
: std::max(input.size(),
static_cast<size_t>(ucnv_toUCountPending(
converter->conv(), &status))));
status = U_ZERO_ERROR;

if (limit > 0)
Expand All @@ -473,7 +472,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 8349faa

Please sign in to comment.