Skip to content

Commit 1d88182

Browse files
mscdexronag
authored andcommitted
stream: improve writable.write() performance
PR-URL: #31624 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Backport-PR-URL: #32169
1 parent 6122620 commit 1d88182

File tree

1 file changed

+27
-48
lines changed

1 file changed

+27
-48
lines changed

lib/_stream_writable.js

+27-48
Original file line numberDiff line numberDiff line change
@@ -267,35 +267,8 @@ Writable.prototype.pipe = function() {
267267
};
268268

269269

270-
function writeAfterEnd(stream, cb) {
271-
const er = new ERR_STREAM_WRITE_AFTER_END();
272-
// TODO: defer error events consistently everywhere, not just the cb
273-
errorOrDestroy(stream, er);
274-
process.nextTick(cb, er);
275-
}
276-
277-
// Checks that a user-supplied chunk is valid, especially for the particular
278-
// mode the stream is in. Currently this means that `null` is never accepted
279-
// and undefined/non-string values are only allowed in object mode.
280-
function validChunk(stream, state, chunk, cb) {
281-
var er;
282-
283-
if (chunk === null) {
284-
er = new ERR_STREAM_NULL_VALUES();
285-
} else if (typeof chunk !== 'string' && !state.objectMode) {
286-
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
287-
}
288-
if (er) {
289-
errorOrDestroy(stream, er);
290-
process.nextTick(cb, er);
291-
return false;
292-
}
293-
return true;
294-
}
295-
296270
Writable.prototype.write = function(chunk, encoding, cb) {
297271
const state = this._writableState;
298-
var ret = false;
299272
const isBuf = !state.objectMode && Stream._isUint8Array(chunk);
300273

301274
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
@@ -305,29 +278,42 @@ Writable.prototype.write = function(chunk, encoding, cb) {
305278

306279
if (typeof encoding === 'function') {
307280
cb = encoding;
308-
encoding = null;
281+
encoding = state.defaultEncoding;
282+
} else {
283+
if (!encoding)
284+
encoding = state.defaultEncoding;
285+
if (typeof cb !== 'function')
286+
cb = nop;
309287
}
310288

311289
if (isBuf)
312290
encoding = 'buffer';
313-
else if (!encoding)
314-
encoding = state.defaultEncoding;
315-
316-
if (typeof cb !== 'function')
317-
cb = nop;
318291

292+
let err;
319293
if (state.ending) {
320-
writeAfterEnd(this, cb);
294+
err = new ERR_STREAM_WRITE_AFTER_END();
321295
} else if (state.destroyed) {
322-
const err = new ERR_STREAM_DESTROYED('write');
323-
process.nextTick(cb, err);
324-
errorOrDestroy(this, err);
325-
} else if (isBuf || validChunk(this, state, chunk, cb)) {
326-
state.pendingcb++;
327-
ret = writeOrBuffer(this, state, chunk, encoding, cb);
296+
err = new ERR_STREAM_DESTROYED('write');
297+
} else if (chunk === null) {
298+
err = new ERR_STREAM_NULL_VALUES();
299+
} else {
300+
if (!isBuf && !state.objectMode) {
301+
if (typeof chunk !== 'string') {
302+
err = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
303+
} else if (encoding !== 'buffer' && state.decodeStrings !== false) {
304+
chunk = Buffer.from(chunk, encoding);
305+
encoding = 'buffer';
306+
}
307+
}
308+
if (err === undefined) {
309+
state.pendingcb++;
310+
return writeOrBuffer(this, state, chunk, encoding, cb);
311+
}
328312
}
329313

330-
return ret;
314+
process.nextTick(cb, err);
315+
errorOrDestroy(this, err, true);
316+
return false;
331317
};
332318

333319
Writable.prototype.cork = function() {
@@ -402,13 +388,6 @@ ObjectDefineProperty(Writable.prototype, 'writableCorked', {
402388
// in the queue, and wait our turn. Otherwise, call _write
403389
// If we return false, then we need a drain event, so set that flag.
404390
function writeOrBuffer(stream, state, chunk, encoding, cb) {
405-
if (!state.objectMode &&
406-
state.decodeStrings !== false &&
407-
encoding !== 'buffer' &&
408-
typeof chunk === 'string') {
409-
chunk = Buffer.from(chunk, encoding);
410-
encoding = 'buffer';
411-
}
412391
const len = state.objectMode ? 1 : chunk.length;
413392

414393
state.length += len;

0 commit comments

Comments
 (0)