Repository navigation
src: skip the async id stack when nobody sees it - #66511
Open
nigrosimone wants to merge 2 commits into
Open
nigrosimone wants to merge 2 commits into
nigrosimone wants to merge 2 commits into
Conversation
With no async hook and no executionAsyncResource() user, every InternalCallbackScope pushed and popped the async ids on the stack, only for executionAsyncId() and triggerAsyncId() to read the top. Swap the two ids in place instead: they are all that those two and process.nextTick() read. executionAsyncResource() is the only reader of the stack: the scopes that skip it form a chain in AsyncHooks, counted in a new field, kLazyScopes, and JS asks C++ for the innermost one only while that count is not zero. The checks for a corrupted stack stay. Refs: nodejs/performance#24 Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
nigrosimone
force-pushed
the
lazy-async-ids
branch
from
October 21, 2026 00:58
8344ac3 to
5bd5449
Compare
nigrosimone
marked this pull request as ready for review
October 21, 2026 03:22
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #66511 +/- ##
==========================================
- Coverage 92.86% 90.57% -2.29%
==========================================
Files 428 797 +369
Lines 195100 278432 +83332
Branches 30147 53510 +23363
==========================================
+ Hits 181175 252200 +71025
- Misses 13597 16582 +2985
- Partials 328 9650 +9322
🚀 New features to boost your workflow:
|
executionAsyncResource() asked first inside a native callback, where the scope skipped the id stack and holds its resource as a Global, and with a context pushed on the stack above that scope. Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
Contributor
Author
|
About the Codecov report: 21a6ea2 adds a test for the two gaps that can be covered, the lazy resource held as a Global (an fs callback) and a context pushed on the stack above a lazy scope. The lines still reported, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With no async hook and no
executionAsyncResource()user, every InternalCallbackScope pushes and pops the async ids on the stack, only forexecutionAsyncId()andtriggerAsyncId()to read the top. This swaps the two ids in place instead: they are all that those two andprocess.nextTick()read, so the readers pay nothing.executionAsyncResource()is the only reader of the stack. The scopes that skip it form a chain in AsyncHooks, counted in a new fieldkLazyScopes, and JS asks C++ for the innermost one only while that count is not zero. The checks for a corrupted stack stay: test/async-hookstest-improper-orderandtest-emit-*-on-destroyedfail without them, because the main script runs in a scope that starts before any hook.compare.js on
napi/make_callback, 30 runs, Linux x64, one core, on main (3e5c8af, with #66395):MakeCallback goes from 128.0 to 119.3 ns at n=1e7. test/async-hooks, the async hooks, AsyncLocalStorage, AsyncResource and nextTick tests in test/parallel, and the new addon test pass.
The new field and the chain are the part to look at first, the design was discussed in nodejs/performance#24.
Disclosure: I used Opus 5.5 (Max) as coding assistant