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

chore(performance): improve countParams #2378

Merged
merged 5 commits into from
May 17, 2020
Merged

chore(performance): improve countParams #2378

merged 5 commits into from
May 17, 2020

Conversation

appleboy
Copy link
Member

package convert

import (
	"bytes"
	"strings"
	"testing"
)

var (
	strColon = []byte(":")
	strStar  = []byte("*")
)

func countParamsOld(path string) uint16 {
	var n uint
	for i := range []byte(path) {
		switch path[i] {
		case ':', '*':
			n++
		}
	}
	return uint16(n)
}

func countParamsNew(path string) uint16 {
	var n uint
	s := strToBytes(path)
	n += uint(bytes.Count(s, strColon))
	n += uint(bytes.Count(s, strStar))
	return uint16(n)
}

var foo = strings.Repeat("/:param", 256)

func BenchmarkCountParamsOld(b *testing.B) {
	for i := 0; i < b.N; i++ {
		countParamsOld(foo)
	}
}

func BenchmarkCountParamsNew(b *testing.B) {
	for i := 0; i < b.N; i++ {
		countParamsNew(foo)
	}
}

Performance result

goos: linux
goarch: amd64
BenchmarkCountParamsOld-48    	 3000000	      1751 ns/op	       0 B/op	       0 allocs/op
BenchmarkCountParamsNew-48    	30000000	       148 ns/op	       0 B/op	       0 allocs/op

@appleboy appleboy added this to the 1.7 milestone May 17, 2020
@appleboy appleboy requested a review from thinkerou May 17, 2020 08:03
@codecov
Copy link

codecov bot commented May 17, 2020

Codecov Report

Merging #2378 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2378   +/-   ##
=======================================
  Coverage   98.63%   98.64%           
=======================================
  Files          41       41           
  Lines        2349     2359   +10     
=======================================
+ Hits         2317     2327   +10     
  Misses         18       18           
  Partials       14       14           
Impacted Files Coverage Δ
tree.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d5b9ba...ba1a3d5. Read the comment docs.

Copy link
Member

@thinkerou thinkerou left a comment

Choose a reason for hiding this comment

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

lgtm

@thinkerou
Copy link
Member

@appleboy maybe we should also commit the pr to httprouter? thanks!

@thinkerou thinkerou merged commit b4c8bf1 into master May 17, 2020
@appleboy appleboy deleted the performance branch May 17, 2020 13:16
@appleboy
Copy link
Member Author

@thinkerou Yes, I will create a new PR on https://github.com/julienschmidt/httprouter

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

Successfully merging this pull request may close these issues.

2 participants