From 6734876c61ce14a90503698b6f01e1aeb58b8523 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 5 Sep 2020 13:50:26 +0200 Subject: [PATCH 1/4] stream: improbe Writable.destroy performance Avoid nextTick if there are no pending callbacks. --- lib/_stream_writable.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 22aee3236c4ea7..8d7da92f31564e 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -826,7 +826,10 @@ ObjectDefineProperties(Writable.prototype, { const destroy = destroyImpl.destroy; Writable.prototype.destroy = function(err, cb) { const state = this._writableState; - if (!state.destroyed) { + if ( + !state.destroyed && + (state.bufferedIndex < state.buffered.length || state[kOnFinished].length) + ) { process.nextTick(errorBuffer, state); } destroy.call(this, err, cb); From ab7b73dafbd17e9aaf1b3235894a43fe08ff969d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 5 Sep 2020 19:10:58 +0200 Subject: [PATCH 2/4] fixup --- lib/_stream_writable.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 8d7da92f31564e..9d3dda5117ffb7 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -45,6 +45,7 @@ const { getHighWaterMark, getDefaultHighWaterMark } = require('internal/streams/state'); +const assert = require('assert'); const { ERR_INVALID_ARG_TYPE, ERR_METHOD_NOT_IMPLEMENTED, @@ -826,12 +827,16 @@ ObjectDefineProperties(Writable.prototype, { const destroy = destroyImpl.destroy; Writable.prototype.destroy = function(err, cb) { const state = this._writableState; + + // Invoke pending callbacks. if ( - !state.destroyed && - (state.bufferedIndex < state.buffered.length || state[kOnFinished].length) + state.bufferedIndex < state.buffered.length || + state[kOnFinished].length ) { + assert(!state.destroyed); process.nextTick(errorBuffer, state); } + destroy.call(this, err, cb); return this; }; From 10073d3b299a3ee81953d1b27f300e099382b2b0 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 7 Sep 2020 10:45:51 +0200 Subject: [PATCH 3/4] fixup --- lib/_stream_writable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 9d3dda5117ffb7..7d1e1f2f973971 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -45,7 +45,6 @@ const { getHighWaterMark, getDefaultHighWaterMark } = require('internal/streams/state'); -const assert = require('assert'); const { ERR_INVALID_ARG_TYPE, ERR_METHOD_NOT_IMPLEMENTED, From 9345eb7105522e4b8eae16ad430557ddb0e5475b Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 7 Sep 2020 11:00:06 +0200 Subject: [PATCH 4/4] fixup --- lib/_stream_writable.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 7d1e1f2f973971..2343704b0cdf85 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -45,6 +45,7 @@ const { getHighWaterMark, getDefaultHighWaterMark } = require('internal/streams/state'); +const assert = require('internal/assert'); const { ERR_INVALID_ARG_TYPE, ERR_METHOD_NOT_IMPLEMENTED,