Skip to content

Commit

Permalink
perf: avoid allocations with (*regexp.Regexp).MatchString
Browse files Browse the repository at this point in the history
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 := looksLikeUrl.Match([]byte("https://www.example.com")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := looksLikeUrl.MatchString("https://www.example.com"); !match {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/bearer/bearer/internal/parser/interfaces/paths
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 1788562	       812.1 ns/op	      24 B/op	       1 allocs/op
BenchmarkMatchString-16    	 2106370	       517.8 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/bearer/bearer/internal/parser/interfaces/paths	3.628s

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Oct 20, 2023
1 parent 859ece2 commit bb6c6ce
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/detectors/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func filterCaptures(params []config.Param, captures []parser.Captures) (filtered

paramContent := capture[param.BuildFullName()].Content()

if !regex.Match([]byte(paramContent)) {
if !regex.MatchString(paramContent) {
shouldIgnore = true
break
}
Expand Down
4 changes: 2 additions & 2 deletions internal/detectors/sql/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ConvertToSimpleType(value string) string {
numberMap := []string{"bit", `tinyint(\(\d?\))?`, "smallint", "mediumint", "int", "integer", "bigint", `float(\(\d?,\d?\))`, `double(\(\d?,\d?\))?`, `decimal(\(\d?,\d?\))`, `dec`}
for _, typeValue := range numberMap {
reg := regexp.MustCompile(typeValue)
if reg.Match([]byte(simplified)) {
if reg.MatchString(simplified) {
return schema.SimpleTypeNumber
}
}
Expand All @@ -33,7 +33,7 @@ func ConvertToSimpleType(value string) string {
stringMap := []string{`char(\(\d?\))?`, `varchar(\(\d?\))?`, `character(\(\d?\))?`, "tinytext", "mediumtext", "longtext"}
for _, typeValue := range stringMap {
reg := regexp.MustCompile(typeValue)
if reg.Match([]byte(simplified)) {
if reg.MatchString(simplified) {
return schema.SimpleTypeString
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/git/defunct_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func cleanupDefunct() {
lines := strings.Split(string(stdout), "\n")

for _, line := range lines {
if !regexpDefunctProcess.Match([]byte(line)) {
if !regexpDefunctProcess.MatchString(line) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions internal/parser/interfaces/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func ValueIsRelevant(value *values.Value) bool {
return false
}

if looksLikeUrl.Match([]byte(text)) {
if looksLikeUrl.MatchString(text) {
return false
}

if !hasNormalChars.Match([]byte(text)) {
if !hasNormalChars.MatchString(text) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parser/interfaces/urls/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func textIsRelevant(value *values.Value) bool {
return false
}

if !hasUsefulInformation.Match([]byte(text)) {
if !hasUsefulInformation.MatchString(text) {
return false
}

Expand Down

0 comments on commit bb6c6ce

Please sign in to comment.