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

refactor(tmpl): avoid unnecessary byte/string conversion #4356

Merged
merged 1 commit into from Oct 10, 2023
Merged

refactor(tmpl): avoid unnecessary byte/string conversion #4356

merged 1 commit into from Oct 10, 2023

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Oct 8, 2023

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

Benchmark:

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

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

Result:

goos: linux
goarch: amd64
pkg: github.com/goreleaser/goreleaser/internal/tmpl cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4320873	       381.2 ns/op	      16 B/op	       1 allocs/op
BenchmarkMatchString-16    	 5973543	       203.9 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/goreleaser/goreleaser/internal/tmpl	3.366s

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

Example benchmark:

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

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

goos: linux
goarch: amd64
pkg: github.com/goreleaser/goreleaser/internal/tmpl
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 4320873	       381.2 ns/op	      16 B/op	       1 allocs/op
BenchmarkMatchString-16    	 5973543	       203.9 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/goreleaser/goreleaser/internal/tmpl	3.366s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Oct 8, 2023
@codecov
Copy link

codecov bot commented Oct 8, 2023

Codecov Report

Merging #4356 (0da5c92) into main (c43a50c) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #4356      +/-   ##
==========================================
- Coverage   83.88%   83.88%   -0.01%     
==========================================
  Files         133      133              
  Lines       12703    12702       -1     
==========================================
- Hits        10656    10655       -1     
  Misses       1630     1630              
  Partials      417      417              
Files Coverage Δ
internal/tmpl/tmpl.go 98.45% <100.00%> (-0.01%) ⬇️

@caarlos0
Copy link
Member

awesome, thanks!

@caarlos0 caarlos0 merged commit 37e3fde into goreleaser:main Oct 10, 2023
10 of 11 checks passed
@caarlos0 caarlos0 added the enhancement New feature or request label Oct 10, 2023
@github-actions github-actions bot added this to the v1.22.0 milestone Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants