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

Updates to make ffi-napi compatible with an updated ref-napi #222

Open
ggryschuk opened this issue Sep 8, 2022 · 0 comments
Open

Updates to make ffi-napi compatible with an updated ref-napi #222

ggryschuk opened this issue Sep 8, 2022 · 0 comments

Comments

@ggryschuk
Copy link

Hi. As noted under the ref-napi issues list I've been wanting to update to use the ref-napi package but it's performance (and memory usage) was significantly slower (and larger) than the ref(NaN) version as others have observed. The ref-napi performance issues also impact ffi-napi.

The good news is I've identified the updates that need to be made to ffi-napi to make it properly work with a ref-napi with the 'performance updates' applied (including an additional update to ref-napi I identified as necessary, see my comments here 72 and here 76).

The updates necessary to ffi-napi are to code that expects that a pointer is a '0-length buffer'. I haven't found anything in the documentation that actually indicates that this is or should be true, and from what I can tell a pointer should be a '1-length buffer' (to hold the pointer itself). With the changes to ref-napi for performance and memory usage a pointer explicitly becomes a '1-length Buffer' which I believe is it's correct behavior (I do have to admit to being confused as to how the previous behavior actually worked in so much as storing a pointer in a Buffer that became 'zero length' on return from the C/C++ code).

Long story short, ffi-napi calls 'WrapPointer' of the ref-napi package (at the C/C++ code level) without specifying a size, since ref-napi now makes a pointer Buffer a 1-length Buffer the existing ffi-napi package won't work. The fix is to add a size of '1' to these WrapPointer calls, here's an example:

original line:

o["dlopen"] = WrapPointer(env, dlopen);

Updated line

o["dlopen"] = WrapPointer(env, dlopen,1);

I'm attaching a diff of the changes to be made to src/callback_info.cc, src/ffi.cc, test/ffi_test.cc, test/dynamic_library.js (to account for a test that assumed a pointer was a 0 length buffer).

With the updates to ref-napi & these updates the ffi-napi package passes all it's mocha tests.

Equally importantly (to me anyway) is that I've tested these changes to my own project & they work great. This was a limited but full test of my code that makes extensive use of ffi-napi, ref-napi & ref-struct-di, the performance was easily on par with the equivalent NaN packages, potentially slightly better.

I hope that someone will apply these patches (or equivalent ones in case I've entirely misunderstood how the ref-napi package should create a pointer Buffer) to the official ref-napi & ffi-napi packages so that others can test them.

Thanks.

PS. My apologies if this isn't correct usage of this forum. If there was a better way to submit this please advise.

node-ffi-napi-diff.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant