Repository navigation
Node-API performance #49922
Description
What is the problem this feature will solve?
Improve performance when creating large V8 objects which is critical since these must always be created on the main thread, blocking the event loop.
Currently, the overhead of each method is very significant - especially for the primitive operations such as simply setting a property.
As a very typical example of the current problem, you can consider these two methods for setting a property on an object:
napi_status napi_set_property(napi_env env,
napi_value object,
napi_value key,
napi_value value);
napi_status napi_set_named_property(napi_env env,
napi_value object,
const char* utf8name,
napi_value value);Naively, one expects that using the first one will be faster when the key is already UTF-16. However calling this method requires to first call napi_create_string_utf16 and the combined overhead of the two calls offsets the single call of the second method when the key is not very long (which is usually the case).
What is the feature you are proposing to solve the problem?
Step 1: (not needed, see below)napi_set_named_property that takes UTF-16
Step 2: Methods that can create in one call a whole array/object from a C data structure - especially ones for strings already encoded in UTF-16
What alternatives have you considered?
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsAwaiting Triage
Activity
mmomtchev commentedon Sep 28, 2023
Also this should probably use an
unionand be inline:node/src/js_native_api_v8.h
Line 295 in 3838b57
PS (sorry it already is
inlinein recent versions)bnoordhuis commentedon Sep 28, 2023
At least with V8, it doesn't matter if you pass in UTF-16, it'll convert it to latin1 / one-byte if possible. The way it checks that is actually pretty expensive so you're probably worse off ceteris paribus.
A version of napi_set_named_property that takes latin1 is a better bet. That way at least you avoid UTF-8 decoding overhead.
Aside: memcpy is a legal way of doing type punning in C++ (for POD types anyway.)
mmomtchev commentedon Sep 28, 2023
Yes, I confirm. There is no way to pass pre-encoded UTF16 data to V8?
Answer:
v8::String::ExternalStringResourceis the way to gomertcanaltin commentedon Oct 19, 2023
Could this be a solution? I tried to do something #50282
vmoroz commentedon Oct 27, 2023
This issue seems to be related to the issue #45905 where we looked at different ways to improve the perf for object creation.
Having a dedicated function that creates a data object from a struct should significantly improve the perf.
To improve property access we must add APIs to create internalized strings which V8 can use directly without any additional preprocessing.
github-actions commentedon Apr 25, 2024
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-staleIssues and PRs exempt from automated stale handling.
label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.