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

Avoid allocations with (*regexp.Regexp).MatchString #415

Merged
merged 1 commit into from
Nov 5, 2023
Merged

Avoid allocations with (*regexp.Regexp).MatchString #415

merged 1 commit into from
Nov 5, 2023

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Nov 5, 2023

We should use (*regexp.Regexp).MatchString instead of (*regexp.Regexp).Match([]byte(...)) when matching string to avoid unnecessary []byte conversions and reduce allocations. A one-line change for free performance improvement.

Example benchmark:

var excludeRegex = regexp.MustCompile("vendor")

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := excludeRegex.Match([]byte("vendor/pkg/main.go")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := excludeRegex.MatchString("vendor/pkg/main.go"); !match {
			b.Fail()
		}
	}
}

Result:

goos: linux
goarch: amd64
pkg: github.com/boyter/scc/v3/processor
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4123592	       274.9 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 7006401	       145.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/boyter/scc/v3/processor	2.637s

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:

var excludeRegex = regexp.MustCompile("vendor")

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := excludeRegex.Match([]byte("vendor/pkg/main.go")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := excludeRegex.MatchString("vendor/pkg/main.go"); !match {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/boyter/scc/v3/processor
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4123592	       274.9 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 7006401	       145.1 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/boyter/scc/v3/processor	2.637s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@boyter
Copy link
Owner

boyter commented Nov 5, 2023

Oh yes very nice catch there. Thanks for submitting.

@boyter boyter merged commit 32a7e80 into boyter:master Nov 5, 2023
1 check 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