From 8188642cb3ec5615c1802dceec3534ddce0a021b Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Thu, 16 Nov 2023 01:00:00 +0800 Subject: [PATCH] perf: avoid allocations with `(*regexp.Regexp).MatchString` (#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 Co-authored-by: PePe Amengual --- server/core/config/parser_validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/core/config/parser_validator.go b/server/core/config/parser_validator.go index eed480e09b..0a37caaccc 100644 --- a/server/core/config/parser_validator.go +++ b/server/core/config/parser_validator.go @@ -78,7 +78,7 @@ func (p *ParserValidator) ParseRepoCfgData(repoCfgData []byte, globalCfg valid.G // i := 0 for _, p := range validConfig.Projects { - if branch == "" || p.BranchRegex == nil || p.BranchRegex.Match([]byte(branch)) { + if branch == "" || p.BranchRegex == nil || p.BranchRegex.MatchString(branch) { validConfig.Projects[i] = p i++ }