Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 89390a6

Browse filesBrowse files
anonrigRafaelGSS
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 c9cf399 commit 89390a6
Copy full SHA for 89390a6

File tree

Expand file treeCollapse file tree

3 files changed

+25
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-11
lines changed
Open diff view settings
Collapse file

‎lib/internal/encoding.js‎

Copy file name to clipboardExpand all lines: lib/internal/encoding.js
+1-9Lines 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);
Collapse file

‎src/util-inl.h‎

Copy file name to clipboardExpand all lines: src/util-inl.h
+23-2Lines changed: 23 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+
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;
Collapse file

‎src/util.h‎

Copy file name to clipboardExpand all lines: src/util.h
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.