Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: avoid allocations with
(*regexp.Regexp).MatchString
(gitleaks…
…#1283) 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 pathRegex = regexp.MustCompile(".*.yaml") func BenchmarkMatch(b *testing.B) { for i := 0; i < b.N; i++ { if match := pathRegex.Match([]byte("./config/deploy.yaml")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := pathRegex.MatchString("./config/deploy.yaml"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/zricethezav/gitleaks/v8/detect cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 2167712 657.9 ns/op 24 B/op 1 allocs/op BenchmarkMatchString-16 3275372 324.8 ns/op 0 B/op 0 allocs/op PASS ok github.com/zricethezav/gitleaks/v8/detect 3.436s Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
- Loading branch information