on main help sign in
perf: make it a tad faster

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

commita05b4342f64130c25feddf7234a1da7666bf0ac5 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateThu 7 Aug 2025, 14:06 (1 year ago) parentfbe879d
1 file changed+76−72
internal/fsext/ls.go+76−72
internal/fsext/ls.go+76−72
@@ -5,6 +5,7 @@ import (
55 "os"
66 "path/filepath"
77 "strings"
8+ "sync"
89
910 "github.com/charlievieth/fastwalk"
1011 "github.com/charmbracelet/crush/internal/config"
@@ -13,62 +14,79 @@ import (
1314)
1415
1516// commonIgnorePatterns contains commonly ignored files and directories
16−var commonIgnorePatterns = ignore.CompileIgnoreLines(
17− // Version control
18− ".git",
19− ".svn",
20− ".hg",
21− ".bzr",
22−
23− // IDE and editor files
24− ".vscode",
25− ".idea",
26− "*.swp",
27− "*.swo",
28− "*~",
29− ".DS_Store",
30− "Thumbs.db",
31−
32− // Build artifacts and dependencies
33− "node_modules",
34− "target",
35− "build",
36− "dist",
37− "out",
38− "bin",
39− "obj",
40− "*.o",
41− "*.so",
42− "*.dylib",
43− "*.dll",
44− "*.exe",
45−
46− // Logs and temporary files
47− "*.log",
48− "*.tmp",
49− "*.temp",
50− ".cache",
51− ".tmp",
52−
53− // Language-specific
54− "__pycache__",
55− "*.pyc",
56− "*.pyo",
57− ".pytest_cache",
58− "vendor",
59− "Cargo.lock",
60− "package-lock.json",
61− "yarn.lock",
62− "pnpm-lock.yaml",
63−
64− // OS generated files
65− ".Trash",
66− ".Spotlight-V100",
67− ".fseventsd",
68−
69− // Crush
70− ".crush",
71−)
17+var commonIgnorePatterns = sync.OnceValue(func() ignore.IgnoreParser {
18+ return ignore.CompileIgnoreLines(
19+ // Version control
20+ ".git",
21+ ".svn",
22+ ".hg",
23+ ".bzr",
24+
25+ // IDE and editor files
26+ ".vscode",
27+ ".idea",
28+ "*.swp",
29+ "*.swo",
30+ "*~",
31+ ".DS_Store",
32+ "Thumbs.db",
33+
34+ // Build artifacts and dependencies
35+ "node_modules",
36+ "target",
37+ "build",
38+ "dist",
39+ "out",
40+ "bin",
41+ "obj",
42+ "*.o",
43+ "*.so",
44+ "*.dylib",
45+ "*.dll",
46+ "*.exe",
47+
48+ // Logs and temporary files
49+ "*.log",
50+ "*.tmp",
51+ "*.temp",
52+ ".cache",
53+ ".tmp",
54+
55+ // Language-specific
56+ "__pycache__",
57+ "*.pyc",
58+ "*.pyo",
59+ ".pytest_cache",
60+ "vendor",
61+ "Cargo.lock",
62+ "package-lock.json",
63+ "yarn.lock",
64+ "pnpm-lock.yaml",
65+
66+ // OS generated files
67+ ".Trash",
68+ ".Spotlight-V100",
69+ ".fseventsd",
70+
71+ // Crush
72+ ".crush",
73+ )
74+})
75+
76+var homeIgnore = sync.OnceValue(func() ignore.IgnoreParser {
77+ home := config.HomeDir()
78+ var lines []string
79+ for _, name := range []string{
80+ filepath.Join(home, ".gitignore"),
81+ filepath.Join(home, ".config", "git", "ignore"),
82+ filepath.Join(home, ".config", "crush", "ignore"),
83+ } {
84+ if bts, err := os.ReadFile(name); err == nil {
85+ lines = append(lines, strings.Split(string(bts), "\n")...)
86+ }
87+ }
88+ return ignore.CompileIgnoreLines(lines...)
89+})
7290
7391type directoryLister struct {
7492 ignores *csync.Map[string, ignore.IgnoreParser]
@@ -81,20 +99,6 @@ func NewDirectoryLister(rootPath string) *directoryLister {
8199 ignores: csync.NewMap[string, ignore.IgnoreParser](),
82100 }
83101 dl.getIgnore(rootPath)
84− dl.ignores.GetOrSet("~", func() ignore.IgnoreParser {
85− home := config.HomeDir()
86− var lines []string
87− for _, name := range []string{
88− filepath.Join(home, ".gitignore"),
89− filepath.Join(home, ".config", "git", "ignore"),
90− filepath.Join(home, ".config", "crush", "ignore"),
91− } {
92− if bts, err := os.ReadFile(name); err == nil {
93− lines = append(lines, strings.Split(string(bts), "\n")...)
94− }
95− }
96− return ignore.CompileIgnoreLines(lines...)
97− })
98102 return dl
99103}
100104
@@ -126,7 +130,7 @@ func (dl *directoryLister) shouldIgnore(path string, ignorePatterns []string) bo
126130 relPath = path
127131 }
128132
129− if commonIgnorePatterns.MatchesPath(relPath) {
133+ if commonIgnorePatterns().MatchesPath(relPath) {
130134 slog.Debug("ingoring common pattern", "path", relPath)
131135 return true
132136 }
@@ -140,7 +144,7 @@ func (dl *directoryLister) shouldIgnore(path string, ignorePatterns []string) bo
140144 return true
141145 }
142146
143− if dl.getIgnore("~").MatchesPath(relPath) {
147+ if homeIgnore().MatchesPath(relPath) {
144148 slog.Debug("ingoring home dir pattern", "path", relPath)
145149 return true
146150 }