summaryrefslogtreecommitdiff
path: root/ext/web/08_text_encoding.js
diff options
context:
space:
mode:
authorAndreu Botella <abb@randomunok.com>2021-10-28 13:32:58 -0700
committerGitHub <noreply@github.com>2021-10-29 07:32:58 +1100
commit507ab50e0f33f0b4264c68179055ad8a7dc60320 (patch)
tree19c3b2f23ddbe52e7e34ed23d0079b4edc8dc671 /ext/web/08_text_encoding.js
parent117d9d2087479b1d85a6f7eeba5b8971a4d0c6ff (diff)
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.
Diffstat (limited to 'ext/web/08_text_encoding.js')
-rw-r--r--ext/web/08_text_encoding.js9
1 files changed, 8 insertions, 1 deletions
diff --git a/ext/web/08_text_encoding.js b/ext/web/08_text_encoding.js
index 28f2848b8..d3f6d45fb 100644
--- a/ext/web/08_text_encoding.js
+++ b/ext/web/08_text_encoding.js
@@ -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,
});