summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBartek Iwańczuk <biwanczuk@gmail.com>2020-03-19 21:31:56 +0100
committerGitHub <noreply@github.com>2020-03-19 21:31:56 +0100
commit87d2ba42bf0dedcd91059145bf8ab5941236354b (patch)
tree52ad0906f8b41586cf9fb57ce02646100725b0c6 /core
parent392d2c11182332b8d3c168169b1585e3419cb1eb (diff)
perf: Optimize TextEncoder and TextDecoder (#4430)
* add tests for "Deno.core.encode" and "Deno.core.decode" for empty inputs * use "Deno.core.encode" in "TextEncoder" * use "Deno.core.decode" in "TextDecoder" * remove "core_decode" and "core_encode" benchmarks
Diffstat (limited to 'core')
-rw-r--r--core/bindings.rs15
-rw-r--r--core/encode_decode_test.js6
2 files changed, 20 insertions, 1 deletions
diff --git a/core/bindings.rs b/core/bindings.rs
index 3745abf69..88bdf7f30 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -632,7 +632,20 @@ fn encode(
};
let text_str = text.to_rust_string_lossy(scope);
let text_bytes = text_str.as_bytes().to_vec().into_boxed_slice();
- let buf = boxed_slice_to_uint8array(scope, text_bytes);
+
+ let buf = if text_bytes.is_empty() {
+ let ab = v8::ArrayBuffer::new(scope, 0);
+ v8::Uint8Array::new(ab, 0, 0).expect("Failed to create UintArray8")
+ } else {
+ let buf_len = text_bytes.len();
+ let backing_store =
+ v8::ArrayBuffer::new_backing_store_from_boxed_slice(text_bytes);
+ let mut backing_store_shared = backing_store.make_shared();
+ let ab =
+ v8::ArrayBuffer::with_backing_store(scope, &mut backing_store_shared);
+ v8::Uint8Array::new(ab, 0, buf_len).expect("Failed to create UintArray8")
+ };
+
rv.set(buf.into())
}
diff --git a/core/encode_decode_test.js b/core/encode_decode_test.js
index 8a366dd66..294144593 100644
--- a/core/encode_decode_test.js
+++ b/core/encode_decode_test.js
@@ -27,12 +27,18 @@ function main() {
108, 100
];
+ const empty = Deno.core.encode("");
+ if (empty.length !== 0) throw new Error("assert");
+
assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1);
assertArrayEquals(
Array.from(Deno.core.encode("Hello \udc12\ud834 World")),
fixture2
);
+ const emptyBuf = Deno.core.decode(new Uint8Array(0));
+ if (emptyBuf !== "") throw new Error("assert");
+
assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello �� World");
}