diff options
author | Yagiz Nizipli <yagiz@nizipli.com> | 2022-11-07 17:07:34 +0300 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2022-11-09 17:17:31 +0300 |
commit | aed55df74b448438a9b786f0a5d52ad629974472 (patch) | |
tree | cebcc12943dbd5d15e1619c0e85f90c5c466bda1 | |
parent | 86a5b71dc98bcd2e2eff59a9cdd64243b467136d (diff) |
util: improve text-decoder performance
PR-URL: https://github.com/nodejs/node/pull/45363
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
-rw-r--r-- | lib/internal/encoding.js | 10 | ||||
-rw-r--r-- | src/util-inl.h | 25 | ||||
-rw-r--r-- | src/util.h | 1 |
3 files changed, 25 insertions, 11 deletions
diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 2ab85d9d9ac..40c87a0a871 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -411,15 +411,7 @@ function makeTextDecoderICU() { decode(input = empty, options = kEmptyObject) { validateDecoder(this); - if (isAnyArrayBuffer(input)) { - try { - input = lazyBuffer().from(input); - } catch { - // If the buffer is detached, - // use an empty Uint8Array to avoid TypeError - input = empty; - } - } else if (!isArrayBufferView(input)) { + if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) { throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'ArrayBufferView'], input); diff --git a/src/util-inl.h b/src/util-inl.h index bdf9732e853..18f829d2775 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -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> @@ -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; diff --git a/src/util.h b/src/util.h index 7a41eae4aa7..6094f07f201 100644 --- a/src/util.h +++ b/src/util.h @@ -506,6 +506,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_; } |