Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http: optimize memory usage after hijacking and handler exits
Avoid holding on to the "connReader" after hijacking. This allows all of the "conn", "response" and "Request" to be garbage collected after the ServeHTTP handler exits. Use a weak-ref on the "response" to allow CloseNotify to work _before_ the handler has exited (i.e. while the "response" is still referenced in "conn.serve"). This aligns with the documentation of CloseNotifier: > After the Handler has returned, there is no guarantee that the channel > receives a value. goos: linux goarch: amd64 pkg: net/http cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz │ lazy-notify │ reader │ │ sec/op │ sec/op vs base │ Server-8 507.0µ ± 1% 499.5µ ± 2% -1.48% (p=0.002 n=10) ServerHijack-8 44.09µ ± 29% 44.56µ ± 27% ~ (p=0.529 n=10) ServerHijackMemoryUsage-8 1.181m ± 15% 1.104m ± 9% ~ (p=0.143 n=10) geomean 297.7µ 290.7µ -2.36% │ lazy-notify │ reader │ │ B/op │ B/op vs base │ Server-8 2.185Ki ± 0% 2.188Ki ± 0% +0.13% (p=0.014 n=10) ServerHijack-8 12.03Ki ± 0% 12.09Ki ± 0% +0.45% (p=0.000 n=10) ServerHijackMemoryUsage-8 11.51Ki ± 0% 11.57Ki ± 0% +0.50% (p=0.000 n=10) geomean 6.714Ki 6.738Ki +0.36% │ lazy-notify │ reader │ │ allocs/op │ allocs/op vs base │ Server-8 20.00 ± 0% 20.00 ± 0% ~ (p=1.000 n=10) ¹ ServerHijack-8 49.00 ± 0% 51.00 ± 0% +4.08% (p=0.000 n=10) ServerHijackMemoryUsage-8 45.00 ± 0% 47.00 ± 0% +4.44% (p=0.000 n=10) geomean 35.33 36.33 +2.82% ¹ all samples are equal │ lazy-notify │ reader │ │ retained_B/op │ retained_B/op vs base │ ServerHijackMemoryUsage-8 11.339k ± 0% 8.714k ± 0% -23.15% (p=0.000 n=10)
- Loading branch information