New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/http: reusing http.Request is slower than allocating a new one #28310
Comments
/cc @bradfitz |
Thank you for filing this issue @thesyncim! Your current benchmarks unfortunately don't do an apples-to-apples comparison because in the case of 78c78
< req, err := http.NewRequest(http.MethodPost, addr, strings.NewReader("hello"))
---
> req, err := http.NewRequest(http.MethodPost, addr, nil)
83a84
> req.Body = ioutil.NopCloser(strings.NewReader("hello")) making your for i := 0; i < b.N; i++ {
req.URL = u
req.Body = ioutil.NopCloser(strings.NewReader("hello"))
resp, err := client.Do(req) and when we examine the comparisons, you'll see them almost exactly the same (except of course for the extra 3 allocations per loop in $ go test -run=^$ -bench=. -count=7
goos: darwin
goarch: amd64
pkg: github.com/odeke-em/bugs/golang/28310
BenchmarkDoNotReuseRequest-8 20721 57681 ns/op 4694 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 20332 60927 ns/op 4695 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 19497 59840 ns/op 4695 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 19916 59264 ns/op 4692 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 20248 61995 ns/op 4695 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 19990 63561 ns/op 4696 B/op 65 allocs/op
BenchmarkDoNotReuseRequest-8 19410 62359 ns/op 4696 B/op 65 allocs/op
BenchmarkReuseRequest-8 19927 61764 ns/op 4316 B/op 68 allocs/op
BenchmarkReuseRequest-8 19706 66927 ns/op 4319 B/op 68 allocs/op
BenchmarkReuseRequest-8 18552 62377 ns/op 4326 B/op 68 allocs/op
BenchmarkReuseRequest-8 20034 60995 ns/op 4319 B/op 68 allocs/op
BenchmarkReuseRequest-8 18922 62632 ns/op 4316 B/op 68 allocs/op
BenchmarkReuseRequest-8 18802 61532 ns/op 4315 B/op 68 allocs/op
BenchmarkReuseRequest-8 18019 70261 ns/op 4325 B/op 68 allocs/op and then with benchstat $ benchstat before.txt after.txt
name old time/op new time/op delta
ReuseRequest-8 60.8µs ± 5% 63.8µs ±10% ~ (p=0.097 n=7+7)
name old alloc/op new alloc/op delta
ReuseRequest-8 4.69kB ± 0% 4.32kB ± 0% -7.99% (p=0.001 n=7+7)
name old allocs/op new allocs/op delta
ReuseRequest-8 65.0 ± 0% 68.0 ± 0% +4.62% (p=0.001 n=7+7) which is the right parity and for a facet is even better with an ~8% reduction in allocations per ops. However, reusing requests is to be taken with caution, please refer to docs on how to do so properly. I'll close this issue as I've shown with the proper comparison. Please feel free however, to open if something else is up. |
Please answer these questions before submitting your issue
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
https://play.golang.org/p/okT3ECKTCiu
What did you expect to see?
the same or better performance in BenchmarkReuseRequest
What did you see instead?
lower performance and higher allocations
goos: darwin
goarch: amd64
BenchmarkDoNotReuseRequest-8 20000 79941 ns/op 4660 B/op 70 allocs/op
BenchmarkReuseRequest-8 5000 349885 ns/op 18562 B/op 157 allocs/op
PASS
System details
The text was updated successfully, but these errors were encountered: