-
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
Expose v8::String::NewExternalOneByte
and v8::String::NewExternalTwoByte
to Node-API
#48198
Comments
Cross-engine portability is one of the n-api design goals and I don't think other engines have functionality that is similar enough. For instance, I'm fairly sure quickjs doesn't have a concept of non-owned strings. I suppose n-api targeting other engines could copy the string to the JS heap but having APis with wildly different performance across engines isn't great. |
Other engines could add such APIs if at some day node is seen as relevant user. |
For something comparable, the current Chrome implementation of webassembly stringref proposal implements It would probably make sense to at least have |
We've had problems with Electron not supporting external Buffers: nodejs/node-addon-api#1257. I'm not opposed to this proposal though. I think it's fine to fall back to regular string creation. |
BTW, is there an example of usage somewhere? Who manages the life cycle of the ExternalOneByteStringResource? |
I think the most common usage scenario is create JavaScript String from static string, like: denoland/deno#18051. There are also use cases in NAPI-RS: |
Yes, I handled it in NAPI-RS, too. I don't think it's a problem if we have a fallback strategy: https://github.com/napi-rs/napi-rs/blob/main/crates/napi/src/bindgen_runtime/js_values/buffer.rs#L245 |
let core_str =
v8::String::new_external_onebyte_static(scope, b"core").unwrap(); It looks like the rust bindings for V8 have some mechanism for creating a static v8::ExternalStringResource to wrap the string literal. I don't think we can do that in Node-API without exposing v8::ExternalStringResource itself. I think the only way to go is to heap-allocate v8::ExternalStringResource and tie it via a weak reference to the resulting string, to be freed when the string is garbage-collected. This would likely make it slower, not faster, and more memory intensive too, unless the string is sizable. |
We decided at the 2023-06-02 Node-API meeting that we will implement this as a best-effort optimization.
|
Introduce APIs that allow for the creation of JavaScript strings without copying the underlying native string into the engine. The APIs fall back to regular string creation if the engine's external string APIs are unavailable. In this case, an optional boolean out-parameter indicates that the string was copied, and the optional finalizer is called if given. PR-URL: #48339 Fixes: #48198 Reviewed-By: Daeyeon Jeong <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
Introduce APIs that allow for the creation of JavaScript strings without copying the underlying native string into the engine. The APIs fall back to regular string creation if the engine's external string APIs are unavailable. In this case, an optional boolean out-parameter indicates that the string was copied, and the optional finalizer is called if given. PR-URL: nodejs#48339 Fixes: nodejs#48198 Reviewed-By: Daeyeon Jeong <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
Introduce APIs that allow for the creation of JavaScript strings without copying the underlying native string into the engine. The APIs fall back to regular string creation if the engine's external string APIs are unavailable. In this case, an optional boolean out-parameter indicates that the string was copied, and the optional finalizer is called if given. PR-URL: nodejs#48339 Fixes: nodejs#48198 Reviewed-By: Daeyeon Jeong <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
Introduce APIs that allow for the creation of JavaScript strings without copying the underlying native string into the engine. The APIs fall back to regular string creation if the engine's external string APIs are unavailable. In this case, an optional boolean out-parameter indicates that the string was copied, and the optional finalizer is called if given. PR-URL: #48339 Fixes: #48198 Reviewed-By: Daeyeon Jeong <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
Introduce APIs that allow for the creation of JavaScript strings without copying the underlying native string into the engine. The APIs fall back to regular string creation if the engine's external string APIs are unavailable. In this case, an optional boolean out-parameter indicates that the string was copied, and the optional finalizer is called if given. PR-URL: #48339 Fixes: #48198 Reviewed-By: Daeyeon Jeong <[email protected]> Signed-off-by: Gabriel Schulhof <[email protected]>
What is the problem this feature will solve?
Provide a faster way to create JavaScript string from static str
What is the feature you are proposing to solve the problem?
v8::String::NewExternalOneByte
andv8::String::NewExternalTwoByte
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: