-
Notifications
You must be signed in to change notification settings - Fork 461
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: remove useless code from tree #793
base: develop
Are you sure you want to change the base?
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## develop #793 +/- ##
===========================================
+ Coverage 82.06% 82.09% +0.03%
===========================================
Files 97 97
Lines 9639 9639
===========================================
+ Hits 7910 7913 +3
+ Misses 1254 1252 -2
+ Partials 475 474 -1
☔ View full report in Codecov by Sentry. |
thanks |
zh: 修改后, 寻找 any 节点的速度比之前快了一倍 BenchMark code func BenchmarkFindAnyParam(b *testing.B) {
var isNil bool
tree := &router{method: "GET", root: &node{}}
routes := [...]string{
"/hi/hello/test/*any",
"/b",
"/ABC",
"/search/*query",
"/src/*filepath",
"/x",
"/x/y",
"/y/z",
"/1/:id/2",
"/aa",
"/a/*any",
"/doc",
"/doc/go1.html",
"/doc/go/away",
"/no/a",
"/no/b",
"/z/:age",
"/x/3/4",
"/x/:age/5",
}
for _, route := range routes {
recv := catchPanic(func() {
tree.addRoute(route, fakeHandler(route))
})
if recv != nil {
b.Fatalf("panic inserting route '%s': %v", route, recv)
}
}
v := make(param.Params, 0, 10)
b.ResetTimer()
for n := 0; n < b.N; n++ {
value := tree.find("/hi/hello/test/sth", &v, false)
if value.handlers == nil {
isNil = true
} else {
isNil = false
}
}
b.StopTimer()
assert.DeepEqual(b, isNil, false)
} before: goos: linux
goarch: amd64
pkg: github.com/cloudwego/hertz/pkg/route
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
BenchmarkFindAnyParam
BenchmarkFindAnyParam-2 26044662 49.24 ns/op
PASS after change: goos: linux
goarch: amd64
pkg: github.com/cloudwego/hertz/pkg/route
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
BenchmarkFindAnyParam
BenchmarkFindAnyParam-2 49172884 23.27 ns/op
PASS |
pkg/route/tree.go
Outdated
@@ -441,7 +439,7 @@ func (r *router) find(path string, paramsPointer *param.Params, unescape bool) ( | |||
} | |||
} | |||
|
|||
(*paramsPointer)[index].Value = bytesconv.B2s(append(buf, val...)) | |||
(*paramsPointer)[index].Value = val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不能动,这个对上层暴露的是 string,但是是 unsafe string,如果底层 byte 变了,会导致 string 变的,而且string用户都没法拷贝。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一行虽然有性能提升,不过不能动,之前特地改成这样的
What type of PR is this?
optimize
Check the PR title.
<type>(optional scope): <description>
.(Optional) Translate the PR title into Chinese.
去除 tree 中 find AnyParam 节点中无意义的代码
(Optional) More detail description for this PR(en: English/zh: Chinese).
zh: 修改后, 寻找 any 节点的速度比之前快了一倍
BenchMark code
before:
after change: