Commit 9499223
committed
net/http: optimize request body writes
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.1 parent 01f34cb commit 9499223
2 files changed
Lines changed: 40 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 56 | | |
70 | 57 | | |
71 | 58 | | |
| |||
347 | 334 | | |
348 | 335 | | |
349 | 336 | | |
350 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
351 | 341 | | |
352 | | - | |
| 342 | + | |
353 | 343 | | |
354 | 344 | | |
355 | 345 | | |
356 | 346 | | |
357 | 347 | | |
358 | | - | |
| 348 | + | |
359 | 349 | | |
360 | 350 | | |
361 | 351 | | |
| |||
364 | 354 | | |
365 | 355 | | |
366 | 356 | | |
367 | | - | |
| 357 | + | |
368 | 358 | | |
369 | | - | |
| 359 | + | |
370 | 360 | | |
371 | 361 | | |
372 | 362 | | |
373 | 363 | | |
374 | | - | |
| 364 | + | |
375 | 365 | | |
376 | 366 | | |
377 | 367 | | |
| |||
402 | 392 | | |
403 | 393 | | |
404 | 394 | | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
405 | 420 | | |
406 | 421 | | |
407 | 422 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1375 | 1375 | | |
1376 | 1376 | | |
1377 | 1377 | | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
1378 | 1384 | | |
1379 | 1385 | | |
1380 | 1386 | | |
| |||
0 commit comments