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

perf: avoid allocations with (*regexp.Regexp).MatchString #1344

Merged
merged 1 commit into from
Oct 20, 2023
Merged

perf: avoid allocations with (*regexp.Regexp).MatchString #1344

merged 1 commit into from
Oct 20, 2023

Conversation

Juneezee
Copy link
Contributor

Description

We should use (*regexp.Regexp).MatchString instead of (*regexp.Regexp).Match([]byte(...)) when matching string to avoid unnecessary []byte conversions and reduce allocations.

Example benchmark:

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := looksLikeUrl.Match([]byte("https://www.example.com")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := looksLikeUrl.MatchString("https://www.example.com"); !match {
			b.Fail()
		}
	}
}

Result:

goos: linux
goarch: amd64
pkg: github.com/bearer/bearer/internal/parser/interfaces/paths cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 1788562	       812.1 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 2106370	       517.8 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/bearer/bearer/internal/parser/interfaces/paths	3.628s

Checklist

  • I've added test coverage that shows my fix or feature works as expected.
  • I've updated or added documentation if required.
  • I've included usage information in the description if CLI behavior was updated or added.
  • PR title follows Conventional Commits format

We should use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` when matching string to avoid
unnecessary `[]byte` conversions and reduce allocations.

Example benchmark:

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := looksLikeUrl.Match([]byte("https://www.example.com")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := looksLikeUrl.MatchString("https://www.example.com"); !match {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/bearer/bearer/internal/parser/interfaces/paths
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 1788562	       812.1 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 2106370	       517.8 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/bearer/bearer/internal/parser/interfaces/paths	3.628s

Signed-off-by: Eng Zer Jun <[email protected]>
@cfabianski
Copy link
Collaborator

Thanks @Juneezee will wait for the CI and merge ❤️

@cfabianski cfabianski merged commit 43c970c into Bearer:main Oct 20, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants