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(ext/fetch): use new instead of createBranded #20624
Conversation
ext/fetch/20_headers.js
Outdated
@@ -286,6 +286,11 @@ class Headers { | |||
|
|||
/** @param {HeadersInit} [init] */ | |||
constructor(init = undefined) { | |||
if (init === webidl.brand) { | |||
this[webidl.brand] = webidl.brand; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this[init] = init
faster? It should have two less property lookups
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, missed that.
ext/fetch/23_request.js
Outdated
const request = new Request(webidl.brand); | ||
request[_request] = inner; | ||
request[_signal] = signal; | ||
request[_getHeaders] = () => headersFromHeaderList(inner.headerList, guard); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the way v8 optimizes classes, it might be faster to pass all of these into the constructor after the brand, so that the object always has the same shape after the constructor returns.
new Request(webidl.brand, inner, signal, () => ...);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll run some benchmarks
f5fdcf8
to
028fd52
Compare
@@ -40,6 +40,7 @@ const _headerList = Symbol("header list"); | |||
const _iterableHeaders = Symbol("iterable headers"); | |||
const _iterableHeadersCache = Symbol("iterable headers cache"); | |||
const _guard = Symbol("guard"); | |||
const _brand = webidl.brand; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! We can do a follow-up if passing constructor args helps as well
This PR optimizes
fromInner*
methods ofRequest
/Header
/Response
used byDeno.serve
andfetch
by usingnew
instead ofObjectCreate
fromcreateBranded
.The "brand" is created by passing
webidl.brand
to the constructor instead.deno/ext/webidl/00_webidl.js
Lines 1001 to 1005 in 142449e
Benchmark