1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

perf(encoding): avoid copying the input data in TextDecoder (#12573)

The implementation of `TextDecoder` had a bug where it was copying the
input data in every case. This change removes that copy in
non-`SharedArrayBuffer` cases.

Since passing a shared buffer source to Rust would fail, this copy of
the input data was making `TextDecoder` work in cases where the input
is shared. In order to avoid a breaking change, the copy is retained in
those cases.
This commit is contained in:
Andreu Botella 2021-10-28 13:32:58 -07:00 committed by GitHub
parent 117d9d2087
commit 507ab50e0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -114,7 +114,14 @@
} else {
input = new Uint8Array(input);
}
return core.opSync("op_encoding_decode", new Uint8Array(input), {
if (input.buffer instanceof SharedArrayBuffer) {
// We clone the data into a non-shared ArrayBuffer so we can pass it
// to Rust.
// `input` is now a Uint8Array, and calling the TypedArray constructor
// with a TypedArray argument copies the data.
input = new Uint8Array(input);
}
return core.opSync("op_encoding_decode", input, {
rid: this.#rid,
stream: options.stream,
});