lib: optimize async context frame activation - #65519
Open
pimterry wants to merge 1 commit into
Open
Conversation
Signed-off-by: Tim Perry <pimterry@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65519 +/- ##
==========================================
+ Coverage 90.14% 90.16% +0.01%
==========================================
Files 751 751
Lines 253585 253579 -6
Branches 47772 47786 +14
==========================================
+ Hits 228596 228637 +41
+ Misses 16228 16200 -28
+ Partials 8761 8742 -19
🚀 New features to boost your workflow:
|
lpinca
approved these changes
Aug 24, 2026
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.
Async context frame is activated lazily. The current implementation did this by waiting for the first call to
checkEnabled, and then changing the prototype ofAsyncContextFrametoActiveAsyncContextFrameif enabled. This overrides thecurrent,set,exchangeanddisablemethods to the active versions.Swapping the prototype like this makes the replaced methods slow. My best understanding is that V8 caches the lookup for the methods on their prototype when they're used initially, and when the prototype is changed (lazily later, via various triggers but notably including socket timeout setup for any server) those caches are lost, and never recover, making every use of those methods about 8x slower. This relates to AsyncLocalStorage, but it doesn't require actually using it - it applies to basically everybody.
That's bad because we call these methods a lot: every tick, immediate & timer. Assigning the methods instead of modifying the prototype avoids this slowdown.
I found this exploring HTTP/1 performance: this tiny change immediately boosts HTTP perf for the
http/simplebenchmark by about 5%, with no user-visible changes, by dropping overhead on all the ticks involved. I'll do a benchmark run to confirm impact in a minute.It should also improve performance in plenty of other places too though, really any code heavily using timers or nextTick: as an extreme case, I see pure timer benchmarks like
timers-timeout-nexttickjump up to 70% with this change. Micro-optimization, but in very hot & widely used code.No downside AFAICT. Actual change is very simple (replace
ObjectSetPrototypeOfwith 4x method assignments) but I've simplified the surrounding code en route (dropped theActiveAsyncContextFrameclass completely, since it's now just loose methods) and movedcheckEnabledto the end of the file, which means we can drop the eslint-disable for use-before-define.