-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: refactor napi_set_property function for improved performance #50282
Conversation
Review requested:
|
c8f6bdb
to
3b5302d
Compare
1047cc3
to
f686b2e
Compare
@mertcanaltin , thank you for working on the improvements to Node-API! It seems that the change is targeting to address the same issues as we discussed them in #45905. A better approach would be to create property keys with Would you be interested to implement such functions that create internalized strings? napi_status node_api_create_property_key_latin1(napi_env env, const char* str, size_t length, napi_value* result);
napi_status node_api_create_property_key_utf8(napi_env env, const char* str, size_t length, napi_value* result);
napi_status node_api_create_property_key_utf16(napi_env env, const char16_t* str, size_t length, napi_value* result);
napi_status node_api_create_property_key(napi_env env, napi_value str_or_sym, napi_value* result); The first three functions must create internalized strings from latin1, utf8, and utf16 strings. The last one must internalize |
Thank you very much for reviewing and detailing, I will be working on it again with an update in the future |
This commit refactors the napi_set_property_utf16 function to use internalized string property keys, which can lead to better performance. It implements @vmoroz suggestion to create internalized string property keys for accessing object properties. Internalized keys improve speed for property access operations like set, get, has, and delete by allowing V8 to compare string pointers instead of the entire string. This change aligns with the proposal in issue #45905. New functions added:
This change is expected to enhance the overall performance of Node-API operations involving property access. Fixes: #50282 |
@mertcanaltin , thank you for starting to implement the property key creation functions! I believe the Please have a look at the PR #48339 where we added creating external strings. |
greetings thank you very much for your suggestions, I applied it, now I think I need to add a test and update the document @vmoroz |
Awesome! Could you use the napi_status NAPI_CDECL node_api_create_property_key_utf16(napi_env env,
const char16_t* str,
size_t length,
napi_value* result) {
return v8impl::NewString(env, str, length, result, [&](v8::Isolate* isolate) {
return v8::String::NewFromTwoByte(isolate,
reinterpret_cast<const uint16_t*>(str),
v8::NewStringType::kInternalized,
static_cast<int>(length));
});
}
|
thank you for your suggestion I updated as you said @vmoroz |
@mertcanaltin , perfect! |
I made the additions as you said @vmoroz 🚀 🕺 |
I think I have a problem with the napiVersion |
@mertcanaltin. it looks great!
Also, please put the code for the new string APIs next to the string creation APIs added in PR #48339. |
these steps were great I hope I succeeded I made an update @vmoroz 🚀 |
return v8::String::NewFromTwoByte(isolate, | ||
reinterpret_cast<const uint16_t*>(str), | ||
v8::NewStringType::kInternalized, | ||
static_cast<int>(length)); |
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 do we need to cast to int
here? We don't cast to int
in the regular version of this function.
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.
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.
@gabrielschulhof , we are getting warnings in MSVC compiler if types are mismatch.
The NewFromTwoByte
expects type int
which is signed 32-bit integer.
In this function length
is size_t
which is unsigned 64-bit integer for 64 platforms.
MSVC warns about possible precision loss when the cast implicitly here.
The explicit cast suppresses the warning as we explicitly express our intent.
Ideally, we must add the cast in other places too to avoid the MSVC warnings.
But it should be another PR since currently Node.js has a lot of MSVC warnings anyway.
lint added empty line
70f4be5
to
6a27929
Compare
I removed my tag so as not to do something wrong, maybe the whole pipeline should go through and then I thought I should do it. 🤔 |
I wonder if I should do an update here @vmoroz 🚀 |
@mertcanaltin , it seems that CI has some flaky tests. I see other PRs have the same issue. |
PR-URL: #50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Landed in 59e7444 @mertcanaltin thanks for your work and patience on this one. |
thank you very much for your support ❤️ |
PR-URL: nodejs#50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: nodejs#50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: #50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: #50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: #50282 Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Node.js PR #50282 - N-API Performance Improvement
This pull request aims to optimize the
napi_set_property
function to enhance the overall performance of Node-API. The changes include:node_api_create_property_key_utf16
function has been updated to match the proposed format.node_api_create_property_key_utf16
function is now implemented using theNewString
function for a more streamlined approach.napi_set_property_utf16
function, which did not provide additional advantages overnode_api_create_property_key_utf16
, has been removed.It's worth noting that these changes are focused on improving the overall performance of Node-API operations, especially those involving property access.
/w @vmoroz 🙏
issue: #49922