-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
stream: improve performance of finished() #59873
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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #59873 +/- ##
==========================================
+ Coverage 88.23% 88.25% +0.01%
==========================================
Files 703 703
Lines 207393 207401 +8
Branches 39888 39885 -3
==========================================
+ Hits 183003 183043 +40
+ Misses 16346 16315 -31
+ Partials 8044 8043 -1
🚀 New features to boost your workflow:
|
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.
Hey, thanks for the PR, I added a few comments about some issues I found running locally.
Also, would you mind split this change in two commits, the first one being the benchmark and the second one the optimization?
You can also improve the commit message a little bit by adding what you really changed to improve performance, eg: stream: preserve AsyncLocalStorage on finished only when needed
About the benchmark name, maybe changing to finished
should be more relevant than end-of-stream
since we are testing the finished
function.
Preview of the results:
h4ad:node-copy-4/ (improve-stream-finished-perf✗) $ node-benchmark-compare streams.csv [10:49:03]
confidence improvement accuracy (*) (**) (***)
streams/compose.js n=1000 *** 42.25 % ±3.90% ±5.24% ±6.91%
streams/end-of-stream.js streamType='readable' n=100000 *** 76.67 % ±1.41% ±1.89% ±2.48%
streams/end-of-stream.js streamType='writable' n=100000 *** 105.00 % ±2.01% ±2.69% ±3.56%
streams/pipe-object-mode.js n=5000000 -0.30 % ±0.81% ±1.08% ±1.42%
streams/pipe.js n=5000000 0.14 % ±1.77% ±2.36% ±3.08%
Be aware that when doing many comparisons the risk of a false-positive result increases.
In this case, there are 5 comparisons, you can thus expect the following amount of false-positive results:
0.25 false positives, when considering a 5% risk acceptance (*, **, ***),
0.05 false positives, when considering a 1% risk acceptance (**, ***),
0.01 false positives, when considering a 0.1% risk acceptance (***)
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.
While I love the PR, but I fear that this breaks continuation when using async_hooks.
We could argue that with ALS not using async_hooks we could remove them (or add them via a compile time flag) but that's outside of the scope of this PR.
56bc9ed
to
2d4a3b5
Compare
Thanks @mcollina! I removed the async_hooks part. |
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.
thanks
There is command line option I think we might need special handing for this variation. |
@avcribl you didn't address my comment. |
2d4a3b5
to
e7d45d6
Compare
@mcollina I've pushed a new commit that adds a check to determine whether async_context_frame is enabled. I tested both scenarios: In both cases, the unit tests ran successfully and passed:
Also, I ran the benchmark test with and without the flag and both have similar improved performance:
Please let me know if it addresses your comment. |
I don't see any new tests in this PR, just a benchmark. @Qard or @Flarna could chime in as well, but checking if AsyncContextFrame is enabled is not enough in case plain async_hooks are used. There are still a lot of modules out there that are not using AsyncLocalStorage for context tracking. My understanding is that using |
It's quite hard to tell. I would recommend to write tests for all these scenarios to be improved here and also verify that non improvable scenarios still work. FWIW I'm not a big fan of tinkering with async hooks/async local store internals at several places in code. |
if ((AsyncContextFrame.enabled && AsyncContextFrame.current()) || | ||
(!AsyncContextFrame.enabled && getHookArrays()[0].length > 0)) { |
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.
This could be simplified to:
if ((AsyncContextFrame.enabled && AsyncContextFrame.current()) || | |
(!AsyncContextFrame.enabled && getHookArrays()[0].length > 0)) { | |
if (AsyncContextFrame.current() || enabledHooksExist()) { |
The AsyncContextFrame.current()
call is a no-op when inactive, you don't really need the enabled check as that will just add more instructions. Also, as @Flarna pointed out, enabledHooksExist()
is the clearer way to check if there are active async_hooks listeners.
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.
Thanks! I removed AsyncContextFrame.enabled
One thing worth noting is that this would be a breaking change as currently the AsyncLocalStorage.bind(...) will always bind, even if there are no hooks or stores in use, but async_hooks could start listening at any time. In the current model you could start listening after the bind call there but before the callback is called and you would see the events. After this change you would not. |
e7d45d6
to
51e0905
Compare
Thanks all for the feedback! |
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
finished(readable, common.mustCall(() => { | ||
strictEqual(internalAsyncHooks.getHookArrays()[0].length > 0, | ||
true, 'Should have active user async hook'); | ||
})); |
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.
Note that this does not verify continuation is preserved
finished(readable, common.mustCall(() => { | ||
strictEqual(internalAsyncHooks.getHookArrays()[0].length > 0, | ||
true, 'Should have active user async hook'); | ||
})); |
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.
Note that this does not verify continuation is preserved
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.
This doesn't verify the code path either. It is a copy of the condition and someone might change one of the locations.
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.
Thanks! I expanded on the test!
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.
@mcollina I addressed your comment, thanks!
ff9809b
to
fd7f7a4
Compare
Marking as server-major just to make sure we don't accidentally land this in a minor with behaviour breakage. See: #59873 (comment)
|
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
cc @Flarna |
@Qard are you still sure this is a major? |
Well, I suppose async_hooks is technically still marked experimental, so we can consider it non-breaking. It definitely does break async_hooks slightly though as this will change the model to not trigger later events if the init was missed. With AsyncLocalStorage not using it anymore, I don't really care so much for that use case, but others still using async_hooks might care. Though, in my opinion, other events triggering despite earlier ones being missed has been as much a footgun for people, so... 🤷🏻 |
@mcollina Is there any update for my PR? Thanks! |
I restated the tests. Hoping that they will be green. |
Thanks @lemire! Some of the tests are flaky, would you please trigger the tests again? Thanks! |
The changes optimize the
end-of-stream.js
module to only preserve AsyncLocalStorage context when it's actually needed, improving performance by avoiding ALS overhead in the common case where no async context tracking is active.The performance test results with this patch:
The same perf test results with the current nodejs:
You can compare and see the noticeable performance improvement with this patch.