在 node.js 中正确使用 _writev
Correct usage of _writev in node.js
node.js中_writev()
的正确用法是什么?
文档说:
If a stream implementation is capable of processing multiple chunks of data at once, the writable._writev()
method should be implemented.
它还说:
The primary intent of writable.cork()
is to avoid a situation where writing many small chunks of data to a stream do not cause a backup in the internal buffer that would have an adverse impact on performance. In such situations, implementations that implement the writable._writev()
method can perform buffered writes in a more optimized manner.
从流实现的角度来看,这是可以的。但是从可写流消费者的角度来看,调用 write
或 writev
的唯一方法是通过 Writable.write()
和 writable.cork()
我想看一个小例子来描述实现 _writev()
的实际用例
使用uncork()
时会调用_writev()
。节点文档中有一个简单的例子。
stream.cork();
stream.write('some ');
stream.write('data ');
process.nextTick(() => stream.uncork());
更多见,
https://nodejs.org/api/stream.html#stream_writable_uncork
https://github.com/nodejs/node/blob/master/lib/_stream_writable.js#L257
除了 write
之外,还可以将 writev
方法添加到实例中,如果流包含多个块,将选择该方法而不是 write
。例如 Elasticsearch 允许你批量插入记录;因此,如果您正在创建一个可写流来包装 Elasticsearch,那么使用 writev
方法来执行单个批量插入而不是几个单独的插入是有意义的,它的效率要高得多。例如,对于 MongoDB 等也是如此。
这个 post(不是我的)显示了 Elasticsearch 实现 https://medium.com/@mark.birbeck/using-writev-to-create-a-fast-writable-stream-for-elasticsearch-ac69bd010802
node.js中_writev()
的正确用法是什么?
文档说:
If a stream implementation is capable of processing multiple chunks of data at once, the
writable._writev()
method should be implemented.
它还说:
The primary intent of
writable.cork()
is to avoid a situation where writing many small chunks of data to a stream do not cause a backup in the internal buffer that would have an adverse impact on performance. In such situations, implementations that implement thewritable._writev()
method can perform buffered writes in a more optimized manner.
从流实现的角度来看,这是可以的。但是从可写流消费者的角度来看,调用 write
或 writev
的唯一方法是通过 Writable.write()
和 writable.cork()
我想看一个小例子来描述实现 _writev()
使用uncork()
时会调用_writev()
。节点文档中有一个简单的例子。
stream.cork();
stream.write('some ');
stream.write('data ');
process.nextTick(() => stream.uncork());
更多见,
https://nodejs.org/api/stream.html#stream_writable_uncork https://github.com/nodejs/node/blob/master/lib/_stream_writable.js#L257
除了 write
之外,还可以将 writev
方法添加到实例中,如果流包含多个块,将选择该方法而不是 write
。例如 Elasticsearch 允许你批量插入记录;因此,如果您正在创建一个可写流来包装 Elasticsearch,那么使用 writev
方法来执行单个批量插入而不是几个单独的插入是有意义的,它的效率要高得多。例如,对于 MongoDB 等也是如此。
这个 post(不是我的)显示了 Elasticsearch 实现 https://medium.com/@mark.birbeck/using-writev-to-create-a-fast-writable-stream-for-elasticsearch-ac69bd010802