Skip to content
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

Merged
merged 7 commits into from
Jan 24, 2023

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Jan 13, 2023

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

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Jan 13, 2023
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
Copy link
Member

@ronag ronag left a 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?

Copy link
Member

@tniessen tniessen left a 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.
Copy link
Member

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.)

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

@anonrig anonrig Jan 13, 2023

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

@anonrig
Copy link
Member Author

anonrig commented Jan 13, 2023

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?

I updated the example. Thanks for the hint! 🥇 💯

doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
Copy link
Member

@RafaelGSS RafaelGSS left a 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.

@devsnek devsnek self-requested a review January 13, 2023 19:52
@tony-go
Copy link
Member

tony-go commented Jan 13, 2023

Awesome @anonrig 🙌🏼

Learned a lot just by reading the PR ^^

doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Show resolved Hide resolved
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 20, 2023
@nodejs-github-bot
Copy link
Collaborator

doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
Comment on lines 14 to 15
* Throwing errors is not available on fast API, but can be done
through the fallback to slow API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.

Copy link
Member

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?

Copy link
Member Author

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?

doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
doc/contributing/adding-v8-fast-api.md Outdated Show resolved Hide resolved
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
Copy link
Member

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?

Copy link
Member Author

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?

src/README.md Outdated Show resolved Hide resolved
src/README.md Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@anonrig anonrig added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Jan 23, 2023
@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 24, 2023
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Jan 24, 2023
@nodejs-github-bot
Copy link
Collaborator

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/.ncu
https://github.com/nodejs/node/actions/runs/3992434537

@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Jan 24, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 24, 2023
@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 24, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 24, 2023
@nodejs-github-bot nodejs-github-bot merged commit 7dd4583 into nodejs:main Jan 24, 2023
@nodejs-github-bot
Copy link
Collaborator

Landed in 7dd4583

@anonrig anonrig deleted the docs-fast-api branch January 24, 2023 03:46
ruyadorno pushed a commit that referenced this pull request Feb 1, 2023
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]>
@ruyadorno ruyadorno mentioned this pull request Feb 1, 2023
juanarbol pushed a commit that referenced this pull request Mar 3, 2023
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]>
@juanarbol juanarbol mentioned this pull request Mar 3, 2023
juanarbol pushed a commit that referenced this pull request Mar 5, 2023
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.