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 11, 2022
1 parent 4107ce0 commit d6c10e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
12 changes: 1 addition & 11 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
} = primordials;

const {
ERR_ENCODING_INVALID_ENCODED_DATA,
ERR_ENCODING_NOT_SUPPORTED,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
Expand Down Expand Up @@ -411,11 +410,6 @@ function makeTextDecoderICU() {

decode(input = empty, options = kEmptyObject) {
validateDecoder(this);
if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) {
throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView'],
input);
}
validateObject(options, 'options', {
nullable: true,
allowArray: true,
Expand All @@ -426,11 +420,7 @@ function makeTextDecoderICU() {
if (options !== null)
flags |= options.stream ? 0 : CONVERTER_FLAGS_FLUSH;

const ret = _decode(this[kHandle], input, flags);
if (typeof ret === 'number') {
throw new ERR_ENCODING_INVALID_ENCODED_DATA(this.encoding, ret);
}
return ret;
return _decode(this[kHandle], input, flags, this.encoding);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details);
V(ERR_CRYPTO_JOB_INIT_FAILED, Error) \
V(ERR_DLOPEN_DISABLED, Error) \
V(ERR_DLOPEN_FAILED, Error) \
V(ERR_ENCODING_INVALID_ENCODED_DATA, TypeError) \
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
V(ERR_INVALID_ADDRESS, Error) \
V(ERR_INVALID_ARG_VALUE, TypeError) \
Expand Down
24 changes: 18 additions & 6 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,25 @@ void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 3); // Converter, Buffer, Flags
CHECK_GE(args.Length(), 4); // Converter, Buffer, Flags, Encoding

ConverterObject* converter;
ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());

if (!(args[1]->IsArrayBuffer() || args[1]->IsSharedArrayBuffer() ||
args[1]->IsArrayBufferView())) {
return node::THROW_ERR_INVALID_ARG_TYPE(
env->isolate(),
"The \"input\" argument must be an instance of SharedArrayBuffer, "
"ArrayBuffer or ArrayBufferView.");
}

ArrayBufferViewContents<char> input(args[1]);
int flags = args[2]->Uint32Value(env->context()).ToChecked();

CHECK(args[3]->IsString());
Local<String> from_encoding = args[3].As<String>();

UErrorCode status = U_ZERO_ERROR;
MaybeStackBuffer<UChar> result;

Expand Down Expand Up @@ -524,14 +536,14 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
Local<Value> ret;
if (encoded.ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
} else {
args.GetReturnValue().Set(error);
return;
}

return;
}

args.GetReturnValue().Set(status);
node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
env->isolate(),
"The encoded data was not valid for encoding %s",
*node::Utf8Value(env->isolate(), from_encoding));
}

ConverterObject::ConverterObject(
Expand Down

0 comments on commit d6c10e1

Please sign in to comment.