Skip to content

Commit 3e613b2

Browse files
perf(scanner): hoist parser regexps to package level
Compile the five parser path patterns once at package init instead of recompiling them in each parser constructor on every repo scan. BenchmarkNewMemParsers: 9673ns→57ns (169x), 30424B→120B, 255→6 allocs. Signed-off-by: Matías Insaurralde <matias@insaurral.de>
1 parent 2593420 commit 3e613b2

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

scanner/parsers.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import (
1111
"gopkg.in/yaml.v3"
1212
)
1313

14+
// Parser path patterns are stateless and safe for concurrent use, so they are
15+
// compiled once at package init rather than per-parser-construction (which used
16+
// to recompile all five on every repo scan).
17+
var (
18+
githubActionsMetadataPattern = regexp.MustCompile(`(^|/)action\.ya?ml$`)
19+
githubWorkflowPattern = regexp.MustCompile(`^\.github/workflows/[^/]+\.ya?ml$`)
20+
azurePipelinesPattern = regexp.MustCompile(`\.?azure-pipelines(-.+)?\.ya?ml$`)
21+
gitlabCiPattern = regexp.MustCompile(`\.?gitlab-ci(-.+)?\.ya?ml$`)
22+
tektonPattern = regexp.MustCompile(`^\.tekton/[^/]+\.ya?ml$`)
23+
)
24+
1425
type GithubActionsMetadataParser struct {
1526
pattern *regexp.Regexp
1627
}
1728

1829
func NewGithubActionsMetadataParser() *GithubActionsMetadataParser {
1930
return &GithubActionsMetadataParser{
20-
pattern: regexp.MustCompile(`(^|/)action\.ya?ml$`),
31+
pattern: githubActionsMetadataPattern,
2132
}
2233
}
2334

@@ -63,7 +74,7 @@ type GithubActionWorkflowParser struct {
6374

6475
func NewGithubActionWorkflowParser() *GithubActionWorkflowParser {
6576
return &GithubActionWorkflowParser{
66-
pattern: regexp.MustCompile(`^\.github/workflows/[^/]+\.ya?ml$`),
77+
pattern: githubWorkflowPattern,
6778
}
6879
}
6980

@@ -108,7 +119,7 @@ type AzurePipelinesParser struct {
108119

109120
func NewAzurePipelinesParser() *AzurePipelinesParser {
110121
return &AzurePipelinesParser{
111-
pattern: regexp.MustCompile(`\.?azure-pipelines(-.+)?\.ya?ml$`),
122+
pattern: azurePipelinesPattern,
112123
}
113124
}
114125

@@ -154,7 +165,7 @@ type GitlabCiParser struct {
154165

155166
func NewGitlabCiParser() *GitlabCiParser {
156167
return &GitlabCiParser{
157-
pattern: regexp.MustCompile(`\.?gitlab-ci(-.+)?\.ya?ml$`),
168+
pattern: gitlabCiPattern,
158169
}
159170
}
160171

@@ -228,7 +239,7 @@ type PipelineAsCodeTektonParser struct {
228239

229240
func NewPipelineAsCodeTektonParser() *PipelineAsCodeTektonParser {
230241
return &PipelineAsCodeTektonParser{
231-
pattern: regexp.MustCompile(`^\.tekton/[^/]+\.ya?ml$`),
242+
pattern: tektonPattern,
232243
}
233244
}
234245

0 commit comments

Comments
 (0)