1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

perf(http): cache webidl.converters lookups in ext/fetch/23_response.js (#26256)

This commit is contained in:
David Sherret 2024-10-14 23:25:18 -04:00 committed by GitHub
parent 9f0a447f7c
commit 4c9eee3ebe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -61,6 +61,15 @@ const _mimeType = Symbol("mime type");
const _body = Symbol("body"); const _body = Symbol("body");
const _brand = webidl.brand; const _brand = webidl.brand;
// it's slightly faster to cache these
const webidlConvertersBodyInitDomString =
webidl.converters["BodyInit_DOMString?"];
const webidlConvertersUSVString = webidl.converters["USVString"];
const webidlConvertersUnsignedShort = webidl.converters["unsigned short"];
const webidlConvertersAny = webidl.converters["any"];
const webidlConvertersByteString = webidl.converters["ByteString"];
const webidlConvertersHeadersInit = webidl.converters["HeadersInit"];
/** /**
* @typedef InnerResponse * @typedef InnerResponse
* @property {"basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"} type * @property {"basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"} type
@ -259,8 +268,8 @@ class Response {
*/ */
static redirect(url, status = 302) { static redirect(url, status = 302) {
const prefix = "Failed to execute 'Response.redirect'"; const prefix = "Failed to execute 'Response.redirect'";
url = webidl.converters["USVString"](url, prefix, "Argument 1"); url = webidlConvertersUSVString(url, prefix, "Argument 1");
status = webidl.converters["unsigned short"](status, prefix, "Argument 2"); status = webidlConvertersUnsignedShort(status, prefix, "Argument 2");
const baseURL = getLocationHref(); const baseURL = getLocationHref();
const parsedURL = new URL(url, baseURL); const parsedURL = new URL(url, baseURL);
@ -286,8 +295,8 @@ class Response {
*/ */
static json(data = undefined, init = { __proto__: null }) { static json(data = undefined, init = { __proto__: null }) {
const prefix = "Failed to execute 'Response.json'"; const prefix = "Failed to execute 'Response.json'";
data = webidl.converters.any(data); data = webidlConvertersAny(data);
init = webidl.converters["ResponseInit_fast"](init, prefix, "Argument 2"); init = webidlConvertersResponseInitFast(init, prefix, "Argument 2");
const str = serializeJSValueToJSONString(data); const str = serializeJSValueToJSONString(data);
const res = extractBody(str); const res = extractBody(str);
@ -313,8 +322,8 @@ class Response {
} }
const prefix = "Failed to construct 'Response'"; const prefix = "Failed to construct 'Response'";
body = webidl.converters["BodyInit_DOMString?"](body, prefix, "Argument 1"); body = webidlConvertersBodyInitDomString(body, prefix, "Argument 1");
init = webidl.converters["ResponseInit_fast"](init, prefix, "Argument 2"); init = webidlConvertersResponseInitFast(init, prefix, "Argument 2");
this[_response] = newInnerResponse(); this[_response] = newInnerResponse();
this[_headers] = headersFromHeaderList( this[_headers] = headersFromHeaderList(
@ -443,47 +452,49 @@ webidl.converters["Response"] = webidl.createInterfaceConverter(
"Response", "Response",
ResponsePrototype, ResponsePrototype,
); );
webidl.converters["ResponseInit"] = webidl.createDictionaryConverter( const webidlConvertersResponseInit = webidl.converters["ResponseInit"] = webidl
"ResponseInit", .createDictionaryConverter(
[{ "ResponseInit",
key: "status", [{
defaultValue: 200, key: "status",
converter: webidl.converters["unsigned short"], defaultValue: 200,
}, { converter: webidlConvertersUnsignedShort,
key: "statusText", }, {
defaultValue: "", key: "statusText",
converter: webidl.converters["ByteString"], defaultValue: "",
}, { converter: webidlConvertersByteString,
key: "headers", }, {
converter: webidl.converters["HeadersInit"], key: "headers",
}], converter: webidlConvertersHeadersInit,
); }],
webidl.converters["ResponseInit_fast"] = function ( );
init, const webidlConvertersResponseInitFast = webidl
prefix, .converters["ResponseInit_fast"] = function (
context, init,
opts, prefix,
) { context,
if (init === undefined || init === null) { opts,
return { status: 200, statusText: "", headers: undefined }; ) {
} if (init === undefined || init === null) {
// Fast path, if not a proxy return { status: 200, statusText: "", headers: undefined };
if (typeof init === "object" && !core.isProxy(init)) { }
// Not a proxy fast path // Fast path, if not a proxy
const status = init.status !== undefined if (typeof init === "object" && !core.isProxy(init)) {
? webidl.converters["unsigned short"](init.status) // Not a proxy fast path
: 200; const status = init.status !== undefined
const statusText = init.statusText !== undefined ? webidlConvertersUnsignedShort(init.status)
? webidl.converters["ByteString"](init.statusText) : 200;
: ""; const statusText = init.statusText !== undefined
const headers = init.headers !== undefined ? webidlConvertersByteString(init.statusText)
? webidl.converters["HeadersInit"](init.headers) : "";
: undefined; const headers = init.headers !== undefined
return { status, statusText, headers }; ? webidlConvertersHeadersInit(init.headers)
} : undefined;
// Slow default path return { status, statusText, headers };
return webidl.converters["ResponseInit"](init, prefix, context, opts); }
}; // Slow default path
return webidlConvertersResponseInit(init, prefix, context, opts);
};
/** /**
* @param {Response} response * @param {Response} response