[英]How can I maintain zero-copy in a native node module?
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. 换句话说,当我从数据库中获取数据时,我想将数据传递给V8,而无需复制内存或需要解析某些内容。
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. 到目前为止,我所看到的所有方法都涉及在JSON之间来回解析(在这种情况下几乎浪费资源)或实例化V8数据结构并将数据复制到它们中。
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. 注意:如果您想知道,零拷贝数据查找意味着(简而言之)数据库引擎在检索数据时不需要复制内存。
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) 有趣的文档可以在这里找到: https : //developers.google.com/v8/ (overview)和http://izs.me/v8-docs/main.html(API docs)
Seems that V8 has an ExternalStringResource
class which can be used for this purpose: 似乎V8有一个ExternalStringResource
类,可用于此目的:
http://izs.me/v8-docs/classv8_1_1String_1_1ExternalStringResource.html 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: Node本身也有一个Buffer
类,它也可以用于类似的目的:
http://nodejs.org/api/buffer.html 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. 如果您只对对象的string
或Buffer
属性(但不是整个对象本身)具有零复制能力,则可以使用V8 API中的拦截器或访问器轻松实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.