Skip to content

Commit 3b2a2a7

Browse files
authored
Avoid allocations with (*regexp.Regexp).MatchString (#1302)
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 goTestRegExp = regexp.MustCompile(`_test\.go$`) func BenchmarkMatch(b *testing.B) { for i := 0; i < b.N; i++ { if match := goTestRegExp.Match([]byte("file_test.go")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := goTestRegExp.MatchString("file_test.go"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/onsi/ginkgo/v2/ginkgo/watch cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 5665784 314.4 ns/op 16 B/op 1 allocs/op BenchmarkMatchString-16 8481872 140.5 ns/op 0 B/op 0 allocs/op PASS ok github.com/onsi/ginkgo/v2/ginkgo/watch 4.321s Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 64b8552 commit 3b2a2a7

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

ginkgo/internal/test_suite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func suitesInDir(dir string, recurse bool) TestSuites {
226226
files, _ := os.ReadDir(dir)
227227
re := regexp.MustCompile(`^[^._].*_test\.go$`)
228228
for _, file := range files {
229-
if !file.IsDir() && re.Match([]byte(file.Name())) {
229+
if !file.IsDir() && re.MatchString(file.Name()) {
230230
suite := TestSuite{
231231
Path: relPath(dir),
232232
PackageName: packageNameForSuite(dir),
@@ -241,7 +241,7 @@ func suitesInDir(dir string, recurse bool) TestSuites {
241241
if recurse {
242242
re = regexp.MustCompile(`^[._]`)
243243
for _, file := range files {
244-
if file.IsDir() && !re.Match([]byte(file.Name())) {
244+
if file.IsDir() && !re.MatchString(file.Name()) {
245245
suites = append(suites, suitesInDir(dir+"/"+file.Name(), recurse)...)
246246
}
247247
}
@@ -272,7 +272,7 @@ func filesHaveGinkgoSuite(dir string, files []os.DirEntry) bool {
272272
reGinkgo := regexp.MustCompile(`package ginkgo|\/ginkgo"|\/ginkgo\/v2"|\/ginkgo\/v2/dsl/`)
273273

274274
for _, file := range files {
275-
if !file.IsDir() && reTestFile.Match([]byte(file.Name())) {
275+
if !file.IsDir() && reTestFile.MatchString(file.Name()) {
276276
contents, _ := os.ReadFile(dir + "/" + file.Name())
277277
if reGinkgo.Match(contents) {
278278
return true

ginkgo/watch/dependencies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (d Dependencies) resolveAndAdd(deps []string, depth int) {
7878
if err != nil {
7979
continue
8080
}
81-
if !pkg.Goroot && (!ginkgoAndGomegaFilter.Match([]byte(pkg.Dir)) || ginkgoIntegrationTestFilter.Match([]byte(pkg.Dir))) {
81+
if !pkg.Goroot && (!ginkgoAndGomegaFilter.MatchString(pkg.Dir) || ginkgoIntegrationTestFilter.MatchString(pkg.Dir)) {
8282
d.addDepIfNotPresent(pkg.Dir, depth)
8383
}
8484
}

ginkgo/watch/package_hash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ func (p *PackageHash) computeHashes() (codeHash string, codeModifiedTime time.Ti
7979
continue
8080
}
8181

82-
if goTestRegExp.Match([]byte(info.Name())) {
82+
if goTestRegExp.MatchString(info.Name()) {
8383
testHash += p.hashForFileInfo(info)
8484
if info.ModTime().After(testModifiedTime) {
8585
testModifiedTime = info.ModTime()
8686
}
8787
continue
8888
}
8989

90-
if p.watchRegExp.Match([]byte(info.Name())) {
90+
if p.watchRegExp.MatchString(info.Name()) {
9191
codeHash += p.hashForFileInfo(info)
9292
if info.ModTime().After(codeModifiedTime) {
9393
codeModifiedTime = info.ModTime()

types/code_location.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func PruneStack(fullStackTrace string, skip int) string {
149149
re := regexp.MustCompile(`\/ginkgo\/|\/pkg\/testing\/|\/pkg\/runtime\/`)
150150
for i := 0; i < len(stack)/2; i++ {
151151
// We filter out based on the source code file name.
152-
if !re.Match([]byte(stack[i*2+1])) {
152+
if !re.MatchString(stack[i*2+1]) {
153153
prunedStack = append(prunedStack, stack[i*2])
154154
prunedStack = append(prunedStack, stack[i*2+1])
155155
}

0 commit comments

Comments
 (0)