Repository navigation
http: keep request option copies in fast mode - #67395
Conversation
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67395 +/- ##
========================================
Coverage 90.66% 90.67%
========================================
Files 799 800 +1
Lines 280245 280423 +178
Branches 54121 54136 +15
========================================
+ Hits 254097 254269 +172
- Misses 16277 16286 +9
+ Partials 9871 9868 -3
🚀 New features to boost your workflow:
|
pimterry
left a comment
There was a problem hiding this comment.
This is neat, it's a good spot of a case we can speed up a bit.
That said, I pointed the new Fable at it to test this and explore a bit, and I think there's an even faster option 😄: { ...options, __proto__: null } (i.e. switching the order so we spread first, and __proto__ comes last).
This creates the object with the fast path clone for spread (here) but directly sets the prototype up front, so it skips the subsequent method call and prototype change of ObjectSetPrototypeOf(...), and still ends up in fast mode.
Do you want to take a look? Or if not I agree this is good regardless, we could merge now and test further improvements later.
|
Wow, that looks great, yeah let's do that instead. I'll test and push the change |
| EventEmitter.call(this); | ||
|
|
||
| this.options = { __proto__: null, ...options }; | ||
| this.options = ObjectSetPrototypeOf({ ...options }, null); |
There was a problem hiding this comment.
The ObjectSetPrototypeOf call also has overhead. We can achieve the same fast-properties result with something like...
const kNullProtoObject = { __proto__: null };
this.options = { __proto__: kNullProtoObject, ...options };Should come out a bit faster.
There was a problem hiding this comment.
hey James, thanks for the review
what do you think about this?
it's 3-4% slower than your suggestion, but it still doesn't use ObjectSetPrototypeOf while keeping fast properties and the same behavior in all cases- i'm kind of surprised it works to be honest 😂
24cb5f2 to
98f32da
Compare
| EventEmitter.call(this); | ||
|
|
||
| this.options = { __proto__: null, ...options }; | ||
| this.options = { ...options, __proto__: null }; |
There was a problem hiding this comment.
The { __proto__: kNullProtoObject, ...options } approach is better, I believe.
There was a problem hiding this comment.
Sure, saw your other PR, let’s switch to this
There was a problem hiding this comment.
@jasnell I'm guessing { __proto__: kNullProtoObject, ...options } wouldn't be semver major here?
pimterry
left a comment
There was a problem hiding this comment.
LGTM.
No strong feelings about kNullProtoObject or this, either one is fine by me, they seem to perform very similarly AFAICT.
The options of every client request are copied several times: once
in ClientRequest to merge the URL and the options, once more to
build the options handed to the agent, and again in Agent#addRequest
(on every request, including keep-alive reuse) and createSocket to
merge in the agent options. Each copy was a `{ __proto__: null, ...}`
literal or an Object.assign() onto `{ __proto__: null }`. V8 creates
such an object in dictionary mode, and copying from a dictionary-mode
source takes the generic slow path, so every copy in the chain was
slow and fed a slow source into the next one.
Use kEmptyObject, a frozen object with a null prototype and no
properties, as the prototype of each copy instead of null. The copy
still inherits nothing, has the same own properties in the same
order, and stays in fast mode. The same is done for the copy of the
options in the http and https Agent constructors, which is the
source of the agent options merged into every request.
benchmark/http/create-clientrequest.js improves by 2 to 24 % at the
default e=1 (50 runs) and by 49 to 87 % with e=6 (30 runs).
Assisted-by: claude:opus-5.5
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
98f32da to
984302a
Compare
|
Landed in 369b9c8 |
The options of every client request are copied several times: once
in ClientRequest to merge the URL and the options, once more to
build the options handed to the agent, and again in Agent#addRequest
(on every request, including keep-alive reuse) and createSocket to
merge in the agent options. Each copy was a `{ __proto__: null, ...}`
literal or an Object.assign() onto `{ __proto__: null }`. V8 creates
such an object in dictionary mode, and copying from a dictionary-mode
source takes the generic slow path, so every copy in the chain was
slow and fed a slow source into the next one.
Use kEmptyObject, a frozen object with a null prototype and no
properties, as the prototype of each copy instead of null. The copy
still inherits nothing, has the same own properties in the same
order, and stays in fast mode. The same is done for the copy of the
options in the http and https Agent constructors, which is the
source of the agent options merged into every request.
benchmark/http/create-clientrequest.js improves by 2 to 24 % at the
default e=1 (50 runs) and by 49 to 87 % with e=6 (30 runs).
Assisted-by: claude:opus-5.5
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: #67395
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Replaces the
{ __proto__: null, ...options }copies on the client request path and thehttp/httpsAgentconstructors with{ ...options, __proto__: null }so they stay out of dictionary modebenchmark/http/create-clientrequest.js(50 runs at the defaulte=1, 30 runs with--set e=6):e=1e=6Server benchmarks driven by
benchmarker=test-double-http(s), where theclient half is
http.request(30 runs,c=50 chunks=1 len=4 type=buffer):http/simple.jshttps/simple.js