-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
doc: add v8 fast api contribution guidelines #46199
Conversation
Review requested:
|
4944ab5
to
dcc756f
Compare
dcc756f
to
5a5669f
Compare
5a5669f
to
a2251da
Compare
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.
Instead of calculateX could you maybe have div(a, b)
as an example and have divide by 0 as example for when to switch to slow path?
a2251da
to
177f200
Compare
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.
cc @devsnek who has done most of the fast API work in node IIRC
Node.js uses [V8](https://github.com/v8/v8) as the JavaScript engine. | ||
In order to provide fast paths for functions that are called quite often, | ||
V8 proposes the usage of `fast api calls` that does not use any of the V8 | ||
internals, but uses internal C functions. |
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.
internal C functions
is rather ambiguous. Also, V8 internals
usually refers to the non-public V8 API, which we try not to use in any case. Lastly, if I remember correctly, V8 API calls are allowed to some degree. Not performing any allocations on the V8 heap is what's critical. (Unless this changed since I last worked on fast API calls.)
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.
I see. Thanks! Can you give an example of V8 API call, without any allocation on the V8 heap?
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.
I might be wrong, but I think @MayaLekova implemented a fast API that relies on GetAlignedPointerFromInternalField
in 6cfba9f, for example. @devsnek might know more about the exact conditions for which APIs are allowed.
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.
You are right. You can access pre-existing/pre-allocated data from the fast apis. I thought it was inferred from the documentation (at least I assumed). Is there any specific wording you'd like me to 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.
I've updated the documentation.
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 performing any allocations on the V8 heap is what's critical.
Indeed that's still the case, and until we have the current GC architecture, it will stay like this. Possible future relaxation of this rule might come with conservative stack scanning. Please note that this also leads to the limitation for not calling back into JS code (I think this was discussed in this thread too).
Some brief background on the support for various types in V8 could be found in this external doc.
177f200
to
da9f97e
Compare
I updated the example. Thanks for the hint! 🥇 💯 |
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.
I think you should mention this file in the src/README.md too.
Awesome @anonrig 🙌🏼 Learned a lot just by reading the PR ^^ |
90c81b8
to
66a8977
Compare
* Throwing errors is not available on fast API, but can be done | ||
through the fallback to slow API. |
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.
* Throwing errors is not available on fast API, but can be done | |
through the fallback to slow API. | |
* Throwing errors is not available from within a fast API call, but can be done | |
through the fallback to the slow API. |
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.
Perhaps it's also better to suggest that since in principal, the fast API and the slow API should behave the same to avoid bugs, it's better not to throw errors in the slow API either and just update some flags and return to the JS land to throw?
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.
I agree in principal with @joyeecheung, but that is not a limitation of a V8 Fast API declaration, but an implementation recommendation. I think it should not be included in this documentation, but in a more style
specific documentation like README.md?
In V8, the options fallback is defined as `FastApiCallbackOptions` inside | ||
[`v8-fast-api-calls.h`](../../deps/v8/include/v8-fast-api-calls.h). | ||
|
||
* C++ land |
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.
Why is this a bullet point?
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.
It aligns with other sections in the same file for mentioning the context of the example it's referring to. Do you have a recommendation for an alternative?
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
Commit Queue failed- Loading data for nodejs/node/pull/46199 ✔ Done loading data for nodejs/node/pull/46199 ----------------------------------- PR info ------------------------------------ Title doc: add v8 fast api contribution guidelines (#46199) Author Yagiz Nizipli (@anonrig) Branch anonrig:docs-fast-api -> nodejs:main Labels doc, author ready, commit-queue-squash Commits 7 - doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines - fixup! doc: add v8 fast api contribution guidelines Committers 1 - Yagiz Nizipli PR-URL: https://github.com/nodejs/node/pull/46199 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Darshan Sen Reviewed-By: Rafael Gonzaga Reviewed-By: James M Snell ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/46199 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Darshan Sen Reviewed-By: Rafael Gonzaga Reviewed-By: James M Snell -------------------------------------------------------------------------------- ℹ This PR was created on Fri, 13 Jan 2023 14:07:12 GMT ✔ Approvals: 5 ✔ - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/46199#pullrequestreview-1266448194 ✔ - Robert Nagy (@ronag) (TSC): https://github.com/nodejs/node/pull/46199#pullrequestreview-1248744183 ✔ - Darshan Sen (@RaisinTen) (TSC): https://github.com/nodejs/node/pull/46199#pullrequestreview-1249572133 ✔ - Rafael Gonzaga (@RafaelGSS) (TSC): https://github.com/nodejs/node/pull/46199#pullrequestreview-1253500941 ✔ - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/46199#pullrequestreview-1262907890 ✔ Last GitHub CI successful ℹ Last Full PR CI on 2023-01-20T03:25:41Z: https://ci.nodejs.org/job/node-test-pull-request/49099/ ⚠ Commits were pushed after the last Full PR CI run: ⚠ - fixup! doc: add v8 fast api contribution guidelines - Querying data for job/node-test-pull-request/49099/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/3992434537 |
Landed in 7dd4583 |
PR-URL: #46199 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #46199 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #46199 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: James M Snell <[email protected]>
I'm introducing a guideline to define and share the limitations/caveats and experiences I've gained from researching on this topic. This would help new contributors to add fast APIs to existing functions.
Referencing: nodejs/performance#23