How to return a C++ object from getter?

92 views
Skip to first unread message

Second Datke

unread,
May 4, 2015, 2:19:57 AM5/4/15
to v8-u...@googlegroups.com
Some background information:

I'm working on a V8-C++ binding library. I know I'm reinventing the wheel, but there is some tiny divergence in my objectives: It's not for Web, Node.js, etc. It's actually for a game. I'm trying using V8 as its scripting engine, and it's not something like small Web game or demos of Canvas/WebGL, I wish it could be scalable and flexible (with the help of V8 Javascript).

Thus, as you know, a complex game system needs to be fast, and it may have many classes, which I want to expose to V8. My work for the 'wheel' went smoothly, with a few troubles about template metaprogramming, but eventually I worked out some useful features e.g. class methods, attributes, ctor binding.

The requirement also indicates that I need support for properties of custom classes, for example, it's quite common that an GameObject has some Vector attributes, and I need access to them in JS. Currently I use a SetAccessor() on InstanceTemplate for attributes, and I assigned a default getter/setter pair for it. The setter is quite simple (just fetch, convert the value, and assign to the pointer in InternalField). But things become different for getter.

Currently I store an ObjectTemplate and NewInstance() every time I return object from getters.

I'm also reading sources of other libraries on this. Famous ones like v8-juices and v8pp, uses a std::map/unordered_map that seems store every instances of certain class that referenced in JS. I think that it's kind of "dirty" but it seems quite fast. In fact, it's about two time faster than mine (while my library faster than v8pp in object creation, setter, etc. So the getter is the only problem left.).

Some projects involved in bindings, like Node.js, Atom Electron, etc., just 'avoids' this problem, since most of data inside Web/Client application is just numbers/strings,  they have no need of this, and it's not so performance critical too.
I also have experience of using Lua with binding of C++. It's quite efficient, since creation of userdata(a datatype of Lua representing C data/pointer) is fast, then you just set a metatable to it, done! But in V8, an instantiation of object is needed, which is relatively costly.

Is there any better solution?

Ben Noordhuis

unread,
May 4, 2015, 7:22:42 AM5/4/15
to v8-u...@googlegroups.com
If you want maximum performance, you should try to do as much as
possible in JS. Calling into C++ from JS has some overhead and
creating and mutating objects using the C++ API is comparatively
expensive. Instead of discrete objects with properties, perhaps you
can use a single typed array that you periodically update from C++
land?

A caveat to keep in mind when working with floating point numbers in
V8: floats are boxed, they are stored on the JS heap.

Creating an object that looks like `{ x: 1.23 }` may result in two
heap allocations, one for the object and one for the number. Heap
allocations are bad because it means more work for the garbage
collector and less CPU time for your application.

V8 does try to unbox floats but there are many places where it can't
or won't: for example, returning a float from a C++ API function
always seems to heap-allocate it.
Reply all
Reply to author
Forward
0 new messages