Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: improve text-decoder performance #45363

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions benchmark/util/text-decoder.js
Expand Up @@ -6,14 +6,32 @@ const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'latin1', 'iso-8859-3'],
ignoreBOM: [0, 1],
len: [256, 1024 * 16, 1024 * 512],
n: [1e6]
n: [1e2],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer']
});

function main({ encoding, len, n, ignoreBOM }) {
const buf = Buffer.allocUnsafe(len);
function main({ encoding, len, n, ignoreBOM, type }) {
const decoder = new TextDecoder(encoding, { ignoreBOM });
let buf;

switch (type) {
case 'SharedArrayBuffer': {
buf = new SharedArrayBuffer(len);
break;
}
case 'ArrayBuffer': {
buf = new ArrayBuffer(len);
break;
}
case 'Buffer': {
buf = Buffer.allocUnsafe(len);
break;
}
}

bench.start();
decoder.decode(buf);
for (let i = 0; i < n; i++) {
decoder.decode(buf);
}
bench.end(n);
}
10 changes: 1 addition & 9 deletions lib/internal/encoding.js
Expand Up @@ -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;
addaleax marked this conversation as resolved.
Show resolved Hide resolved
}
} 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
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());
}
anonrig marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Expand Up @@ -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_; }
Expand Down