-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
#3881
Conversation
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 := branchRegex.Match([]byte("main")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := branchRegex.MatchString("main"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/runatlantis/atlantis/server/core/config cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 8269699 141.4 ns/op 4 B/op 1 allocs/op BenchmarkMatchString-16 14298446 95.81 ns/op 0 B/op 0 allocs/op PASS ok github.com/runatlantis/atlantis/server/core/config 2.784s Signed-off-by: Eng Zer Jun <[email protected]>
Fwiw I agree with this change. I see some references online that also point to a performance gain
|
/cherry-pick release-0.26 |
@Juneezee Thanks for the contribution |
…tis#3881) 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 := branchRegex.Match([]byte("main")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := branchRegex.MatchString("main"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/runatlantis/atlantis/server/core/config cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 8269699 141.4 ns/op 4 B/op 1 allocs/op BenchmarkMatchString-16 14298446 95.81 ns/op 0 B/op 0 allocs/op PASS ok github.com/runatlantis/atlantis/server/core/config 2.784s Signed-off-by: Eng Zer Jun <[email protected]> Co-authored-by: PePe Amengual <[email protected]>
…tis#3881) 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 := branchRegex.Match([]byte("main")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := branchRegex.MatchString("main"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/runatlantis/atlantis/server/core/config cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 8269699 141.4 ns/op 4 B/op 1 allocs/op BenchmarkMatchString-16 14298446 95.81 ns/op 0 B/op 0 allocs/op PASS ok github.com/runatlantis/atlantis/server/core/config 2.784s Signed-off-by: Eng Zer Jun <[email protected]> Co-authored-by: PePe Amengual <[email protected]>
…tis#3881) 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 := branchRegex.Match([]byte("main")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := branchRegex.MatchString("main"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/runatlantis/atlantis/server/core/config cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 8269699 141.4 ns/op 4 B/op 1 allocs/op BenchmarkMatchString-16 14298446 95.81 ns/op 0 B/op 0 allocs/op PASS ok github.com/runatlantis/atlantis/server/core/config 2.784s Signed-off-by: Eng Zer Jun <[email protected]> Co-authored-by: PePe Amengual <[email protected]>
what
Replace
(*regexp.Regexp).Match([]byte(...))
with(*regexp.Regexp).MatchString
why
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 gain.Example benchmark:
tests
references