From 30491d707cf539e18f77a669437b77d0aecdd240 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 16:27:35 +0000 Subject: [PATCH 1/2] Initial plan From 0a36f5b9259de4ed8ad604bc0c0b83d9f88d3b17 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 16:56:09 +0000 Subject: [PATCH 2/2] fix: resolve ~50% performance regression in FindIncludesInContent Three targeted optimizations: 1. Fast-path byte check in ParseImportDirective before running regex 2. Eliminate redundant LegacyIncludeDirectivePattern regex by deriving isLegacy from captured groups of the first match 3. Replace bufio.Scanner allocation with strings.Lines in findIncludesInContent Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/cli/remove_command.go | 7 ++----- pkg/parser/import_directive.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/cli/remove_command.go b/pkg/cli/remove_command.go index d6a59ac1234..71410c9d12c 100644 --- a/pkg/cli/remove_command.go +++ b/pkg/cli/remove_command.go @@ -1,7 +1,6 @@ package cli import ( - "bufio" "fmt" "os" "path/filepath" @@ -391,9 +390,7 @@ func findIncludesInContent(content, baseDir string, verbose bool) ([]string, err _ = verbose // unused parameter for now, keeping for potential future use var includes []string - scanner := bufio.NewScanner(strings.NewReader(content)) - for scanner.Scan() { - line := scanner.Text() + for line := range strings.Lines(content) { directive := parser.ParseImportDirective(line) if directive != nil { includePath := directive.Path @@ -411,5 +408,5 @@ func findIncludesInContent(content, baseDir string, verbose bool) ([]string, err } } - return includes, scanner.Err() + return includes, nil // strings.Lines iterates over an in-memory string; no I/O errors can occur. } diff --git a/pkg/parser/import_directive.go b/pkg/parser/import_directive.go index 89a0b06c980..c365157e607 100644 --- a/pkg/parser/import_directive.go +++ b/pkg/parser/import_directive.go @@ -28,14 +28,20 @@ type ImportDirectiveMatch struct { func ParseImportDirective(line string) *ImportDirectiveMatch { trimmedLine := strings.TrimSpace(line) + // Fast-path: import directives must start with '@' or '{'; skip the regex for all other lines. + if len(trimmedLine) == 0 || (trimmedLine[0] != '@' && trimmedLine[0] != '{') { + return nil + } + // Check if it matches the import pattern at all matches := IncludeDirectivePattern.FindStringSubmatch(trimmedLine) if matches == nil { return nil } - // Check if it's legacy syntax - isLegacy := LegacyIncludeDirectivePattern.MatchString(trimmedLine) + // Determine legacy vs new syntax from the captured groups of the first match. + // Group 2 (path for @include/@import) is non-empty iff the legacy alternative matched. + isLegacy := matches[2] != "" importDirectiveLog.Printf("Parsing import directive: legacy=%t, line=%s", isLegacy, trimmedLine) var isOptional bool