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: remove useless code from tree #793

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Skyenought
Copy link
Contributor

@Skyenought Skyenought commented May 26, 2023

What type of PR is this?

optimize

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.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo.

(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

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

@Skyenought Skyenought requested review from a team as code owners May 26, 2023 05:00
@codecov
Copy link

codecov bot commented May 26, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03% 🎉

Comparison is base (fecbe81) 82.06% compared to head (d30e299) 82.09%.

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

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     
Files Changed Coverage Δ
pkg/route/tree.go 96.68% <ø> (ø)
pkg/route/engine.go 60.54% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@li-jin-gou
Copy link
Member

thanks

pkg/route/tree.go Outdated Show resolved Hide resolved
@Skyenought
Copy link
Contributor Author

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

@welkeyever

@@ -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
Copy link
Member

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用户都没法拷贝。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行虽然有性能提升,不过不能动,之前特地改成这样的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants