-
Notifications
You must be signed in to change notification settings - Fork 465
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
Avoid extra memory allocation with templates #602
Comments
This does indeed work, and avoiding a memory allocation is definitely worth it. I'm not sure how to incorporate this, though. We should probably phase out the existing instance methods and accessors. |
Let me take it |
@dmitryash we discussed this during today's team meeting. The best approach is to add an alternate set of methods to |
@dmitryash so if you would like to do a PR, then please, go right ahead 🙂 |
ObjectWrap<T> was enhanced to support template methods for defining properties and methods of JS class. Now C++ methods and functions may be passed as template parameters for ObjectWrap<T>::InstanceMethod, ObjectWrap<T>::StaticAccessor, etc. There are several benefits: - no need to allocate extra memory for passing C++ function napi callback and use add_finalizer() to free memory; - a compiler can see whole chain of calls up to napi callback that may allow better optimisation. Some examples: ```cpp // Method InstanceMethod<&MyClass::method>("method"); // Read-write property InstanceAccessor<&MyClass::get, &MyClass::set>("rw_prop"); // Read-only property InstanceAccessor<&MyClass::get>("ro_prop"); ``` Fixes: nodejs/node-addon-api#602 PR-URL: nodejs/node-addon-api#604 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
ObjectWrap<T> was enhanced to support template methods for defining properties and methods of JS class. Now C++ methods and functions may be passed as template parameters for ObjectWrap<T>::InstanceMethod, ObjectWrap<T>::StaticAccessor, etc. There are several benefits: - no need to allocate extra memory for passing C++ function napi callback and use add_finalizer() to free memory; - a compiler can see whole chain of calls up to napi callback that may allow better optimisation. Some examples: ```cpp // Method InstanceMethod<&MyClass::method>("method"); // Read-write property InstanceAccessor<&MyClass::get, &MyClass::set>("rw_prop"); // Read-only property InstanceAccessor<&MyClass::get>("ro_prop"); ``` Fixes: nodejs/node-addon-api#602 PR-URL: nodejs/node-addon-api#604 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
ObjectWrap<T> was enhanced to support template methods for defining properties and methods of JS class. Now C++ methods and functions may be passed as template parameters for ObjectWrap<T>::InstanceMethod, ObjectWrap<T>::StaticAccessor, etc. There are several benefits: - no need to allocate extra memory for passing C++ function napi callback and use add_finalizer() to free memory; - a compiler can see whole chain of calls up to napi callback that may allow better optimisation. Some examples: ```cpp // Method InstanceMethod<&MyClass::method>("method"); // Read-write property InstanceAccessor<&MyClass::get, &MyClass::set>("rw_prop"); // Read-only property InstanceAccessor<&MyClass::get>("ro_prop"); ``` Fixes: nodejs/node-addon-api#602 PR-URL: nodejs/node-addon-api#604 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
ObjectWrap<T> was enhanced to support template methods for defining properties and methods of JS class. Now C++ methods and functions may be passed as template parameters for ObjectWrap<T>::InstanceMethod, ObjectWrap<T>::StaticAccessor, etc. There are several benefits: - no need to allocate extra memory for passing C++ function napi callback and use add_finalizer() to free memory; - a compiler can see whole chain of calls up to napi callback that may allow better optimisation. Some examples: ```cpp // Method InstanceMethod<&MyClass::method>("method"); // Read-write property InstanceAccessor<&MyClass::get, &MyClass::set>("rw_prop"); // Read-only property InstanceAccessor<&MyClass::get>("ro_prop"); ``` Fixes: nodejs/node-addon-api#602 PR-URL: nodejs/node-addon-api#604 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
I suppose that it is possible to add methods in the following way to avoid extra memory allocation:
Example:
The text was updated successfully, but these errors were encountered: