Skip to content

Commit 1dc91ab

Browse files
anonrigdanielleadams
authored andcommitted
util: improve text-decoder performance
PR-URL: #45363 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 762d285 commit 1dc91ab

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

lib/internal/encoding.js

+1-9
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/util-inl.h

+23-2
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+
DCHECK(value->IsArrayBufferView() || value->IsSharedArrayBuffer() ||
517+
value->IsArrayBuffer());
518+
ReadValue(value);
518519
}
519520

520521
template <typename T, size_t S>
@@ -542,6 +543,26 @@ 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+
DCHECK(buf->IsArrayBufferView() || buf->IsSharedArrayBuffer() ||
550+
buf->IsArrayBuffer());
551+
552+
if (buf->IsArrayBufferView()) {
553+
Read(buf.As<v8::ArrayBufferView>());
554+
} else if (buf->IsArrayBuffer()) {
555+
auto ab = buf.As<v8::ArrayBuffer>();
556+
length_ = ab->ByteLength();
557+
data_ = static_cast<T*>(ab->Data());
558+
} else {
559+
CHECK(buf->IsSharedArrayBuffer());
560+
auto sab = buf.As<v8::SharedArrayBuffer>();
561+
length_ = sab->ByteLength();
562+
data_ = static_cast<T*>(sab->Data());
563+
}
564+
}
565+
545566
// ECMA262 20.1.2.5
546567
inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
547568
if (!v->IsNumber()) return false;

src/util.h

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ class ArrayBufferViewContents {
505505
explicit inline ArrayBufferViewContents(v8::Local<v8::Object> value);
506506
explicit inline ArrayBufferViewContents(v8::Local<v8::ArrayBufferView> abv);
507507
inline void Read(v8::Local<v8::ArrayBufferView> abv);
508+
inline void ReadValue(v8::Local<v8::Value> buf);
508509

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

0 commit comments

Comments
 (0)