Skip to content

Commit

Permalink
transformers/grep: avoid allocations with `(*regexp.Regexp).MatchStri…
Browse files Browse the repository at this point in the history
…ng` (#1416)

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 grepRegex = regexp.MustCompile("foo.*")

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

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := grepRegex.MatchString("foo bar baz"); !match {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/johnkerl/miller/pkg/transformers
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 5700908	       210.3 ns/op	      16 B/op	       1 allocs/op
BenchmarkMatchString-16    	 8006731	       156.4 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/johnkerl/miller/pkg/transformers	2.857s

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Oct 27, 2023
1 parent 6aab161 commit 4b34f80
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/transformers/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (tr *TransformerGrep) Transform(
} else {
inrecAsString = inrec.ToDKVPString()
}
matches := tr.regexp.Match([]byte(inrecAsString))
matches := tr.regexp.MatchString(inrecAsString)
if tr.invert {
if !matches {
outputRecordsAndContexts.PushBack(inrecAndContext)
Expand Down

0 comments on commit 4b34f80

Please sign in to comment.