1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/api
Eng Zer Jun cbd45b12dc
Avoid allocations with (*regexp.Regexp).MatchString (#2679)
We should use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` when matching string to avoid
unnecessary `[]byte` conversions and reduce allocations.

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := versionRe.Match([]byte("v1")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := versionRe.MatchString("v1"); !match {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: go-micro.dev/v4/api/handler/event
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	11430127	       127.4 ns/op	       2 B/op	       1 allocs/op
BenchmarkMatchString-16    	12220628	        97.54 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	go-micro.dev/v4/api/handler/event	3.822s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-12-04 16:00:01 +00:00
..
client fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
handler Avoid allocations with (*regexp.Regexp).MatchString (#2679) 2023-12-04 16:00:01 +00:00
internal/proto Further consolidate the libraries 2019-06-03 18:44:43 +01:00
proto regenerate all proto (#1440) 2020-03-30 21:58:32 +01:00
resolver fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
router fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
server fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
api_test.go fix: some linting issues (#2563) 2022-09-30 16:27:07 +02:00
api.go fix: struct field alignment (#2632) 2023-04-26 02:16:34 +02:00
default.go fix: some linting issues (#2563) 2022-09-30 16:27:07 +02:00
options.go fix(api): add WithRegistry option for api (#2618) 2023-03-07 17:05:25 +01:00