From cf071a01f2297834057039b8a973b06be2f2da2d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 24 Jul 2019 15:56:13 +0200 Subject: [PATCH] stream: resolve perf regression introduced by V8 7.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit contains two fixes: 1. use instanceof instead of Object.getPrototypeOf, as checking an object prototype with Object.getPrototypeOf is slower than an instanceof check. 2. avoid parseInt(undefined, 10) to get NaN as it regressed. PR-URL: https://github.com/nodejs/node/pull/28842 Fixes: https://github.com/nodejs/node/issues/28586 Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Anto Aravinth Reviewed-By: Anna Henningsen Reviewed-By: Trivikram Kamat Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott --- lib/_stream_readable.js | 11 +++++++++-- lib/_stream_writable.js | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 76077a117ad5d8..31cddbedac93a5 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -250,7 +250,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) { } else if (state.objectMode || chunk && chunk.length > 0) { if (typeof chunk !== 'string' && !state.objectMode && - Object.getPrototypeOf(chunk) !== Buffer.prototype) { + // Do not use Object.getPrototypeOf as it is slower since V8 7.3. + !(chunk instanceof Buffer)) { chunk = Stream._uint8ArrayToBuffer(chunk); } @@ -393,7 +394,13 @@ function howMuchToRead(n, state) { // You can override either this method, or the async _read(n) below. Readable.prototype.read = function(n) { debug('read', n); - n = parseInt(n, 10); + // Same as parseInt(undefined, 10), however V8 7.3 performance regressed + // in this scenario, so we are doing it manually. + if (n === undefined) { + n = NaN; + } else if (!Number.isInteger(n)) { + n = parseInt(n, 10); + } const state = this._readableState; const nOrig = n; diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index e85b4dead859bc..bf9abeeed81d45 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -277,7 +277,8 @@ Writable.prototype.write = function(chunk, encoding, cb) { var ret = false; const isBuf = !state.objectMode && Stream._isUint8Array(chunk); - if (isBuf && Object.getPrototypeOf(chunk) !== Buffer.prototype) { + // Do not use Object.getPrototypeOf as it is slower since V8 7.3. + if (isBuf && !(chunk instanceof Buffer)) { chunk = Stream._uint8ArrayToBuffer(chunk); }