Skip to content

Commit

Permalink
util: improve text-decoder performance
Browse files Browse the repository at this point in the history
PR-URL: nodejs#45363
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
anonrig committed Nov 28, 2022
1 parent 22e9f5b commit 26fbc98
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 1 addition & 3 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ function makeTextDecoderICU() {

decode(input = empty, options = kEmptyObject) {
validateDecoder(this);
if (isAnyArrayBuffer(input)) {
input = lazyBuffer().from(input);
} else if (!isArrayBufferView(input)) {
if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) {
throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView'],
input);
Expand Down
25 changes: 23 additions & 2 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,9 @@ SlicedArguments::SlicedArguments(
template <typename T, size_t S>
ArrayBufferViewContents<T, S>::ArrayBufferViewContents(
v8::Local<v8::Value> value) {
CHECK(value->IsArrayBufferView());
Read(value.As<v8::ArrayBufferView>());
DCHECK(value->IsArrayBufferView() || value->IsSharedArrayBuffer() ||
value->IsArrayBuffer());
ReadValue(value);
}

template <typename T, size_t S>
Expand Down Expand Up @@ -542,6 +543,26 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
}
}

template <typename T, size_t S>
void ArrayBufferViewContents<T, S>::ReadValue(v8::Local<v8::Value> buf) {
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment");
DCHECK(buf->IsArrayBufferView() || buf->IsSharedArrayBuffer() ||
buf->IsArrayBuffer());

if (buf->IsArrayBufferView()) {
Read(buf.As<v8::ArrayBufferView>());
} else if (buf->IsArrayBuffer()) {
auto ab = buf.As<v8::ArrayBuffer>();
length_ = ab->ByteLength();
data_ = static_cast<T*>(ab->Data());
} else {
CHECK(buf->IsSharedArrayBuffer());
auto sab = buf.As<v8::SharedArrayBuffer>();
length_ = sab->ByteLength();
data_ = static_cast<T*>(sab->Data());
}
}

// ECMA262 20.1.2.5
inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
if (!v->IsNumber()) return false;
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ class ArrayBufferViewContents {
explicit inline ArrayBufferViewContents(v8::Local<v8::Object> value);
explicit inline ArrayBufferViewContents(v8::Local<v8::ArrayBufferView> abv);
inline void Read(v8::Local<v8::ArrayBufferView> abv);
inline void ReadValue(v8::Local<v8::Value> buf);

inline const T* data() const { return data_; }
inline size_t length() const { return length_; }
Expand Down

0 comments on commit 26fbc98

Please sign in to comment.