(DO NOT LAND YET) experiment: socket.read(buf)
+1's will be removed, please use GitHub reactions (- Fishrock123)
Raw numbers to convince
Throughput before this patch:
$ node client.js
2.2 gb/s
Throughput with this patch:
$ node client.js buf
5.6 gb/s
Rationale
Lots of time is spent in malloc
/free
calls and V8's GC to manage Buffer
instances when reading data from the socket. However, many use cases could be rewritten in such way that the Buffer
will be allocated only once and reused. Current API doesn't allow this, but with a slight modification to stream.Readable
it could:
const buf = Buffer.alloc(1024 * 1024);
const chunk = stream.read(buf);
// NOTE: `chunk` may be either a slice of the `buf`, or a different buffer
// if the underlying stream does not support this mode
In case of this .read(buf)
call, one more argument will be passed to _read
function:
Socket.prototype._read = function _read(n, buf) {
if (buf) {
// Do something...
}
};
Intention of this PR
This PR contains experimental implementation of this feature, and I would like to ask @nodejs/collaborators, @nodejs/streams, and @nodejs/ctc to weigh in and provide some feedback on it.
Thanks!
If we will reach some preliminary consensus on this here, I'll move the discussion over to https://github.com/nodejs/node-eps