Is calling C functions in JS directly possible?

98 views
Skip to first unread message

Patric Lemo

unread,
Mar 31, 2022, 10:45:32 PM3/31/22
to v8-users
I know the FastAPI, but it's not fast enough. Is there any way to call the C functions directly, only considering the case of no parameter and no return value? For example, you can store the function address in a BigInt and then invoke it directly in JS. Are there any risks here? Can it be achieved? 
I wonder if anyone knows any other ways to speed up native calls

Patric Lemo

unread,
May 25, 2022, 12:03:26 AM5/25/22
to v8-users

Problems I met when using fast API calls.

Hit rate:

Whether Fast API can take effect seems to be related to JIT. The JIT was not triggered in a simple test example for the first 8000 ~ 11000 times.
In a complex project, some high-frequency functions should meet the trigger conditions but fail to trigger in the 20-minute test process.


Property functions are not triggered


Tested the getter setter method of x / y of `Vector2`, but failed to trigger the corresponding CFunction in a big loop.


Questions:


  1.  How to improve the hit rate?
  2.  Is it possible to trigger CFunction directly?
  3. Fucntioncallback and CFunction need to be provided at the same time. Can I omit the FunctionCallback?
  4. Will property callback supportFast API?

other:

Provide a mechanism to store the function address in a JS variable, jump(instruction) directly to the function, and ignore the sandbox mechanism?

Ben Noordhuis

unread,
May 27, 2022, 4:34:47 AM5/27/22
to v8-u...@googlegroups.com
On Wed, May 25, 2022 at 6:03 AM Patric Lemo <jiangz...@gmail.com> wrote:
>
>
> Problems I met when using fast API calls.
>
> Hit rate:
>
> Whether Fast API can take effect seems to be related to JIT. The JIT was not triggered in a simple test example for the first 8000 ~ 11000 times.
> In a complex project, some high-frequency functions should meet the trigger conditions but fail to trigger in the 20-minute test process.
>
>
> Property functions are not triggered
>
>
> Tested the getter setter method of x / y of `Vector2`, but failed to trigger the corresponding CFunction in a big loop.

Check with --trace_opt or --trace_opt_verbose if the method is getting
optimized (and if not, why not.) Fast API calls only work from
optimized code.

If you need help figuring out the problem, please post your code.

> Questions:
>
> How to improve the hit rate?
> Is it possible to trigger CFunction directly?
> Fucntioncallback and CFunction need to be provided at the same time. Can I omit the FunctionCallback?
> Will property callback supportFast API?
>
>
> other:
>
> Provide a mechanism to store the function address in a JS variable, jump(instruction) directly to the function, and ignore the sandbox mechanism?

JS and C++ have different calling conventions and value
representations so no, that can't work. There needs to be a
translation layer somewhere.
Reply all
Reply to author
Forward
0 new messages