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

perf: Optimize TextEncoder and TextDecoder #4430

Merged
merged 4 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions cli/js/web/encode_utf8.ts

This file was deleted.

16 changes: 12 additions & 4 deletions cli/js/web/text_encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import * as base64 from "./base64.ts";
import { decodeUtf8 } from "./decode_utf8.ts";
import * as domTypes from "./dom_types.ts";
import { encodeUtf8 } from "./encode_utf8.ts";
import { core } from "../core.ts";

const CONTINUE = null;
const END_OF_STREAM = -1;
Expand Down Expand Up @@ -352,6 +352,15 @@ export class TextDecoder {
bytes = new Uint8Array(0);
}

// For simple utf-8 decoding "Deno.core.decode" can be used for performance
if (
this._encoding === "utf-8" &&
this.fatal === false &&
this.ignoreBOM === false
) {
return core.decode(bytes);
}

// For performance reasons we utilise a highly optimised decoder instead of
// the general decoder.
if (this._encoding === "utf-8") {
Expand Down Expand Up @@ -396,10 +405,9 @@ interface TextEncoderEncodeIntoResult {
export class TextEncoder {
readonly encoding = "utf-8";
encode(input = ""): Uint8Array {
// For performance reasons we utilise a highly optimised decoder instead of
// the general decoder.
// Deno.core.encode() provides very efficient utf-8 encoding
if (this.encoding === "utf-8") {
return encodeUtf8(input);
return core.encode(input);
}

const encoder = new UTF8Encoder();
Expand Down
15 changes: 14 additions & 1 deletion core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
6 changes: 6 additions & 0 deletions core/encode_decode_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down