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

node-api: add support for UTF-8 and Latin-1 property keys #52984

Closed
wants to merge 1 commit into from

Conversation

mertcanaltin
Copy link
Member

@mertcanaltin mertcanaltin commented May 14, 2024

This commit addresses issue #52979. It introduces new functions for optimized property key creation:

node_api_create_property_key_utf8
node_api_create_property_key_latin1
These functions help in creating property keys that allow more efficient property access when reused multiple times.

This commit also includes:

Updates to documentation to reflect the addition of the new functions.
New tests to ensure the correctness of the implementation.
Additionally, this work is related to the previous PR node-api: refactor napi_set_property function for improved performance #50282, which covered the utf16 case.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. labels May 14, 2024
@mertcanaltin mertcanaltin requested a review from anonrig May 14, 2024 18:56
doc/api/n-api.md Outdated Show resolved Hide resolved
doc/api/n-api.md Outdated Show resolved Hide resolved
Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

I would prefer adding node_api_set_named_property(napi_env env, napi_value object, const char* utf8name, size_t name_length, napi_value value) (which is the new naming pattern) and deprecating napi_set_named_property once the new one promoted to stable.

src/js_native_api_v8.cc Outdated Show resolved Hide resolved
Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

Thank you for working on this. Request for change on implementation as comments above.

test/js-native-api/test_string/test_string.c Outdated Show resolved Hide resolved
@mertcanaltin mertcanaltin force-pushed the dev-52979 branch 2 times, most recently from ab3f4c8 to 9c1a0bb Compare May 15, 2024 15:30
@mertcanaltin mertcanaltin changed the title napi: dd support for napi_set_named_property_len function napi: add support for napi_set_named_property_len function May 15, 2024
doc/api/n-api.md Outdated Show resolved Hide resolved
doc/api/n-api.md Outdated Show resolved Hide resolved
src/js_native_api.h Outdated Show resolved Hide resolved
@gabrielschulhof
Copy link
Contributor

@mertcanaltin we discussed this PR at today's Node-API team meeting. We have come to the conclusion that, although this new API adds convenience for developers, it actually encourages a pattern of runtime inefficiency by making it easy to create properties with keys for which the engine is unaware that they are not just regular strings but property keys, and thus it doesn't optimize them.

We believe it would be better to pivot this PR to creating additional variants of node_api_create_property_key_utf16 for the other two string encodings because it would encourage add-on maintainers to create property keys that are treated efficiently by the engine, thus increasing the performance of code that has to create large numbers of objects.

In addition, it would be advisable for add-on maintainers to create a property key once, and then reuse the resulting napi_value in subsequent calls to napi_set_property and/or napi_define_properties in order to avoid repeatedly creating the same string.

This is certainly less convenient for those writing the code, however, it increases the performance of the resulting add-ons.

@mertcanaltin
Copy link
Member Author

mertcanaltin commented May 17, 2024

@mertcanaltin we discussed this PR at today's Node-API team meeting. We have come to the conclusion that, although this new API adds convenience for developers, it actually encourages a pattern of runtime inefficiency by making it easy to create properties with keys for which the engine is unaware that they are not just regular strings but property keys, and thus it doesn't optimize them.

We believe it would be better to pivot this PR to creating additional variants of node_api_create_property_key_utf16 for the other two string encodings because it would encourage add-on maintainers to create property keys that are treated efficiently by the engine, thus increasing the performance of code that has to create large numbers of objects.

In addition, it would be advisable for add-on maintainers to create a property key once, and then reuse the resulting napi_value in subsequent calls to napi_set_property and/or napi_define_properties in order to avoid repeatedly creating the same string.

This is certainly less convenient for those writing the code, however, it increases the performance of the resulting add-ons.

First of all, thank you very much for your review and support, do you think I can continue to develop this pr I would be very happy if you have any suggestions ❤️🙏 @gabrielschulhof

@mertcanaltin
Copy link
Member Author

mertcanaltin commented May 17, 2024

I created a node_api_create_property_key_utf8 based on what you said

node_api_create_proper-
ty_key_utf8
node_api
_create_proper-
ty_key_ascil

I wonder if we can get a performance as you say by making different string encodings?

src/js_native_api.h Outdated Show resolved Hide resolved
@vmoroz
Copy link
Member

vmoroz commented May 24, 2024

I wonder if we can get a performance as you say by making different string encodings?

@mertcanaltin , there are a few Node-API benchmarks here: https://github.com/nodejs/node/tree/main/benchmark/napi. A new one can be created to benchmark different ways to set and get property values.
Note that the true advantage from using node_api_create_property_key_utf8 and node_api_create_property_key_utf16 must be seen only when the same property key is reused for setting/getting a property multiple times.

@KevinEady
Copy link
Contributor

I am hesitant of adding these APIs, as the functionality to do what you want already exists using both node_api_create_property_key_utf16() and napi_set_property(). I do not follow the argument of "an extra napi call" as outlined in #52979 (comment), as the team discussed it in a Node-API meeting and decided the additional call would be minimal overhead.

Adding new APIs has more overreaching effects than just Node.js itself, as other JavaScript engines must implement the new APIs as well.

Since this functionality already exists and can be performed in a trivial manner, I 👎 this addition.

@mertcanaltin
Copy link
Member Author

@mertcanaltin ... can I ask you to manually squash and rebase this PR. It's not landing cleanly as is.

did the rebase, many thanks for your review

This comment was marked as outdated.

@legendecas legendecas 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 Sep 4, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 4, 2024
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@jasnell
Copy link
Member

jasnell commented Sep 8, 2024

PR is currently blocked from landing by unreliable CI.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Sep 13, 2024

@legendecas legendecas added the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 13, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 13, 2024
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/52984
✔  Done loading data for nodejs/node/pull/52984
----------------------------------- PR info ------------------------------------
Title      node-api: add support for UTF-8 and Latin-1 property keys (#52984)
Author     Mert Can Altin <mertgold60@gmail.com> (@mertcanaltin)
Branch     mertcanaltin:dev-52979 -> nodejs:main
Labels     c++, node-api, author ready, needs-ci, commit-queue-squash
Commits    1
 - node-api: add support for UTF-8 and Latin-1 property keys
Committers 1
 - Mert Can Altin <mert.altin@trendyol.com>
PR-URL: https://github.com/nodejs/node/pull/52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Tue, 14 May 2024 18:50:26 GMT
   ✔  Approvals: 3
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/52984#pullrequestreview-2205518627
   ✔  - Chengzhong Wu (@legendecas) (TSC): https://github.com/nodejs/node/pull/52984#pullrequestreview-2279534350
   ✔  - Vladimir Morozov (@vmoroz): https://github.com/nodejs/node/pull/52984#pullrequestreview-2255528826
   ⚠  GitHub cannot link the author of 'node-api: add support for UTF-8 and Latin-1 property keys' to their GitHub account.
   ⚠  Please suggest them to take a look at https://github.com/nodejs/node/blob/99b1ada/doc/guides/contributing/pull-requests.md#step-1-fork
   ✘  Last GitHub CI failed
   ℹ  Last Full PR CI on 2024-09-13T15:07:38Z: https://ci.nodejs.org/job/node-test-pull-request/62402/
- Querying data for job/node-test-pull-request/62402/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/10854452945

@nodejs-github-bot nodejs-github-bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Sep 13, 2024
jasnell pushed a commit that referenced this pull request Sep 13, 2024
PR-URL: #52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
@jasnell
Copy link
Member

jasnell commented Sep 13, 2024

Landed in 75e4d0d

@jasnell jasnell closed this Sep 13, 2024
@legendecas
Copy link
Member

legendecas commented Sep 13, 2024

@jasnell you got ahead of me with manually landing it in minutes... Thanks for landing it!

RafaelGSS pushed a commit that referenced this pull request Sep 16, 2024
PR-URL: #52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
@RafaelGSS RafaelGSS mentioned this pull request Sep 16, 2024
RafaelGSS pushed a commit that referenced this pull request Sep 16, 2024
PR-URL: #52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
RafaelGSS pushed a commit that referenced this pull request Sep 17, 2024
PR-URL: #52984
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
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. c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-failed An error occurred while landing this pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API.
Projects
Development

Successfully merging this pull request may close these issues.