Giter Club home page Giter Club logo

Comments (6)

jasongin avatar jasongin commented on July 27, 2024 1

If you know the array type in advance, then you can convert directly to it, for example .As<TypedArrayOf<uint8_t>>(). Otherwise, yes you do need to convert to TypedArray and call TypedArray::TypedArrayType() first, before converting to the appropriate TypedArrayOf<T> type.

In most cases conversions between Napi::Value types using .As<T>() are cheap because they are effectively just casts - they don't allocate memory and most don't call any N-API functions. However, TypedArrayOf<T> is an exception, because it does call napi_get_typedarray_info().

So, this scenario is slightly inefficient in that it will result in two calls to napi_get_typedarray_info(): first when getting the array type and second when constructing the TypedArrayOf<T> instance. We could consider adding a more efficient API for converting from TypedArray to TypedArrayOf<T> when the array info is already known.

from node-addon-api.

jasongin avatar jasongin commented on July 27, 2024 1

But such a factory function could not return a TypedArrayOf<T> without the caller already knowing what T is.

(If these objects were allocated on the heap, the factory could return a base class pointer (TypedArray*), and then the caller could down-cast that pointer at a later time. But for efficiency all these objects are allocated on the stack, so that wouldn't work.)

from node-addon-api.

zandaqo avatar zandaqo commented on July 27, 2024

@jasongin I should have clarified that I'm talking about the second scenario you mentioned: when we don't know the type of an array in advance. In that case, converting to TypedArray and then to TypedArrayOf not only takes an extra call to napi_get_typedarray_info() that could be avoided, but also forces us to add extra interfaces when we have a function that accepts any type of a typed array from JS and treats them differently in C++.

For example, my existing C++ code expects to work with standard containers and to use it I have to convert typed arrays into vectors of their respective types (e.g. std::vector<float>, std::vector<double>, etc.). To do that I need to convert a Value to a TypedArray, then call the type getting function, then use switch on its result and map the resulting enum value to a type, and only then use the type to convert the array into TypedArrayOf, get its Data, and instantiate vector from it. Having something that can just convert from Value straight to the right typed array would make it all a lot easier.

from node-addon-api.

jasongin avatar jasongin commented on July 27, 2024

forces us to add extra interfaces when we have a function that accepts any type of a typed array from JS and treats them differently in C++.

Can you give an example of what you mean by "extra interfaces"?

Having something that can just convert from Value straight to the right typed array would make it all a lot easier.

I'm still not sure I understand what you're suggesting. How can the "right" type be returned unless you do a switch on the type enum first? Consider the following (hypothetical) API that can do the conversion in one step:

TypedArrayOf<T> Value::AsTypedArrayOf<T>();

You'd have to know what T is before calling it.

from node-addon-api.

zandaqo avatar zandaqo commented on July 27, 2024

I see. What I'm suggesting is probably more in the line of hiding that switching from the api consumer (perhaps implemented as a factory function) since TypedArrayOf might be more useful than just TypedArray.

from node-addon-api.

zandaqo avatar zandaqo commented on July 27, 2024

But for efficiency all these objects are allocated on the stack, so that wouldn't work.

Oh I get it. Yes, that point juggling is what I had in mind, but I missed the stack/heap part in my reasoning, too much JavaScript I guess. Thanks for clarifying this, and sorry for bothering with it.

from node-addon-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.