From 63a9bd6bb55f69369133c4fa2cbc0966459ca846 Mon Sep 17 00:00:00 2001 From: Iskander Sharipov Date: Thu, 6 Jan 2022 18:43:28 +0300 Subject: [PATCH] pkg/result/processors: compile nolint regexp only once `regexp.MatchString` compiles regexp for every invocation, there is no pattern caching there. This change introduces a precompiled regexp to save some time while checking comments for nolint directives. Found using go-perfguard with cpu profile collected while running golangci-lint on itself. --- pkg/result/processors/nolint.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index c8c44838e3e1..12ae0fc2d546 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -17,6 +17,7 @@ import ( ) var nolintDebugf = logutils.Debug("nolint") +var nolintRe = regexp.MustCompile(`^nolint( |:|$)`) type ignoredRange struct { linters []string @@ -234,7 +235,7 @@ func (p *Nolint) extractFileCommentsInlineRanges(fset *token.FileSet, comments . func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *token.FileSet) *ignoredRange { text = strings.TrimLeft(text, "/ ") - if ok, _ := regexp.MatchString(`^nolint( |:|$)`, text); !ok { + if !nolintRe.MatchString(text) { return nil }