From 28ff3bde4a10c501724e74bcb22172960550f0df Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 19 Oct 2021 07:56:00 +0200 Subject: [PATCH] stream: default hwm based on Buffer.poolSize Make streams default HWM correspond to Buffer.poolSize. --- doc/api/stream.md | 3 ++- lib/internal/fs/streams.js | 3 ++- lib/internal/streams/state.js | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index 7042cbb1180c5f..20af5bf7cc7fe8 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2360,7 +2360,7 @@ changes: * `options` {Object} * `highWaterMark` {number} Buffer level when [`stream.write()`][stream-write] starts returning `false`. **Default:** - `16384` (16 KB), or `16` for `objectMode` streams. + [`Buffer.poolSize`][], or `16` for `objectMode` streams. * `decodeStrings` {boolean} Whether to encode `string`s passed to [`stream.write()`][stream-write] to `Buffer`s (with the encoding specified in the [`stream.write()`][stream-write] call) before passing @@ -3623,6 +3623,7 @@ contain multi-byte characters. [`'end'`]: #event-end [`'finish'`]: #event-finish [`'readable'`]: #event-readable +[`Buffer.poolSize`]: #class-property-bufferpoolsize [`Duplex`]: #class-streamduplex [`EventEmitter`]: events.md#class-eventemitter [`Readable`]: #class-streamreadable diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 5e9e338ab1fcbd..652d6b1b924cea 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -4,6 +4,7 @@ const { Array, FunctionPrototypeBind, MathMin, + MathMax, ObjectDefineProperty, ObjectSetPrototypeOf, PromisePrototypeThen, @@ -152,7 +153,7 @@ function ReadStream(path, options) { // A little bit bigger buffer and water marks by default options = copyObject(getOptions(options, {})); if (options.highWaterMark === undefined) - options.highWaterMark = 64 * 1024; + options.highWaterMark = MathMax(Buffer.poolSize, 64 * 1024); if (options.autoDestroy === undefined) { options.autoDestroy = false; diff --git a/lib/internal/streams/state.js b/lib/internal/streams/state.js index 83050a62f9cedc..d263b4edd25b63 100644 --- a/lib/internal/streams/state.js +++ b/lib/internal/streams/state.js @@ -4,6 +4,7 @@ const { MathFloor, NumberIsInteger, } = primordials; +const { Buffer } = require('buffer'); const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes; @@ -13,7 +14,7 @@ function highWaterMarkFrom(options, isDuplex, duplexKey) { } function getDefaultHighWaterMark(objectMode) { - return objectMode ? 16 : 16 * 1024; + return objectMode ? 16 : Buffer.poolSize; } function getHighWaterMark(state, options, duplexKey, isDuplex) {