From a3539ae3be51c54f0b2f1619e18980fc1a2cb8d2 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 31 Dec 2016 16:32:20 -0500 Subject: [PATCH] stream: use plain objects for write/corked reqs This is similar to a change made awhile back for storing process.nextTick() requests. PR-URL: https://github.com/nodejs/node/pull/10558 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Evan Lucas --- lib/_stream_writable.js | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index ba56225d974fe9..e1c912b3338915 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -16,13 +16,6 @@ util.inherits(Writable, Stream); function nop() {} -function WriteReq(chunk, encoding, cb) { - this.chunk = chunk; - this.encoding = encoding; - this.callback = cb; - this.next = null; -} - function WritableState(options, stream) { options = options || {}; @@ -113,7 +106,9 @@ function WritableState(options, stream) { // allocate the first CorkedRequest, there is always // one allocated and free to use, and we maintain at most two - this.corkedRequestsFree = new CorkedRequest(this); + var corkReq = { next: null, entry: null, finish: undefined }; + corkReq.finish = onCorkedFinish.bind(undefined, corkReq, this); + this.corkedRequestsFree = corkReq; } WritableState.prototype.getBuffer = function getBuffer() { @@ -304,7 +299,7 @@ function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { if (state.writing || state.corked) { var last = state.lastBufferedRequest; - state.lastBufferedRequest = new WriteReq(chunk, encoding, cb); + state.lastBufferedRequest = { chunk, encoding, callback: cb, next: null }; if (last) { last.next = state.lastBufferedRequest; } else { @@ -423,7 +418,9 @@ function clearBuffer(stream, state) { state.corkedRequestsFree = holder.next; holder.next = null; } else { - state.corkedRequestsFree = new CorkedRequest(state); + var corkReq = { next: null, entry: null, finish: undefined }; + corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state); + state.corkedRequestsFree = corkReq; } } else { // Slow case, write chunks one-by-one @@ -528,14 +525,6 @@ function endWritable(stream, state, cb) { stream.writable = false; } -// It seems a linked list but it is not -// there will be only 2 of these for each stream -function CorkedRequest(state) { - this.next = null; - this.entry = null; - this.finish = onCorkedFinish.bind(undefined, this, state); -} - function onCorkedFinish(corkReq, state, err) { var entry = corkReq.entry; corkReq.entry = null;