Skip to content

Avoid extra memory allocation with templates #602

Closed

Description

@dmitryash

I suppose that it is possible to add methods in the following way to avoid extra memory allocation:

Example:

template <class T> 
class ObjectWrap {
    using MethodType = Value (T::*)(const CallbackInfo &info);

    template <MethodType method>
    static auto ObjectWrap<T>::InstanceMethod2(const char* utf8name) -> ClassPropertyDescriptor<T>; 
};

template <typename T>
template <typename ObjectWrap<T>::MethodType method>
inline auto ObjectWrap<T>::InstanceMethod2(const char* utf8name) -> ClassPropertyDescriptor<T>  {
    napi_property_descriptor desc = {};
    desc.utf8name = utf8name;
    desc.method = [](napi_evn env, napi_callback_info args) {
        return details:;WrapCallback([&]{
            CallbackInfo cb_info(env, args);
            T* instance = Unwrap(cb_info.This().As<Object>());
            return (instance->*cb)(callbackInfo);
        });
    };
    return desc;
}

// How to use
class Test: public Napi::ObjectWrap<Test> {
public:
    static auto define_class(Napi::Env env) -> Napi::Function {
        return DefineClass(env, "Test", {
            InstanceMethod2<&Test::get_value>("get_value");
        });
    }

    auto get_value(const Napi::CallbackInfo &args) -> Napi::Value;
};

Activity

  1. gabrielschulhof commented on Nov 17, 2019

    @gabrielschulhof
    Contributor

    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.

  2. dmitryash commented on Nov 18, 2019

    @dmitryash
    ContributorAuthor

    Let me take it

  3. gabrielschulhof commented on Nov 18, 2019

    @gabrielschulhof
    Contributor

    @dmitryash we discussed this during today's team meeting. The best approach is to add an alternate set of methods to ObjectWrap<T> and encourage their use over the existing methods. We can deprecate the existing methods and eventually remove them. Not sure what the name should be though – InstanceMethod2() does not seem too expressive. OTOH TemplatedInstanceMethod() is way too long of a name. I dunno ... InstanceMethodT() 🤷 😕

  4. gabrielschulhof commented on Nov 18, 2019

    @gabrielschulhof
    Contributor

    @dmitryash so if you would like to do a PR, then please, go right ahead 🙂

  5. added a commit that references this issue on Aug 24, 2022
  6. added a commit that references this issue on Aug 26, 2022
  7. added a commit that references this issue on Sep 19, 2022
  8. added a commit that references this issue on Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions