Skip to content

Commit 31d97d1

Browse files
committed
util: improve text-decoder performance
1 parent 79e26b5 commit 31d97d1

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

lib/internal/encoding.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,7 @@ function makeTextDecoderICU() {
411411

412412
decode(input = empty, options = kEmptyObject) {
413413
validateDecoder(this);
414-
if (isAnyArrayBuffer(input)) {
415-
try {
416-
input = lazyBuffer().from(input);
417-
} catch {
418-
// If the buffer is detached,
419-
// use an empty Uint8Array to avoid TypeError
420-
input = empty;
421-
}
422-
} else if (!isArrayBufferView(input)) {
414+
if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) {
423415
throw new ERR_INVALID_ARG_TYPE('input',
424416
['ArrayBuffer', 'ArrayBufferView'],
425417
input);

src/node_i18n.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
453453
// characters times the min char size, multiplied by 2 as unicode may
454454
// take up to 2 UChars to encode a character
455455
size_t limit = 2 * converter->min_char_size() *
456-
(!flush ?
457-
input.length() :
458-
std::max(
459-
input.length(),
460-
static_cast<size_t>(
461-
ucnv_toUCountPending(converter->conv(), &status))));
456+
(!flush ? input.length()
457+
: std::max(input.length(),
458+
static_cast<size_t>(ucnv_toUCountPending(
459+
converter->conv(), &status))));
462460
status = U_ZERO_ERROR;
463461

464462
if (limit > 0)

src/util-inl.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,9 @@ SlicedArguments::SlicedArguments(
513513
template <typename T, size_t S>
514514
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
515515
v8::Local<v8::Value> value) {
516-
CHECK(value->IsArrayBufferView());
517-
Read(value.As<v8::ArrayBufferView>());
516+
CHECK(value->IsArrayBufferView() || value->IsSharedArrayBuffer() ||
517+
value->IsArrayBuffer());
518+
ReadValue(value);
518519
}
519520

520521
template <typename T, size_t S>
@@ -542,6 +543,23 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
542543
}
543544
}
544545

546+
template <typename T, size_t S>
547+
void ArrayBufferViewContents<T, S>::ReadValue(v8::Local<v8::Value> buf) {
548+
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
549+
550+
if (buf->IsArrayBufferView()) {
551+
Read(buf.As<v8::ArrayBufferView>());
552+
} else if (buf->IsArrayBuffer()) {
553+
auto ab = buf.As<v8::ArrayBuffer>();
554+
length_ = ab->ByteLength();
555+
data_ = reinterpret_cast<T*>(ab->Data());
556+
} else {
557+
auto sab = buf.As<v8::SharedArrayBuffer>();
558+
length_ = sab->ByteLength();
559+
data_ = reinterpret_cast<T*>(sab->Data());
560+
}
561+
}
562+
545563
// ECMA262 20.1.2.5
546564
inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
547565
if (!v->IsNumber()) return false;

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ class ArrayBufferViewContents {
506506
explicit inline ArrayBufferViewContents(v8::Local<v8::Object> value);
507507
explicit inline ArrayBufferViewContents(v8::Local<v8::ArrayBufferView> abv);
508508
inline void Read(v8::Local<v8::ArrayBufferView> abv);
509+
inline void ReadValue(v8::Local<v8::Value> buf);
509510

510511
inline const T* data() const { return data_; }
511512
inline size_t length() const { return length_; }

0 commit comments

Comments
 (0)