Given the code below, we cork the socket, but still, we always add chunked encoding data to each chunk buffered instead of just the actual buffer sent during uncork.
We should move the chunked encoding data insertion to connectionCorkNT somehow and avoid a lot of unnecessary overhead.
What happens today is something like:
socket.cork()
writeLen(chunk1.length)
writeBody(chunk1)
writeLen(chunk2.length)
writeBody(chunk2)
socket.uncork()
// [len1, chunk1, len1, chunk1]
What we would like to achieve is:
// [len1 + len2, chunk1, chunk1]
We can achieve this by not writing the len before each chunk while corked. Instead, we can unshift the sum of all buffered chunks before we uncork.
// _http_outgoing.jsfunctionwrite_(msg,chunk,encoding,callback,fromEnd){if(typeofcallback!=='function')callback=nop;letlen;if(chunk===null){thrownewERR_STREAM_NULL_VALUES();}elseif(typeofchunk==='string'){len=Buffer.byteLength(chunk,encoding);}elseif(isUint8Array(chunk)){len=chunk.length;}else{thrownewERR_INVALID_ARG_TYPE('chunk',['string','Buffer','Uint8Array'],chunk);}leterr;if(msg.finished){err=newERR_STREAM_WRITE_AFTER_END();}elseif(msg.destroyed){err=newERR_STREAM_DESTROYED('write');}if(err){if(!msg.destroyed){onError(msg,err,callback);}else{process.nextTick(callback,err);}returnfalse;}if(!msg._header){if(fromEnd){msg._contentLength=len;}msg._implicitHeader();}if(!msg._hasBody){debug('This type of response MUST NOT have a body. '+'Ignoring write() calls.');process.nextTick(callback);returntrue;}if(!fromEnd&&msg.socket&&!msg.socket.writableCorked){msg.socket.cork();process.nextTick(connectionCorkNT,msg.socket);}letret;if(msg.chunkedEncoding&&chunk.length!==0){msg._send(NumberPrototypeToString(len,16),'latin1',null);msg._send(crlf_buf,null,null);msg._send(chunk,encoding,null);ret=msg._send(crlf_buf,null,callback);}else{ret=msg._send(chunk,encoding,callback);}debug('write ret = '+ret);returnret;}functionconnectionCorkNT(conn){conn.uncork();}
Given the code below, we cork the socket, but still, we always add chunked encoding data to each chunk buffered instead of just the actual buffer sent during uncork.
We should move the chunked encoding data insertion to
connectionCorkNTsomehow and avoid a lot of unnecessary overhead.What happens today is something like:
What we would like to achieve is:
We can achieve this by not writing the len before each chunk while corked. Instead, we can
unshiftthe sum of all buffered chunks before we uncork.