Skip to content

Commit 7d1a279

Browse files
authored
perf: fix ~50% regression in FindIncludesInContent (#21265)
1 parent d71ea6b commit 7d1a279

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

pkg/cli/remove_command.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli
22

33
import (
4-
"bufio"
54
"fmt"
65
"os"
76
"path/filepath"
@@ -391,9 +390,7 @@ func findIncludesInContent(content, baseDir string, verbose bool) ([]string, err
391390
_ = verbose // unused parameter for now, keeping for potential future use
392391
var includes []string
393392

394-
scanner := bufio.NewScanner(strings.NewReader(content))
395-
for scanner.Scan() {
396-
line := scanner.Text()
393+
for line := range strings.Lines(content) {
397394
directive := parser.ParseImportDirective(line)
398395
if directive != nil {
399396
includePath := directive.Path
@@ -411,5 +408,5 @@ func findIncludesInContent(content, baseDir string, verbose bool) ([]string, err
411408
}
412409
}
413410

414-
return includes, scanner.Err()
411+
return includes, nil // strings.Lines iterates over an in-memory string; no I/O errors can occur.
415412
}

pkg/parser/import_directive.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ type ImportDirectiveMatch struct {
2828
func ParseImportDirective(line string) *ImportDirectiveMatch {
2929
trimmedLine := strings.TrimSpace(line)
3030

31+
// Fast-path: import directives must start with '@' or '{'; skip the regex for all other lines.
32+
if len(trimmedLine) == 0 || (trimmedLine[0] != '@' && trimmedLine[0] != '{') {
33+
return nil
34+
}
35+
3136
// Check if it matches the import pattern at all
3237
matches := IncludeDirectivePattern.FindStringSubmatch(trimmedLine)
3338
if matches == nil {
3439
return nil
3540
}
3641

37-
// Check if it's legacy syntax
38-
isLegacy := LegacyIncludeDirectivePattern.MatchString(trimmedLine)
42+
// Determine legacy vs new syntax from the captured groups of the first match.
43+
// Group 2 (path for @include/@import) is non-empty iff the legacy alternative matched.
44+
isLegacy := matches[2] != ""
3945
importDirectiveLog.Printf("Parsing import directive: legacy=%t, line=%s", isLegacy, trimmedLine)
4046

4147
var isOptional bool

0 commit comments

Comments
 (0)