Skip to content
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

goat: avoid unnecessary byte/string conversion #717

Merged
merged 1 commit into from
Jun 27, 2023
Merged

goat: avoid unnecessary byte/string conversion #717

merged 1 commit into from
Jun 27, 2023

Conversation

Juneezee
Copy link
Contributor

We can use (*regexp.Regexp).MatchString instead of (*regexp.Regexp).Match([]byte(...)) to avoid unnecessary []byte conversions and reduce allocations.

Example benchmark:

package main_test

import (
	"regexp"
	"testing"
)

var testRegex = regexp.MustCompile(`\w+`)

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

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

Result:

goos: linux
goarch: amd64
pkg: github.com/zhenghaoz/gorse/cmd/goat
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	11826721	       138.9 ns/op	       3 B/op	       1 allocs/op
BenchmarkMatchString-16    	12954583	        88.85 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/zhenghaoz/gorse/cmd/goat	3.843s

We can use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` to avoid unnecessary `[]byte`
conversions and reduce allocations.

Example benchmark:

var testRegex = regexp.MustCompile(`\w+`)

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

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

goos: linux
goarch: amd64
pkg: github.com/zhenghaoz/gorse/cmd/goat
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	11826721	       138.9 ns/op	       3 B/op	       1 allocs/op
BenchmarkMatchString-16    	12954583	        88.85 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/zhenghaoz/gorse/cmd/goat	3.843s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@codecov
Copy link

codecov bot commented Jun 27, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (3528e93) 70.10% compared to head (846892a) 70.10%.

❗ Current head 846892a differs from pull request most recent head c36bfd5. Consider uploading reports for the commit c36bfd5 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #717   +/-   ##
=======================================
  Coverage   70.10%   70.10%           
=======================================
  Files          64       64           
  Lines       12238    12238           
=======================================
  Hits         8579     8579           
  Misses       2875     2875           
  Partials      784      784           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@zhenghaoz zhenghaoz merged commit 8301e9e into gorse-io:master Jun 27, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants