Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize child_process IPC for large data #10557

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions benchmark/child_process/child-process-read-ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
if (process.argv[2] === 'child') {
const len = +process.argv[3];
const msg = `"${'.'.repeat(len)}"`;
while (true) {
process.send(msg);
}
const send = () => {
while (process.send(msg));
// Wait: backlog of unsent messages exceeds threshold
setTimeout(send, 20);
};
send();
} else {
const common = require('../common.js');
const bench = common.createBenchmark(main, {
len: [64, 256, 1024, 4096, 32768],
len: [
64, 256, 1024, 4096, 16384, 65536,
65536 << 4, 65536 << 8
],
dur: [5]
});
const spawn = require('child_process').spawn;
Expand All @@ -29,8 +35,7 @@ if (process.argv[2] === 'child') {

setTimeout(function() {
child.kill();
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
bench.end(gbits);
bench.end(bytes);
}, dur * 1000);
}
}
24 changes: 14 additions & 10 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,19 @@ function setupChannel(target, channel) {
channel.onread = function(nread, pool, recvHandle) {
// TODO(bnoordhuis) Check that nread > 0.
if (pool) {
jsonBuffer += decoder.write(pool);

var i, start = 0;
// Linebreak is used as a message end sign
var lines = decoder.write(pool).split('\n');
var chunks = lines.slice(0, -1);
// Last line does not have trailing linebreak
var incompleteChunk = lines[lines.length - 1];
if (chunks.length === 0) {
jsonBuffer += incompleteChunk;
this.buffering = jsonBuffer.length !== 0;
return;
}
chunks[0] = jsonBuffer + chunks[0];

//Linebreak is used as a message end sign
while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
var json = jsonBuffer.slice(start, i);
chunks.forEach(function(json) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach() is slower than a normal for loop.

var message = JSON.parse(json);

// There will be at most one NODE_HANDLE message in every chunk we
Expand All @@ -462,10 +468,8 @@ function setupChannel(target, channel) {
handleMessage(target, message, recvHandle);
else
handleMessage(target, message, undefined);

start = i + 1;
}
jsonBuffer = jsonBuffer.slice(start);
});
jsonBuffer = incompleteChunk;
this.buffering = jsonBuffer.length !== 0;

} else {
Expand Down