Commit 46a2402
committed
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.
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 reader
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.
Note that this depends on change https://golang.org/cl/163737 to
properly function, as the lack of ReaderFrom implementation otherwise
means that this functionality is essentially walled off.
Benchmarks between this commit and https://golang.org/cl/163737
(where ReaderFrom was implemented for persistConnWriter):
linux/amd64:
name old time/op new time/op delta
FileAndServer_1KB/NoTLS-4 53.7µs ± 0% 53.5µs ± 0% -0.31% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-4 61.0µs ± 0% 61.1µs ± 0% ~ (p=0.165 n=10+10)
FileAndServer_16MB/NoTLS-4 6.30ms ± 6% 3.81ms ± 5% -39.48% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-4 13.5ms ± 1% 13.4ms ± 1% ~ (p=0.631 n=10+10)
FileAndServer_64MB/NoTLS-4 27.3ms ± 1% 16.4ms ± 3% -40.05% (p=0.000 n=9+10)
FileAndServer_64MB/TLS-4 53.9ms ± 2% 53.4ms ± 1% -0.93% (p=0.006 n=10+9)
name old speed new speed delta
FileAndServer_1KB/NoTLS-4 19.1MB/s ± 0% 19.1MB/s ± 0% +0.32% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-4 16.8MB/s ± 0% 16.8MB/s ± 0% ~ (p=0.302 n=10+10)
FileAndServer_16MB/NoTLS-4 2.67GB/s ± 6% 4.41GB/s ± 5% +65.32% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-4 1.25GB/s ± 1% 1.25GB/s ± 1% ~ (p=0.631 n=10+10)
FileAndServer_64MB/NoTLS-4 2.45GB/s ± 1% 4.09GB/s ± 3% +66.84% (p=0.000 n=9+10)
FileAndServer_64MB/TLS-4 1.25GB/s ± 2% 1.26GB/s ± 1% +0.93% (p=0.006 n=10+9)
name old alloc/op new alloc/op delta
FileAndServer_1KB/NoTLS-4 6.01kB ± 0% 5.03kB ± 0% -16.31% (p=0.000 n=8+10)
FileAndServer_1KB/TLS-4 6.11kB ± 0% 6.11kB ± 0% ~ (p=0.127 n=10+10)
FileAndServer_16MB/NoTLS-4 38.4kB ± 0% 5.1kB ± 0% -86.71% (p=0.000 n=8+8)
FileAndServer_16MB/TLS-4 74.1kB ± 0% 74.1kB ± 0% ~ (p=0.781 n=10+10)
FileAndServer_64MB/NoTLS-4 38.9kB ± 0% 5.6kB ± 1% -85.58% (p=0.000 n=8+10)
FileAndServer_64MB/TLS-4 184kB ± 1% 184kB ± 0% ~ (p=0.489 n=9+9)
name old allocs/op new allocs/op delta
FileAndServer_1KB/NoTLS-4 69.0 ± 0% 70.0 ± 0% +1.45% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-4 71.0 ± 0% 71.0 ± 0% ~ (all equal)
FileAndServer_16MB/NoTLS-4 70.0 ± 0% 70.0 ± 0% ~ (all equal)
FileAndServer_16MB/TLS-4 1.11k ± 0% 1.11k ± 0% ~ (p=0.189 n=10+10)
FileAndServer_64MB/NoTLS-4 71.0 ± 0% 71.0 ± 0% ~ (all equal)
FileAndServer_64MB/TLS-4 4.23k ± 0% 4.24k ± 0% +0.10% (p=0.002 n=9+8)
darwin/amd64:
name old time/op new time/op delta
FileAndServer_1KB/NoTLS-8 92.0µs ± 4% 91.6µs ± 1% ~ (p=0.601 n=10+7)
FileAndServer_1KB/TLS-8 103µs ± 5% 97µs ± 1% -6.20% (p=0.000 n=10+9)
FileAndServer_16MB/NoTLS-8 10.1ms ± 6% 10.2ms ± 4% ~ (p=0.315 n=10+8)
FileAndServer_16MB/TLS-8 18.3ms ± 2% 17.2ms ± 2% -6.34% (p=0.000 n=9+10)
FileAndServer_64MB/NoTLS-8 40.1ms ± 7% 37.7ms ± 3% -6.05% (p=0.000 n=9+9)
FileAndServer_64MB/TLS-8 70.2ms ± 4% 68.1ms ± 3% -3.01% (p=0.001 n=10+10)
name old speed new speed delta
FileAndServer_1KB/NoTLS-8 11.1MB/s ± 4% 11.2MB/s ± 1% ~ (p=0.617 n=10+7)
FileAndServer_1KB/TLS-8 9.92MB/s ± 5% 10.57MB/s ± 1% +6.53% (p=0.000 n=10+9)
FileAndServer_16MB/NoTLS-8 1.67GB/s ± 5% 1.65GB/s ± 3% ~ (p=0.315 n=10+8)
FileAndServer_16MB/TLS-8 910MB/s ± 5% 977MB/s ± 2% +7.40% (p=0.000 n=10+10)
FileAndServer_64MB/NoTLS-8 1.67GB/s ± 6% 1.78GB/s ± 3% +6.36% (p=0.000 n=9+9)
FileAndServer_64MB/TLS-8 956MB/s ± 3% 985MB/s ± 3% +3.09% (p=0.001 n=10+10)
name old alloc/op new alloc/op delta
FileAndServer_1KB/NoTLS-8 6.03kB ± 0% 6.03kB ± 0% -0.04% (p=0.021 n=8+10)
FileAndServer_1KB/TLS-8 6.15kB ± 0% 6.14kB ± 0% -0.18% (p=0.000 n=10+9)
FileAndServer_16MB/NoTLS-8 38.8kB ± 1% 38.9kB ± 1% ~ (p=0.529 n=10+10)
FileAndServer_16MB/TLS-8 75.5kB ± 1% 75.5kB ± 0% ~ (p=0.481 n=10+10)
FileAndServer_64MB/NoTLS-8 40.3kB ± 1% 39.3kB ± 1% -2.47% (p=0.000 n=9+10)
FileAndServer_64MB/TLS-8 189kB ± 1% 189kB ± 1% ~ (p=0.684 n=10+10)
name old allocs/op new allocs/op delta
FileAndServer_1KB/NoTLS-8 68.0 ± 0% 68.0 ± 0% ~ (all equal)
FileAndServer_1KB/TLS-8 71.0 ± 0% 71.0 ± 0% ~ (all equal)
FileAndServer_16MB/NoTLS-8 69.5 ± 1% 69.6 ± 1% ~ (p=1.000 n=10+10)
FileAndServer_16MB/TLS-8 1.13k ± 0% 1.14k ± 0% ~ (p=0.336 n=9+10)
FileAndServer_64MB/NoTLS-8 73.6 ± 1% 71.0 ± 0% -3.47% (p=0.000 n=9+9)
FileAndServer_64MB/TLS-8 4.34k ± 0% 4.34k ± 0% ~ (p=0.323 n=10+10)
Benchmarks between this commit and https://golang.org/cl/163862
(where benchmarks were added):
linux/amd64:
name old time/op new time/op delta
FileAndServer_1KB/NoTLS-4 53.3µs ± 0% 53.4µs ± 0% +0.21% (p=0.028 n=10+9)
FileAndServer_1KB/TLS-4 61.2µs ± 0% 60.8µs ± 0% -0.73% (p=0.000 n=10+9)
FileAndServer_16MB/NoTLS-4 25.4ms ± 5% 3.8ms ± 5% -84.99% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-4 33.3ms ± 2% 13.5ms ± 3% -59.35% (p=0.000 n=9+10)
FileAndServer_64MB/NoTLS-4 105ms ± 2% 16ms ± 1% -84.38% (p=0.000 n=9+9)
FileAndServer_64MB/TLS-4 128ms ± 2% 54ms ± 2% -58.18% (p=0.000 n=10+10)
name old speed new speed delta
FileAndServer_1KB/NoTLS-4 19.2MB/s ± 0% 19.2MB/s ± 0% -0.21% (p=0.018 n=10+9)
FileAndServer_1KB/TLS-4 16.7MB/s ± 0% 16.8MB/s ± 0% +0.72% (p=0.000 n=10+9)
FileAndServer_16MB/NoTLS-4 660MB/s ± 5% 4400MB/s ± 6% +566.83% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-4 504MB/s ± 2% 1241MB/s ± 2% +146.03% (p=0.000 n=9+10)
FileAndServer_64MB/NoTLS-4 640MB/s ± 2% 4089MB/s ± 2% +538.71% (p=0.000 n=9+10)
FileAndServer_64MB/TLS-4 524MB/s ± 2% 1254MB/s ± 2% +139.15% (p=0.000 n=10+10)
name old alloc/op new alloc/op delta
FileAndServer_1KB/NoTLS-4 4.94kB ± 0% 5.03kB ± 0% +1.82% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-4 5.07kB ± 0% 6.11kB ± 0% +20.43% (p=0.000 n=8+10)
FileAndServer_16MB/NoTLS-4 5.93kB ± 8% 5.10kB ± 0% -14.06% (p=0.000 n=10+8)
FileAndServer_16MB/TLS-4 141kB ± 1% 74kB ± 0% -47.53% (p=0.000 n=10+10)
FileAndServer_64MB/NoTLS-4 9.66kB ±22% 5.59kB ± 1% -42.09% (p=0.000 n=10+10)
FileAndServer_64MB/TLS-4 551kB ± 0% 184kB ± 0% -66.59% (p=0.000 n=9+9)
name old allocs/op new allocs/op delta
FileAndServer_1KB/NoTLS-4 66.0 ± 0% 70.0 ± 0% +6.06% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-4 70.0 ± 0% 71.0 ± 0% +1.43% (p=0.000 n=10+10)
FileAndServer_16MB/NoTLS-4 67.6 ± 1% 70.0 ± 0% +3.55% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-4 4.19k ± 0% 1.11k ± 0% -73.59% (p=0.000 n=10+10)
FileAndServer_64MB/NoTLS-4 76.3 ± 5% 71.0 ± 0% -6.95% (p=0.000 n=10+10)
FileAndServer_64MB/TLS-4 16.6k ± 0% 4.2k ± 0% -74.44% (p=0.000 n=10+8)
darwin/amd64:
name old time/op new time/op delta
FileAndServer_1KB/NoTLS-8 91.5µs ± 2% 96.8µs ± 8% +5.76% (p=0.010 n=10+9)
FileAndServer_1KB/TLS-8 98.9µs ± 2% 98.9µs ± 2% ~ (p=0.968 n=10+9)
FileAndServer_16MB/NoTLS-8 80.3ms ±20% 9.5ms ± 2% -88.19% (p=0.000 n=9+9)
FileAndServer_16MB/TLS-8 54.3ms ±12% 17.6ms ± 4% -67.52% (p=0.000 n=9+10)
FileAndServer_64MB/NoTLS-8 406ms ±19% 39ms ± 3% -90.45% (p=0.000 n=10+9)
FileAndServer_64MB/TLS-8 205ms ± 6% 71ms ± 3% -65.27% (p=0.000 n=8+10)
name old speed new speed delta
FileAndServer_1KB/NoTLS-8 11.2MB/s ± 2% 10.6MB/s ± 9% -5.30% (p=0.010 n=10+9)
FileAndServer_1KB/TLS-8 10.4MB/s ± 2% 10.4MB/s ± 2% ~ (p=0.889 n=10+9)
FileAndServer_16MB/NoTLS-8 211MB/s ±17% 1770MB/s ± 2% +739.26% (p=0.000 n=9+9)
FileAndServer_16MB/TLS-8 302MB/s ±26% 952MB/s ± 4% +214.93% (p=0.000 n=10+10)
FileAndServer_64MB/NoTLS-8 167MB/s ±17% 1729MB/s ± 3% +938.05% (p=0.000 n=10+9)
FileAndServer_64MB/TLS-8 316MB/s ±15% 944MB/s ± 3% +198.59% (p=0.000 n=10+10)
name old alloc/op new alloc/op delta
FileAndServer_1KB/NoTLS-8 4.96kB ± 0% 6.03kB ± 0% +21.48% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-8 5.10kB ± 0% 6.15kB ± 0% +20.59% (p=0.000 n=10+10)
FileAndServer_16MB/NoTLS-8 7.70kB ± 1% 38.64kB ± 0% +401.51% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-8 137kB ± 2% 76kB ± 0% -44.73% (p=0.000 n=10+9)
FileAndServer_64MB/NoTLS-8 24.1kB ±49% 40.0kB ± 2% +66.24% (p=0.000 n=10+10)
FileAndServer_64MB/TLS-8 530kB ± 1% 189kB ± 1% -64.42% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
FileAndServer_1KB/NoTLS-8 66.0 ± 0% 68.0 ± 0% +3.03% (p=0.000 n=10+10)
FileAndServer_1KB/TLS-8 70.0 ± 0% 71.0 ± 0% +1.43% (p=0.000 n=10+10)
FileAndServer_16MB/NoTLS-8 71.6 ± 1% 69.0 ± 0% -3.63% (p=0.000 n=10+10)
FileAndServer_16MB/TLS-8 3.96k ± 2% 1.13k ± 0% -71.34% (p=0.000 n=10+9)
FileAndServer_64MB/NoTLS-8 105 ±22% 73 ± 3% -30.61% (p=0.000 n=10+10)
FileAndServer_64MB/TLS-8 15.7k ± 0% 4.3k ± 0% -72.40% (p=0.000 n=10+10)
Updates #30377.1 parent 9a71015 commit 46a2402
2 files changed
Lines changed: 222 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 | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| |||
90 | 94 | | |
91 | 95 | | |
92 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
0 commit comments