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 V8 Fast API #54731

Open
ronag opened this issue Sep 3, 2024 · 7 comments
Open

Node-API V8 Fast API #54731

ronag opened this issue Sep 3, 2024 · 7 comments
Assignees
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.

Comments

@ronag
Copy link
Member

ronag commented Sep 3, 2024

What is the problem this feature will solve?

Node-API methods could be faster in certain cases

What is the feature you are proposing to solve the problem?

Somehow add support for V8 Fast API.

What alternatives have you considered?

No response

@ronag ronag added feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API. labels Sep 3, 2024
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Sep 3, 2024
@bnoordhuis
Copy link
Member

Multi-engine support is still a design constraint for napi, as far as I'm aware, so I don't see how that could possibly work - V8's fast API is incredibly V8-centric.

@ronag
Copy link
Member Author

ronag commented Sep 7, 2024

Similar to V8 we could create a constrained mode in Node-API that in V8 case could be implemented with fast api and in other engines would just fallback to normal implementation.

@vmoroz
Copy link
Member

vmoroz commented Sep 8, 2024

We have discussed adding the new API that adds the V8 Fast API support in Node-API team meeting.
It must be a great addition as long as it can fall back to the normal API for other JS engines.
I would like to create a draft implementation as a proof of concept.
Since I am also working on the Node-API implementation for the Hermes engine, it would be interesting to see if it can be applied there.

Copy link
Contributor

github-actions bot commented Mar 8, 2025

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Mark issue so that it is never considered stale 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.

@github-actions github-actions bot added the stale label Mar 8, 2025
@ronag
Copy link
Member Author

ronag commented Mar 8, 2025

@vmoroz any updates?

@github-actions github-actions bot removed the stale label Mar 9, 2025
@Creatur3245
Copy link

Here's the information about Issue #54731: Node-API V8 Fast API:

Problem:

  • Node-API methods could be faster in certain cases.

Proposed Feature:

  • Add support for V8 Fast API.

Alternatives Considered:

  • No response.

Additional Details:

  • Created by: ronag
  • Assignee: vmoroz
  • Labels: feature request, node-api
  • Comments: 5
  • State: Open

For more information, you can visit the issue page.

@Creatur3245
Copy link

👍

  • To provide a detailed and structured approach, I'll create a plan to elaborate on, solve, or fix the issue you're facing with the Node-API V8 Fast API feature. This will/should help determine the necessary steps and tools to use.

Let's start by creating a plan.

1. **Understand the Problem:**
  • Elaborate on why Node-API methods could be faster in certain cases.

  • Explain the concept of the V8 Fast API and how it can potentially solve the problem.

  1. Identify Possible Solutions:

    • Detail how to add support for the V8 Fast API in Node-API.

    • Explore any existing discussions, issues, or pull requests related to this topic.

  2. Implementation Steps:

    • Outline the steps required to implement support for the V8 Fast API in Node-API.

    • Provide code examples or references to similar implementations if available.

  3. Testing and Validation:

    • Describe how to test the changes to ensure they improve performance.

    • Suggest benchmarks or performance tests specific to Node-API methods.

  4. Documentation and Community Feedback:

    • Provide guidance on documenting the changes.

    • Discuss how to gather feedback from the Node.js community to further refine the implementation.

I'll start by executing the first step to understand the problem and explain the concept of the V8 Fast API.

Step 1: Understand the Problem and Concept of V8 Fast API

Why Node-API Methods Could Be Faster in Certain Cases

Node-API (N-API) provides a stable API for building native addons in Node.js. It abstracts the underlying JavaScript engine, allowing developers to write addons without worrying about changes in V8 (the JavaScript engine used by Node.js). However, the generality of N-API can lead to performance overhead in certain cases, where direct interaction with the V8 engine could be more efficient.

Concept of V8 Fast API

The V8 Fast API is a mechanism provided by the V8 JavaScript engine that allows for more efficient function calls between JavaScript and native code. It is designed to minimize the overhead associated with these calls by using a more streamlined calling convention. This can lead to significant performance improvements for operations that are called frequently or involve intensive computation.

Next Steps

To proceed with Step 2, I'll identify possible solutions for adding support for the V8 Fast API in Node-API, and explore any existing discussions, issues, or pull requests related to this topic.

Let's perform a search for any relevant discussions or pull requests in the nodejs/node repository.

V8 Fast API in Node-API

Problem Description

Node-API methods could be optimized for better performance in certain scenarios.

Proposed Solution

The proposal is to add support for V8 Fast API, which can potentially make Node-API methods faster.

Supporting Details

  1. Example of V8 Fast API Usage

    • In V8, the options fallback is defined as FastApiCallbackOptions.
    • Example in C++:
      static double divide(const int32_t a, const int32_t b, v8::FastApiCallbackOptions& options) {
        if (b == 0) {
          options.fallback = true;
          return 0;
        } else {
          return a / b;
        }
      }
    • Integration with Node.js:
      static void SlowDivide(const FunctionCallbackInfo<Value>& args) {
        Environment* env = Environment::GetCurrent(args);
        CHECK_GE(args.Length(), 2);
        CHECK(args[0]->IsInt32());
        CHECK(args[1]->IsInt32());
        auto a = args[0].As<v8::Int32>();
        auto b = args[1].As<v8::Int32>();
      
        if (b->Value() == 0) {
          return node::THROW_ERR_INVALID_STATE(env, "Error");
        }
      
        double result = a->Value() / b->Value();
        args.GetReturnValue().Set(v8::Number::New(env->isolate(), result));
      }
      
      static double FastDivide(const int32_t a, const int32_t b, v8::FastApiCallbackOptions& options) {
        if (b == 0) {
          TRACK_V8_FAST_API_CALL("custom_namespace.divide.error");
          options.fallback = true;
          return 0;
        } else {
          TRACK_V8_FAST_API_CALL("custom_namespace.divide.ok");
          return a / b;
        }
      }
      
      CFunction fast_divide_(CFunction::Make(FastDivide));
      
      static void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context, void* priv) {
        SetFastMethod(context, target, "divide", SlowDivide, &fast_divide_);
      }
      
      void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
        registry->Register(SlowDivide);
        registry->Register(FastDivide);
        registry->Register(fast_divide_.GetTypeInfo());
      }
  2. Relevant Files in Node.js Repository

You can find more details and the complete context by visiting the issue page.

Do reach out if anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

4 participants