Skip to content

Commit

Permalink
perf: avoid unnecessary clone
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Apr 14, 2024
1 parent 54fa2b2 commit cfcbce8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
}
Expand Down Expand Up @@ -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]

Check failure on line 1120 in lib/web/fetch/util.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
*/
async function readAllBytes (reader) {
async function readAllBytes (reader, shouldClone) {
const bytes = []
let byteLength = 0

Expand All @@ -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)
Expand Down

0 comments on commit cfcbce8

Please sign in to comment.