Question

I'm writing a native node module for an in-process database which features zero-copy data lookups. I'd like my module to also have this ability. In other words, when I get data from the database, I'd like to pass the data to V8 without the need to copy memory or the need to parse something.

How can I do this?

So far, all the ways I have seen involved either parsing back and forth between JSON (pretty much a waste of resources in this case) or instantiating V8 data structures and copying data into them.

NOTE: in case you are wondering, zero-copy data lookup means (in a nutshell) that the database engine does not need to copy memory when it retrieves data.

Was it helpful?

Solution

I think I found the solution, although it is rather limited.

Interesting documentation can be found here: https://developers.google.com/v8/ (overview) and http://izs.me/v8-docs/main.html (API docs)

Seems that V8 has an ExternalStringResource class which can be used for this purpose:
http://izs.me/v8-docs/classv8_1_1String_1_1ExternalStringResource.html

Node itself also has a Buffer class which can also be used for similar ends:
http://nodejs.org/api/buffer.html

By using the above two classes, it is possible to implement zero-copy in a native node module for strings and byte arrays. Unfortunately it seems that (at the time I'm writing this) it isn't possible for objects.

EDIT

In case you are okay with just having the zero-copy ability for the string or Buffer properties of the object (but not the whole object itself), it can easily be implemented using the interceptors or accessors in the V8 API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top