From 54fa2b2138570a2db385fcc220f17fea818d6be0 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:45:51 +0900 Subject: [PATCH 1/2] perf(fetch): improve body mixin methods --- benchmarks/fetch/body-arraybuffer.mjs | 24 ++++++++++++++++++++++++ lib/web/fetch/body.js | 5 +++-- lib/web/fetch/util.js | 7 +++++-- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 benchmarks/fetch/body-arraybuffer.mjs diff --git a/benchmarks/fetch/body-arraybuffer.mjs b/benchmarks/fetch/body-arraybuffer.mjs new file mode 100644 index 00000000000..f05e62dc98b --- /dev/null +++ b/benchmarks/fetch/body-arraybuffer.mjs @@ -0,0 +1,24 @@ +import { group, bench, run } from 'mitata' +import { Response } from '../../lib/web/fetch/response.js' + +const settings = { + small: 2 << 8, + middle: 2 << 12, + long: 2 << 16 +} + +for (const [name, length] of Object.entries(settings)) { + const buffer = Buffer.allocUnsafe(length).map(() => (Math.random() * 100) | 0) + group(`${name} (length ${length})`, () => { + bench('Response#arrayBuffer', async () => { + return await new Response(buffer).arrayBuffer() + }) + + // for comparison + bench('Response#text', async () => { + return await new Response(buffer).text() + }) + }) +} + +await run() diff --git a/lib/web/fetch/body.js b/lib/web/fetch/body.js index 26cce5f3e0c..eb77b8733c4 100644 --- a/lib/web/fetch/body.js +++ b/lib/web/fetch/body.js @@ -318,7 +318,8 @@ function bodyMixinMethods (instance) { // given a byte sequence bytes: return a new ArrayBuffer // whose contents are bytes. return consumeBody(this, (bytes) => { - return new Uint8Array(bytes).buffer + // Note: arrayBuffer already cloned. + return bytes.buffer }, instance) }, @@ -432,7 +433,7 @@ async function consumeBody (object, convertBytesToJSValue, instance) { // 5. If object’s body is null, then run successSteps with an // empty byte sequence. if (object[kState].body == null) { - successSteps(new Uint8Array()) + successSteps(Buffer.allocUnsafe(0)) return promise.promise } diff --git a/lib/web/fetch/util.js b/lib/web/fetch/util.js index 8c73f909604..7bccc7d4993 100644 --- a/lib/web/fetch/util.js +++ b/lib/web/fetch/util.js @@ -1069,8 +1069,7 @@ async function fullyReadBody (body, processBody, processBodyError) { // 5. Read all bytes from reader, given successSteps and errorSteps. try { - const result = await readAllBytes(reader) - successSteps(result) + successSteps(await readAllBytes(reader)) } catch (e) { errorSteps(e) } @@ -1128,6 +1127,10 @@ async function readAllBytes (reader) { if (done) { // 1. Call successSteps with bytes. + if (bytes.length === 1) { + const { buffer, byteOffset, byteLength } = bytes[0] + return Buffer.from(buffer.slice(byteOffset, byteOffset + byteLength), 0, byteLength) + } return Buffer.concat(bytes, byteLength) } From ac0787f2d6abb31a3fe8999cc9c89bc0d6da55a2 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sun, 14 Apr 2024 19:55:31 +0900 Subject: [PATCH 2/2] perf: avoid unnecessary clone --- lib/web/fetch/body.js | 15 ++++++++------- lib/web/fetch/util.js | 10 +++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/web/fetch/body.js b/lib/web/fetch/body.js index eb77b8733c4..e6af47c9a45 100644 --- a/lib/web/fetch/body.js +++ b/lib/web/fetch/body.js @@ -309,7 +309,7 @@ function bodyMixinMethods (instance) { // Return a Blob whose contents are bytes and type attribute // is mimeType. return new Blob([bytes], { type: mimeType }) - }, instance) + }, instance, false) }, arrayBuffer () { @@ -320,19 +320,19 @@ function bodyMixinMethods (instance) { return consumeBody(this, (bytes) => { // Note: arrayBuffer already cloned. return bytes.buffer - }, instance) + }, instance, true) }, text () { // The text() method steps are to return the result of running // consume body with this and UTF-8 decode. - return consumeBody(this, utf8DecodeBytes, instance) + return consumeBody(this, utf8DecodeBytes, instance, false) }, json () { // The json() method steps are to return the result of running // consume body with this and parse JSON from bytes. - return consumeBody(this, parseJSONFromBytes, instance) + return consumeBody(this, parseJSONFromBytes, instance, false) }, formData () { @@ -384,7 +384,7 @@ function bodyMixinMethods (instance) { throw new TypeError( 'Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".' ) - }, instance) + }, instance, false) } } @@ -400,8 +400,9 @@ function mixinBody (prototype) { * @param {Response|Request} object * @param {(value: unknown) => unknown} convertBytesToJSValue * @param {Response|Request} instance + * @param {boolean} [shouldClone] */ -async function consumeBody (object, convertBytesToJSValue, instance) { +async function consumeBody (object, convertBytesToJSValue, instance, shouldClone) { webidl.brandCheck(object, instance) // 1. If object is unusable, then return a promise rejected @@ -439,7 +440,7 @@ async function consumeBody (object, convertBytesToJSValue, instance) { // 6. Otherwise, fully read object’s body given successSteps, // errorSteps, and object’s relevant global object. - await fullyReadBody(object[kState].body, successSteps, errorSteps) + await fullyReadBody(object[kState].body, successSteps, errorSteps, shouldClone) // 7. Return promise. return promise.promise diff --git a/lib/web/fetch/util.js b/lib/web/fetch/util.js index 7bccc7d4993..da55a7c4dfc 100644 --- a/lib/web/fetch/util.js +++ b/lib/web/fetch/util.js @@ -1043,7 +1043,7 @@ function iteratorMixin (name, object, kInternalIterator, keyIndex = 0, valueInde /** * @see https://fetch.spec.whatwg.org/#body-fully-read */ -async function fullyReadBody (body, processBody, processBodyError) { +async function fullyReadBody (body, processBody, processBodyError, shouldClone) { // 1. If taskDestination is null, then set taskDestination to // the result of starting a new parallel queue. @@ -1069,7 +1069,7 @@ async function fullyReadBody (body, processBody, processBodyError) { // 5. Read all bytes from reader, given successSteps and errorSteps. try { - successSteps(await readAllBytes(reader)) + successSteps(await readAllBytes(reader, shouldClone)) } catch (e) { errorSteps(e) } @@ -1117,8 +1117,9 @@ function isomorphicEncode (input) { * @see https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes * @see https://streams.spec.whatwg.org/#read-loop * @param {ReadableStreamDefaultReader} reader + * @param {boolean} [shouldClone] */ -async function readAllBytes (reader) { +async function readAllBytes (reader, shouldClone) { const bytes = [] let byteLength = 0 @@ -1129,6 +1130,9 @@ async function readAllBytes (reader) { // 1. Call successSteps with bytes. if (bytes.length === 1) { const { buffer, byteOffset, byteLength } = bytes[0] + if (shouldClone === false) { + return Buffer.from(buffer, byteOffset, byteLength) + } return Buffer.from(buffer.slice(byteOffset, byteOffset + byteLength), 0, byteLength) } return Buffer.concat(bytes, byteLength)