Skip to content
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

optimize: pooled HeaderScanner and HeaderValueScanner #284

Closed
wants to merge 0 commits into from

Conversation

Haswf
Copy link
Contributor

@Haswf Haswf commented Oct 4, 2022

What type of PR is this?

optimize: pooled HeaderScanner and HeaderValueScanner

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.

(Optional) Translate the PR title into Chinese.

optimize: 池化 HeaderScanner and HeaderValueScanner

(Optional) More detail description for this PR(en: English/zh: Chinese).

en: use sync.Pool to recycle HeaderScanner and HeaderValueScanner. It also contains unit tests to ensure scanner has been properly reset.
zh(optional):

Which issue(s) this PR fixes:

implements #227

@Haswf
Copy link
Contributor Author

Haswf commented Oct 4, 2022

Idk if the performance optimization is worth the additional complexity introduced by pooling. We need benchmark to measure and justify performance improvement.

@codecov
Copy link

codecov bot commented Oct 5, 2022

Codecov Report

Patch coverage: 90.00% and project coverage change: -7.71 ⚠️

Comparison is base (bec7f37) 75.62% compared to head (0a41104) 67.92%.

❗ Current head 0a41104 differs from pull request most recent head f5899f3. Consider uploading reports for the commit f5899f3 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #284      +/-   ##
===========================================
- Coverage    75.62%   67.92%   -7.71%     
===========================================
  Files           96       89       -7     
  Lines         9451     8635     -816     
===========================================
- Hits          7147     5865    -1282     
- Misses        1857     2397     +540     
+ Partials       447      373      -74     
Impacted Files Coverage Δ
pkg/protocol/http1/ext/headerscanner.go 57.85% <87.50%> (+3.76%) ⬆️
pkg/protocol/http1/req/header.go 66.41% <100.00%> (+0.97%) ⬆️
pkg/protocol/http1/resp/header.go 79.03% <100.00%> (-0.34%) ⬇️

... and 45 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@li-jin-gou li-jin-gou linked an issue Oct 6, 2022 that may be closed by this pull request
@li-jin-gou
Copy link
Member

CI need to solve

@Haswf
Copy link
Contributor Author

Haswf commented Oct 8, 2022

Some unit tests in context_test.go failed when executed locally on go1.18.5. Is it relevant to this pr?

❯ go test ./...
? github.com/cloudwego/hertz [no test files]
? github.com/cloudwego/hertz/examples/html_rendering [no test files]
? github.com/cloudwego/hertz/examples/standard [no test files]
ok github.com/cloudwego/hertz/internal/bytesconv (cached)
? github.com/cloudwego/hertz/internal/bytestr [no test files]
? github.com/cloudwego/hertz/internal/nocopy [no test files]
ok github.com/cloudwego/hertz/internal/stats (cached)
2022/10/08 22:01:30.296245 fs.go:858: [Error] HERTZ: Cannot open file="./file", error=open ./file: no such file or directory
--- FAIL: TestContextRenderFileFromFS (0.00s)
context_test.go:405: assertion failed, unexpected: text/x-go; charset=utf-8, expected: text/plain; charset=utf-8
2022/10/08 22:01:30.296330 fs.go:858: [Error] HERTZ: Cannot open file="./file", error=open ./file: no such file or directory
--- FAIL: TestContextRenderFile (0.00s)
context_test.go:422: assertion failed, unexpected: text/x-go; charset=utf-8, expected: text/plain; charset=utf-8
FAIL
FAIL github.com/cloudwego/hertz/pkg/app 0.049s
ok github.com/cloudwego/hertz/pkg/app/client (cached)
? github.com/cloudwego/hertz/pkg/app/client/discovery [no test files]
ok github.com/cloudwego/hertz/pkg/app/client/loadbalance (cached)
? github.com/cloudwego/hertz/pkg/app/client/retry [no test files]
ok github.com/cloudwego/hertz/pkg/app/middlewares/client/sd (cached)
ok github.com/cloudwego/hertz/pkg/app/middlewares/server/basic_auth (cached)
ok github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery (cached)
ok github.com/cloudwego/hertz/pkg/app/server (cached)
ok github.com/cloudwego/hertz/pkg/app/server/binding (cached)
? github.com/cloudwego/hertz/pkg/app/server/registry [no test files]
ok github.com/cloudwego/hertz/pkg/app/server/render (cached)
ok github.com/cloudwego/hertz/pkg/common/adaptor (cached)
ok github.com/cloudwego/hertz/pkg/common/bytebufferpool (cached)
ok github.com/cloudwego/hertz/pkg/common/compress (cached)
ok github.com/cloudwego/hertz/pkg/common/config (cached)
ok github.com/cloudwego/hertz/pkg/common/errors (cached)
? github.com/cloudwego/hertz/pkg/common/hlog [no test files]
ok github.com/cloudwego/hertz/pkg/common/stackless (cached)
? github.com/cloudwego/hertz/pkg/common/test/assert [no test files]
ok github.com/cloudwego/hertz/pkg/common/test/mock (cached)
ok github.com/cloudwego/hertz/pkg/common/timer (cached)
? github.com/cloudwego/hertz/pkg/common/tracer [no test files]
ok github.com/cloudwego/hertz/pkg/common/tracer/stats (cached)
? github.com/cloudwego/hertz/pkg/common/tracer/traceinfo [no test files]
ok github.com/cloudwego/hertz/pkg/common/ut (cached)
ok github.com/cloudwego/hertz/pkg/common/utils (cached)
ok github.com/cloudwego/hertz/pkg/network (cached)
ok github.com/cloudwego/hertz/pkg/network/dialer (cached)
ok github.com/cloudwego/hertz/pkg/network/netpoll (cached)
ok github.com/cloudwego/hertz/pkg/network/standard (cached)
ok github.com/cloudwego/hertz/pkg/protocol (cached)
? github.com/cloudwego/hertz/pkg/protocol/client [no test files]
? github.com/cloudwego/hertz/pkg/protocol/consts [no test files]
ok github.com/cloudwego/hertz/pkg/protocol/http1 (cached)
ok github.com/cloudwego/hertz/pkg/protocol/http1/ext (cached)
? github.com/cloudwego/hertz/pkg/protocol/http1/factory [no test files]
? github.com/cloudwego/hertz/pkg/protocol/http1/proxy [no test files]
ok github.com/cloudwego/hertz/pkg/protocol/http1/req (cached)
ok github.com/cloudwego/hertz/pkg/protocol/http1/resp (cached)
? github.com/cloudwego/hertz/pkg/protocol/suite [no test files]
ok github.com/cloudwego/hertz/pkg/route (cached)
? github.com/cloudwego/hertz/pkg/route/consts [no test files]
? github.com/cloudwego/hertz/pkg/route/param [no test files]
FAIL

@li-jin-gou
Copy link
Member

li-jin-gou commented Oct 9, 2022

Good job and Is it possible to add a benchmark to see the effect? @Haswf

@Haswf
Copy link
Contributor Author

Haswf commented Oct 9, 2022

Good job and Is it possible to add a benchmark to see the effect? @Haswf

Sure, I will run benchmark against current develop branch.

@welkeyever welkeyever added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 8, 2022
@welkeyever
Copy link
Member

Update this PR if there is anything new to the bench test 🙏

@CLAassistant
Copy link

CLAassistant commented Nov 22, 2022

CLA assistant check
All committers have signed the CLA.

@li-jin-gou
Copy link
Member

li-jin-gou commented Dec 28, 2022

benchmark: GOMAXPROCS=4 go1.19.1 test -bench='Benchmark(Response|Request)HeaderRead' -benchmem -benchtime=10s

old:

goos: darwin
goarch: amd64
pkg: github.com/cloudwego/hertz
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRequestHeaderRead-4    	135763893	        94.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkResponseHeaderRead-4   	122723790	        93.91 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/cloudwego/hertz	46.838s

new:

goos: darwin
goarch: amd64
pkg: github.com/cloudwego/hertz
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRequestHeaderRead-4    	100000000	       104.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkResponseHeaderRead-4   	120312861	       103.7 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/cloudwego/hertz	36.459s

It doesn't seem to be optimised, but we'll have to work out why later and not merge it for now.

@Haswf Haswf requested review from a team as code owners April 2, 2023 13:18
@welkeyever welkeyever added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 19, 2023
@Haswf Haswf closed this Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Development

Successfully merging this pull request may close these issues.

⚡ optimize: may HeaderScanner and HeaderValueScanner be pooled?
5 participants