Skip to content

Commit a3539ae

Browse files
committed
stream: use plain objects for write/corked reqs
This is similar to a change made awhile back for storing process.nextTick() requests. PR-URL: #10558 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent b888bfe commit a3539ae

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

lib/_stream_writable.js

+7-18
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ util.inherits(Writable, Stream);
1616

1717
function nop() {}
1818

19-
function WriteReq(chunk, encoding, cb) {
20-
this.chunk = chunk;
21-
this.encoding = encoding;
22-
this.callback = cb;
23-
this.next = null;
24-
}
25-
2619
function WritableState(options, stream) {
2720
options = options || {};
2821

@@ -113,7 +106,9 @@ function WritableState(options, stream) {
113106

114107
// allocate the first CorkedRequest, there is always
115108
// one allocated and free to use, and we maintain at most two
116-
this.corkedRequestsFree = new CorkedRequest(this);
109+
var corkReq = { next: null, entry: null, finish: undefined };
110+
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, this);
111+
this.corkedRequestsFree = corkReq;
117112
}
118113

119114
WritableState.prototype.getBuffer = function getBuffer() {
@@ -304,7 +299,7 @@ function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
304299

305300
if (state.writing || state.corked) {
306301
var last = state.lastBufferedRequest;
307-
state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
302+
state.lastBufferedRequest = { chunk, encoding, callback: cb, next: null };
308303
if (last) {
309304
last.next = state.lastBufferedRequest;
310305
} else {
@@ -423,7 +418,9 @@ function clearBuffer(stream, state) {
423418
state.corkedRequestsFree = holder.next;
424419
holder.next = null;
425420
} else {
426-
state.corkedRequestsFree = new CorkedRequest(state);
421+
var corkReq = { next: null, entry: null, finish: undefined };
422+
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
423+
state.corkedRequestsFree = corkReq;
427424
}
428425
} else {
429426
// Slow case, write chunks one-by-one
@@ -528,14 +525,6 @@ function endWritable(stream, state, cb) {
528525
stream.writable = false;
529526
}
530527

531-
// It seems a linked list but it is not
532-
// there will be only 2 of these for each stream
533-
function CorkedRequest(state) {
534-
this.next = null;
535-
this.entry = null;
536-
this.finish = onCorkedFinish.bind(undefined, this, state);
537-
}
538-
539528
function onCorkedFinish(corkReq, state, err) {
540529
var entry = corkReq.entry;
541530
corkReq.entry = null;

0 commit comments

Comments
 (0)