From 9bc4ea8d8b8d06b6728ab810487ccf41ee91efb8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 1 Feb 2019 00:18:51 +0100 Subject: [PATCH] worker: use _writev in internal communication --- lib/internal/main/worker_thread.js | 5 +++-- lib/internal/worker.js | 7 +++++-- lib/internal/worker/io.js | 7 +++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 3b7a2ef873ffab..ecf5d00c440a26 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -167,8 +167,9 @@ port.on('message', (message) => { CJSLoader.Module.runMain(filename); } } else if (message.type === STDIO_PAYLOAD) { - const { stream, chunk, encoding } = message; - process[stream].push(chunk, encoding); + const { stream, chunks } = message; + for (const { chunk, encoding } of chunks) + process[stream].push(chunk, encoding); } else { assert( message.type === STDIO_WANTS_MORE_DATA, diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 48886aee3fef5d..c312c3a1105523 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -243,8 +243,11 @@ class Worker extends EventEmitter { return this[kOnErrorMessage](message.error); case messageTypes.STDIO_PAYLOAD: { - const { stream, chunk, encoding } = message; - return this[kParentSideStdio][stream].push(chunk, encoding); + const { stream, chunks } = message; + const readable = this[kParentSideStdio][stream]; + for (const { chunk, encoding } of chunks) + readable.push(chunk, encoding); + return; } case messageTypes.STDIO_WANTS_MORE_DATA: { diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 1167198676b01b..0a97f17595faa8 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -206,12 +206,11 @@ class WritableWorkerStdio extends Writable { this[kWritableCallbacks] = []; } - _write(chunk, encoding, cb) { + _writev(chunks, cb) { this[kPort].postMessage({ type: messageTypes.STDIO_PAYLOAD, stream: this[kName], - chunk, - encoding + chunks: chunks.map(({ chunk, encoding }) => ({ chunk, encoding })) }); this[kWritableCallbacks].push(cb); if (this[kPort][kWaitingStreams]++ === 0) @@ -222,7 +221,7 @@ class WritableWorkerStdio extends Writable { this[kPort].postMessage({ type: messageTypes.STDIO_PAYLOAD, stream: this[kName], - chunk: null + chunks: [ { chunk: null, encoding: '' } ] }); cb(); }