From e58f77e431c7f9404bd76f6b7cf5b6d7841c0b0f Mon Sep 17 00:00:00 2001 From: randomicon00 <20146907+randomicon00@users.noreply.github.com> Date: Tue, 17 May 2022 14:52:48 +0100 Subject: perf(ext/web): Add fast path for non-streaming TextDecoder (#14217) Co-authored-by: Divy Srivastava --- ext/web/08_text_encoding.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'ext/web/08_text_encoding.js') diff --git a/ext/web/08_text_encoding.js b/ext/web/08_text_encoding.js index 4f26089b1..86ac5ab3c 100644 --- a/ext/web/08_text_encoding.js +++ b/ext/web/08_text_encoding.js @@ -95,16 +95,6 @@ context: "Argument 2", }); - // TODO(lucacasonato): add fast path for non-streaming decoder & decode - - if (this.#rid === null) { - this.#rid = core.opSync("op_encoding_new_decoder", { - label: this.#encoding, - fatal: this.#fatal, - ignoreBom: this.#ignoreBOM, - }); - } - try { try { if (ArrayBufferIsView(input)) { @@ -132,12 +122,28 @@ // with a TypedArray argument copies the data. input = new Uint8Array(input); } + + if (!options.stream && this.#rid === null) { + return core.opSync("op_encoding_decode_single", input, { + label: this.#encoding, + fatal: this.#fatal, + ignoreBom: this.#ignoreBOM, + }); + } + + if (this.#rid === null) { + this.#rid = core.opSync("op_encoding_new_decoder", { + label: this.#encoding, + fatal: this.#fatal, + ignoreBom: this.#ignoreBOM, + }); + } return core.opSync("op_encoding_decode", input, { rid: this.#rid, stream: options.stream, }); } finally { - if (!options.stream) { + if (!options.stream && this.#rid) { core.close(this.#rid); this.#rid = null; } -- cgit v1.2.3