@@ -112,10 +112,9 @@ function WritableState(options, stream) {
112
112
// count buffered requests
113
113
this . bufferedRequestCount = 0 ;
114
114
115
- // create the two objects needed to store the corked requests
116
- // they are not a linked list, as no new elements are inserted in there
115
+ // allocate the first CorkedRequest, there is always
116
+ // one allocated and free to use, and we maintain at most two
117
117
this . corkedRequestsFree = new CorkedRequest ( this ) ;
118
- this . corkedRequestsFree . next = new CorkedRequest ( this ) ;
119
118
}
120
119
121
120
WritableState . prototype . getBuffer = function writableStateGetBuffer ( ) {
@@ -387,12 +386,16 @@ function clearBuffer(stream, state) {
387
386
388
387
doWrite ( stream , state , true , state . length , buffer , '' , holder . finish ) ;
389
388
390
- // doWrite is always async, defer these to save a bit of time
389
+ // doWrite is almost always async, defer these to save a bit of time
391
390
// as the hot path ends with doWrite
392
391
state . pendingcb ++ ;
393
392
state . lastBufferedRequest = null ;
394
- state . corkedRequestsFree = holder . next ;
395
- holder . next = null ;
393
+ if ( holder . next ) {
394
+ state . corkedRequestsFree = holder . next ;
395
+ holder . next = null ;
396
+ } else {
397
+ state . corkedRequestsFree = new CorkedRequest ( state ) ;
398
+ }
396
399
} else {
397
400
// Slow case, write chunks one-by-one
398
401
while ( entry ) {
0 commit comments