You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
net/http: let Transport request body writes use sendfile
net.TCPConn has the ability to send data out using system calls such as
sendfile when the source data comes from an *os.File. However, the way
that I/O has been laid out in the transport means that the File is
actually wrapped behind two outer io.Readers, and as such the TCP stack
cannot properly type-assert the reader, ensuring that it falls back to
genericReadFrom. Or it would, if persistConnWriter implemented
io.ReaderFrom, which is missing as well.
As such, this commit does the following:
* Removes transferBodyReader and moves its functionality to a new
doBodyCopy helper. This is not an io.Reader implementation, but no
functionality is lost this way, and it allows us to unwrap one layer
from the body.
* The second layer of the body is unwrapped if the original writer
was wrapped with ioutil.NopCloser, which is what NewRequest wraps the
body in if it's not a ReadCloser on its own. The unwrap operation
passes through the existing body if there's no nopCloser.
* Finally, io.ReaderFrom is implemented for persistConnWriter in the
higher-level transport, which was preventing ReadFrom in net.TCPConn
from being used in the first place. This has the additional benefit of
facilitating the fallback to genericReadFrom if there's still no
*os.File, which by itself was giving significant performance gains over
the io.Writer implementation.
Fixes#30377.
0 commit comments