From a0751f8444dcbb95f8a1465d4e0b7634f02025bc Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 9 Oct 2026 11:33:16 +0200 Subject: [PATCH] Avoid redundant per-call work in reporter and phpdoc typo fixer relativePath called os.Getwd() for every reported file; resolve the cwd once before the loop and pass it in. fpntFixTypos recompiled its @param pattern on every call; memoize the per-typo-name regex. --- blink/internal/fixer/rules/symplify_docblock3.go | 16 +++++++++++++++- blink/internal/reporter/reporter.go | 15 +++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/blink/internal/fixer/rules/symplify_docblock3.go b/blink/internal/fixer/rules/symplify_docblock3.go index b6e24a9782..e0888e154f 100644 --- a/blink/internal/fixer/rules/symplify_docblock3.go +++ b/blink/internal/fixer/rules/symplify_docblock3.go @@ -4,11 +4,25 @@ import ( "regexp" "sort" "strings" + "sync" "blink/internal/token" "blink/internal/tokens" ) +// fpntTypoReCache memoizes the per-typo-name @param patterns, which are +// otherwise recompiled on every fpntFixTypos call. Keyed by typo name. +var fpntTypoReCache sync.Map + +func fpntTypoRe(typoName string) *regexp.Regexp { + if re, ok := fpntTypoReCache.Load(typoName); ok { + return re.(*regexp.Regexp) + } + re := regexp.MustCompile(`@param(.*?)(` + regexp.QuoteMeta(typoName) + `\b)`) + fpntTypoReCache.Store(typoName, re) + return re +} + // --- FixParamNameTypo: correct a @param variable name to match the real argument --- type FixParamNameTypo struct{} @@ -132,7 +146,7 @@ func fpntFixTypos(argumentNames []string, missArg, paramNames map[int]string, co if !ok { continue } - re := regexp.MustCompile(`@param(.*?)(` + regexp.QuoteMeta(typoName) + `\b)`) + re := fpntTypoRe(typoName) content = replaceFirstFunc(content, re, func(groups []string) string { paramName := groups[2] if done, exists := replaced[paramName]; exists && !done { diff --git a/blink/internal/reporter/reporter.go b/blink/internal/reporter/reporter.go index 1875f0824c..a5f09cef87 100644 --- a/blink/internal/reporter/reporter.go +++ b/blink/internal/reporter/reporter.go @@ -84,11 +84,10 @@ func newFormatter(w io.Writer) formatter { return formatter{w: w, width: width, color: color} } -// relativePath resolves an absolute file path against the current working -// directory, matching ECS's StaticRelativeFilePathHelper output. -func relativePath(path string) string { - cwd, err := os.Getwd() - if err != nil { +// relativePath resolves an absolute file path against cwd, matching ECS's +// StaticRelativeFilePathHelper output. An empty cwd yields the path unchanged. +func relativePath(cwd, path string) string { + if cwd == "" { return path } rel, err := filepath.Rel(cwd, path) @@ -104,9 +103,13 @@ func (f formatter) reportFileDiffs(results []runner.FileResult) { } f.newLine(1) + cwd, err := os.Getwd() + if err != nil { + cwd = "" + } for i, r := range results { f.newLine(2) - f.writeln(f.bold(fmt.Sprintf("%d) %s", i+1, relativePath(r.Path)))) + f.writeln(f.bold(fmt.Sprintf("%d) %s", i+1, relativePath(cwd, r.Path)))) f.newLine(1) f.writeln(f.formatDiff(r.Diff)) f.newLine(1)