Skip to content

Commit

Permalink
stream: use plain objects for write/corked reqs
Browse files Browse the repository at this point in the history
This is similar to a change made awhile back for storing
process.nextTick() requests.

PR-URL: #10558
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
  • Loading branch information
mscdex committed Jan 11, 2017
1 parent b888bfe commit a3539ae
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a3539ae

Please sign in to comment.