child_process: optimize IPC for large data
Squashed from: - child_process: stop indexOf() on whole IPC buffer - child_process: get rid of forEach() and slice() in IPC - child_process: get rid of another forEach() in IPC Fixes: https://github.com/nodejs/node/issues/3145 PR-URL: https://github.com/nodejs/node/pull/10557 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
1216a34590
commit
92eadd3037
@ -446,14 +446,20 @@ function setupChannel(target, channel) {
|
||||
channel.onread = function(nread, pool, recvHandle) {
|
||||
// TODO(bnoordhuis) Check that nread > 0.
|
||||
if (pool) {
|
||||
jsonBuffer += decoder.write(pool);
|
||||
// Linebreak is used as a message end sign
|
||||
var chunks = decoder.write(pool).split('\n');
|
||||
var numCompleteChunks = chunks.length - 1;
|
||||
// Last line does not have trailing linebreak
|
||||
var incompleteChunk = chunks[numCompleteChunks];
|
||||
if (numCompleteChunks === 0) {
|
||||
jsonBuffer += incompleteChunk;
|
||||
this.buffering = jsonBuffer.length !== 0;
|
||||
return;
|
||||
}
|
||||
chunks[0] = jsonBuffer + chunks[0];
|
||||
|
||||
var i, start = 0;
|
||||
|
||||
//Linebreak is used as a message end sign
|
||||
while ((i = jsonBuffer.indexOf('\n', start)) >= 0) {
|
||||
var json = jsonBuffer.slice(start, i);
|
||||
var message = JSON.parse(json);
|
||||
for (var i = 0; i < numCompleteChunks; i++) {
|
||||
var message = JSON.parse(chunks[i]);
|
||||
|
||||
// There will be at most one NODE_HANDLE message in every chunk we
|
||||
// read because SCM_RIGHTS messages don't get coalesced. Make sure
|
||||
@ -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 {
|
||||
@ -494,9 +498,10 @@ function setupChannel(target, channel) {
|
||||
var queue = target._handleQueue;
|
||||
target._handleQueue = null;
|
||||
|
||||
queue.forEach(function(args) {
|
||||
for (var i = 0; i < queue.length; i++) {
|
||||
var args = queue[i];
|
||||
target._send(args.message, args.handle, args.options, args.callback);
|
||||
});
|
||||
}
|
||||
|
||||
// Process a pending disconnect (if any).
|
||||
if (!target.connected && target.channel && !target._handleQueue)
|
||||
|
Loading…
x
Reference in New Issue
Block a user