hertz icon indicating copy to clipboard operation
hertz copied to clipboard

optimize: remove useless code from tree

Open Skyenought opened this issue 2 years ago • 3 comments

What type of PR is this?

optimize

Check the PR title.

  • [x] This PR title match the format: <type>(optional scope): <description>.
  • [x] 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 avatar May 26 '23 05:05 Skyenought

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 73.68%. Comparing base (e8af33f) to head (70a8f78). Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #793   +/-   ##
========================================
  Coverage    73.68%   73.68%           
========================================
  Files          145      145           
  Lines        16860    16860           
========================================
  Hits         12423    12423           
  Misses        3848     3848           
  Partials       589      589           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 26 '23 05:05 codecov[bot]

thanks

li-jin-gou avatar May 26 '23 07:05 li-jin-gou

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

Skyenought avatar May 29 '23 09:05 Skyenought