-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
process: improve queueMicrotask performance #28093
Conversation
Optimize the hot code paths of queueMicrotask by not creating unnecessary objects, not looking up properties on frozen primordials, etc.
This comment has been minimized.
This comment has been minimized.
lib/internal/process/task_queues.js
Outdated
@@ -172,7 +171,7 @@ function queueMicrotask(callback) { | |||
const asyncResource = createMicrotaskResource(); | |||
asyncResource.callback = callback; | |||
|
|||
enqueueMicrotask(FunctionPrototype.bind(runMicrotask, asyncResource)); | |||
enqueueMicrotask(FunctionPrototypeBind(runMicrotask, asyncResource)); |
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.
Same question here.
Are we actually doing something wrong with the primordials so that V8 doesn't know that the properties never change?
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.
Not sure. @mcollina flagged this for me and I've noticed it's definitely slower. He might have more insights — not sure if he talked to the V8 team about it at all.
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 is an ongoing effort to optimize frozen object performance, see https://bugs.chromium.org/p/v8/issues/detail?id=6831 and https://bugs.chromium.org/p/v8/issues/detail?id=8538 we may revisit this when the patches upstream land here.
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.
Are we actually doing something wrong with the primordials so that V8 doesn't know that the properties never change?
We are. Essentially accessing them is slower than accessing normal objects. We should look on not reading properties in hot code path, but maybe only once when the module is loaded.
Maybe @bmeurer would be interested in taking a look at the use cases and perf impact here. |
@@ -151,15 +152,14 @@ class AsyncResource { | |||
this[async_id_symbol] = asyncId; | |||
this[trigger_async_id_symbol] = triggerAsyncId; | |||
|
|||
// This prop name (destroyed) has to be synchronized with C++ | |||
const destroyed = { destroyed: false }; | |||
this[destroyedSymbol] = destroyed; |
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 moving this safe?
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.
Yeah. This is only needed for the case where requireManualDestroy = false
. We pass it on directly to C++ in registerDestroyHook
.
@apapirovski Can you please reintroduce the fix for the frozen primordials? I don't understand the reasoning behind removing it. You might want to re-run the benchmarks before landing. |
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
Then ok. If it does not affect benchmark I’m good! |
Landed in cde3928 |
Optimize the hot code paths of queueMicrotask by not creating unnecessary objects, not looking up properties on frozen primordials, etc. PR-URL: nodejs#28093 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Optimize the hot code paths of queueMicrotask by not creating unnecessary objects, not looking up properties on frozen primordials, etc. PR-URL: #28093 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Optimize the hot code paths of queueMicrotask by not creating unnecessary objects, not looking up properties on frozen primordials, etc.
Benchmark: https://ci.nodejs.org/job/benchmark-node-micro-benchmarks/389/
That said, this is what I get locally so I figure the system makes a difference:
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes