GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MakeCallback is very slow · Issue #24 · nodejs/performance

MakeCallback is very slow #24

New issue
Copy link
New issue
Copy link
Open
Open
MakeCallback is very slow#24
Copy link
Labels
benchmark-neededAdd to issues that does not have a benchmarkhelp wantedExtra attention is needed

Description

@ronag
ronag
opened on Nov 20, 2022
Issue body actions

Calling into JS from CPP is very slow. Is this due to async hooks? Could we disable that with a global option if the user doesn't need async hooks?

Reacted by Nigro Simone

Activity

Next
  1. joyeecheung commented on Nov 21, 2022

    @joyeecheung
    joyeecheung
    on Nov 21, 2022
    Member
    More actions

    See past discussions nodejs/node#10101 - I think the conclusion was that it wasn't that slow, but that was measured 6 years ago and before V8 rolled out several architectural redesigns. We have also deprecated domains for a long time. Maybe the situations have changed now. Would be good to have some new measurements to see if it is worth a shot making it some kind of optional thing.

  2. anonrig commented on Nov 21, 2022

    @anonrig
    anonrig
    on Nov 21, 2022
    Member
    More actions

    Could we disable that with a global option if the user doesn't need async hooks?

    Can you explain this in detail? I'm trying to understand the current implementation, and a quick explanation could/would save a lot of time :)

  3. ronag commented on Nov 22, 2022

    @ronag
    ronag
    on Nov 22, 2022
    MemberAuthor
    More actions

    Could we disable that with a global option if the user doesn't need async hooks?

    Can you explain this in detail? I'm trying to understand the current implementation, and a quick explanation could/would save a lot of time :)

    I don't know much about the details. Async hooks are used to propagate async context. It has a constant overhead even if you don't use it.

  4. mcollina commented on Nov 22, 2022

    @mcollina
    mcollina
    on Nov 22, 2022
    SponsorMember
    More actions

    async_hooks have a tiny overhead if disabled and a significant overhead if enabled.

  5. ronag commented on Nov 22, 2022

    @ronag
    ronag
    on Nov 22, 2022
    MemberAuthor
    More actions

    async_hooks have a tiny overhead if disabled and a significant overhead if enabled.

    Is it possible to disable it?

  6. mcollina commented on Nov 22, 2022

    @mcollina
    mcollina
    on Nov 22, 2022
    SponsorMember
    More actions

    Not really. That's described in the linked issue. Maybe a compile-time flag could do it but we are talking ns of difference.

  7. abenhamdine commented on Dec 1, 2022

    @abenhamdine
    abenhamdine
    on Dec 1, 2022
    More actions

    see also previous related work in
    https://github.com/nodejs/node/pull/41331
    https://github.com/nodejs/node/pull/41279
    some big picture datapoints here, related to uWebSockets module : https://github.com/uNetworking/uWebSockets.js/discussions/341

  8. anonrig
    added
    performance-agenda
    on May 1, 2023
  9. benjamingr commented on May 23, 2023

    @benjamingr
    benjamingr
    on May 23, 2023
    Member
    More actions

    Interesting stuff (though bad manners) at uNetworking/uWebSockets.js#904 namely that experiment (that does break async_hooks but also improves performance)

  10. UlisesGascon
    added
    help wantedExtra attention is needed
    on Jun 12, 2023
  11. H4ad commented on Jul 11, 2023

    @H4ad
    H4ad
    on Jul 11, 2023
    Member
    More actions

    I could be wrong, but once this nodejs/node#48528 is merged, we can probably rewrite MakeCallback to use AsyncContextFrame, right? Assuming AsyncContextFrame is faster than async_hooks.

  12. benjamingr commented on Jul 12, 2023

    @benjamingr
    benjamingr
    on Jul 12, 2023
    Member
    More actions

    @H4ad that would work for people using AsyncLocalStorage but not for people otherwise using async hooks

    Reacted by Vinicius Lourenço
  13. Qard commented on Oct 2, 2023

    @Qard
    Qard
    on Oct 2, 2023
    Member
    More actions

    I've been wanting to split all the async_hooks stuff in C++ out into a delegate class which we could use a no-op version for until a hook is registered. That would make it easier to at least contain the overhead when it's not in-use.

    Reacted by Matteo Collina, Robert Nagy and Vinicius Lourenço
  14. anonrig
    removed
    performance-agenda
    on Oct 16, 2023
  15. joyeecheung commented on Oct 19, 2023

    @joyeecheung
    joyeecheung
    on Oct 19, 2023
    Member
    More actions

    My understanding (or, from nodejs/node#10101) is that async hooks do not actually contribute much to the overhead when it's not enabled - and it's opt-in already so users would be willingly taking the hit. Most of the overhead comes from the tick callback for process.nextTick. While it also only incurs a lot of overhead when actually used, due to the prevalence of its usage in Node.js internals and the user land, the overhead is also much more prevalent. Doing something about async hooks helps a bit, but it's also only a very small piece of the puzzle because it's actually not used that much in the user land. process.nextTick() is the real beast here that affect almost all use cases.

  16. 55 remaining items

  17. nodejs-github-bot
    added a commit that references this issue on Nov 23, 2026

    src: skip the async id stack when nobody sees it

    1ef4444
  18. aduh95
    added a commit that references this issue on Nov 23, 2026

    src: skip the async id stack when nobody sees it

    6abd8c4
  19. nodejs-github-bot
    added a commit that references this issue on Nov 25, 2026

    node-api: find the Environment once

    72bf392
  20. nigrosimone
    added a commit that references this issue on Nov 26, 2026

    src: add MakeCallback() taking an Environment

    3278f89
  21. nigrosimone commented on Nov 26, 2026

    @nigrosimone
    nigrosimone
    on Nov 26, 2026
    More actions

    Update: all the PRs landed!

    • src: avoid env lookups and a global handle in InternalCallbackScope node#66316: no Environment lookups and no global handle in InternalCallbackScope. node::MakeCallback 202-208 -> 155-159 ns.
    • src: keep ALS store in AsyncResource::MakeCallback node#66326: AsyncResource::MakeCallback keeps the ALS store without a scope. AsyncResource +29/+36%.
    • src: cut small costs in node::MakeCallback node#66395: small costs cut in node::MakeCallback. 136-141 -> 122-125 ns.
    • src: do not shrink async resources on every pop node#66344: the async resources deque is not freed on every pop, one alloc and one free less per callback on Windows and macOS.
    • src: skip the async id stack when nobody sees it node#66511: async ids are pushed only when someone can read them. node::MakeCallback +7.3%, AsyncResource +8.5%.
    • node-api: find the Environment once node#66509: napi_make_callback finds the Environment once. 200 -> 174 ns.
    • The V8 fast path (was deps: V8: cherry-pick 25650aa994 node#66500) for the empty microtask checkpoint came with the V8 update on main.
    • src: add MakeCallback() taking an Environment node#66510: new node::MakeCallback() overload taking the Environment, 133 -> 119 ns. It is the only new public API of the series, so feedback on the shape is welcome.
    Reacted by Vinicius Lourenço
  22. nodejs-github-bot
    added a commit that references this issue on Nov 26, 2026

    src: add MakeCallback() taking an Environment

    3872251
  23. nigrosimone commented on Nov 27, 2026

    @nigrosimone
    nigrosimone
    on Nov 27, 2026
    More actions

    Before and after the whole series, measured with the official nightly binaries: v27.0.0-nightly20260928 (before nodejs/node#66316) and v28.0.0-nightly20261126 (after all the PRs except nodejs/node#66510). benchmark/napi/make_callback built for each, 30 alternated runs on one core, Linux x64 (WSL2), n=1e7:

    mcb-chart

    The part of MakeCallback that is Node goes from about 138 to about 61 ns. nodejs/node#66510 is opt-in and adds its own 133 -> 119 ns for addons that use it.

    Thanks to everyone who reviewed. ❤️

    Reacted by Arnaud Benhamdine, Matteo Collina, Robert Nagy and Vinicius Lourenço
  24. mcollina commented on Nov 27, 2026

    @mcollina
    mcollina
    on Nov 27, 2026
    SponsorMember
    More actions

    Are there any places in Node where an environment can be cached and the new makecallback taking an environment could be used?

    Reacted by Nigro Simone
    Reacted by Nigro Simone
  25. nigrosimone commented on Nov 27, 2026

    @nigrosimone
    nigrosimone
    on Nov 27, 2026
    More actions

    Yes! I found three places. Two are changed in nodejs/node#67833: AsyncResource::MakeCallback(), the path of the addons that use node::AsyncResource, and CheckImmediate(). The third is the TLS ticket key callback, a rare path, so I left it. For NAN, which calls node::MakeCallback() directly, there is a draft in nodejs/nan#1027.

  26. gabylb
    added 6 commits that reference this issue on Nov 27, 2026

    src: keep ALS store in AsyncResource::MakeCallback

    1eeb917

    src: cut small costs in node::MakeCallback

    3cbedbc

    src: do not shrink async resources on every pop

    86a0e41

    src: skip the async id stack when nobody sees it

    78201af

    node-api: find the Environment once

    56dc51b

    src: add MakeCallback() taking an Environment

    ca922df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    benchmark-neededAdd to issues that does not have a benchmarkhelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.