Skip to content

Commit 9b7d1a4

Browse files
committed
reduce unnecessary syscalls when searching for matches
1 parent 4ce758f commit 9b7d1a4

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

find/find.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func extractCustomSort(
9595
conf *config.Config,
9696
ch *file.Change,
9797
vars *variables.Variables,
98+
fileInfo fs.FileInfo,
9899
) error {
100+
ch.SortCriterion.Size = fileInfo.Size()
101+
ch.SortCriterion.Time = fileInfo.ModTime()
102+
99103
// Temporarily set Target to SortVariable due to how variables.Replace() works
100104
ch.Target = conf.SortVariable
101105

@@ -132,16 +136,16 @@ func extractCustomSort(
132136
func createFileChange(
133137
conf *config.Config,
134138
dirPath string,
135-
fileInfo fs.FileInfo,
139+
fileName string,
140+
isDir bool,
136141
) *file.Change {
137142
baseDir := filepath.Dir(dirPath)
138-
fileName := fileInfo.Name()
139143
sourcePath := filepath.Join(baseDir, fileName)
140144

141145
match := &file.Change{
142146
BaseDir: baseDir,
143147
TargetDir: baseDir,
144-
IsDir: fileInfo.IsDir(),
148+
IsDir: isDir,
145149
Source: fileName,
146150
OriginalName: fileName,
147151
SourcePath: sourcePath,
@@ -193,35 +197,36 @@ func checkIfMatch(
193197
filters Filters,
194198
sortVars *variables.Variables,
195199
) (*file.Change, bool, error) {
196-
var err error
197-
198-
fileInfo, err := entry.Info()
199-
if err != nil {
200-
return nil, false, err
201-
}
202-
203-
var isMatch bool
200+
fileName := entry.Name()
201+
isDir := entry.IsDir()
204202

205203
if conf.Search.FindCond != nil {
206-
match := createFileChange(conf, path, fileInfo)
204+
fileInfo, err := entry.Info()
205+
if err != nil {
206+
return nil, false, err
207+
}
208+
209+
match := createFileChange(conf, path, fileName, isDir)
207210
match.Target = conf.Search.FindCond.String()
211+
match.SortCriterion.Size = fileInfo.Size()
212+
match.SortCriterion.Time = fileInfo.ModTime()
208213

209-
return match, true, err
214+
return match, true, nil
210215
}
211216

212-
fileName := entry.Name()
217+
searchName := fileName
213218

214-
if conf.IgnoreExt && !entry.IsDir() && !conf.Pair {
215-
fileName = pathutil.StripExtension(fileName)
219+
if conf.IgnoreExt && !isDir && !conf.Pair {
220+
searchName = pathutil.StripExtension(fileName)
216221
}
217222

218-
if !conf.Search.Regex.MatchString(fileName) {
223+
if !conf.Search.Regex.MatchString(searchName) {
219224
return nil, false, nil
220225
}
221226

222-
match := createFileChange(conf, path, fileInfo)
227+
match := createFileChange(conf, path, fileName, isDir)
223228

224-
isMatch = true
229+
isMatch := true
225230

226231
slog.Debug(
227232
"found file matching search pattern",
@@ -238,7 +243,12 @@ func checkIfMatch(
238243
}
239244

240245
if conf.SortVariable != "" {
241-
err = extractCustomSort(ctx, conf, match, sortVars)
246+
fileInfo, err := entry.Info()
247+
if err != nil {
248+
return nil, false, err
249+
}
250+
251+
err = extractCustomSort(ctx, conf, match, sortVars, fileInfo)
242252
if err != nil {
243253
return nil, false, err
244254
}
@@ -358,12 +368,12 @@ func searchPaths(
358368
for _, rootPath := range conf.FilesAndDirPaths {
359369
rootPath = filepath.Clean(rootPath)
360370

361-
fileInfo, err := os.Stat(rootPath)
371+
rootPathInfo, err := os.Stat(rootPath)
362372
if err != nil {
363373
return nil, err
364374
}
365375

366-
if !fileInfo.IsDir() {
376+
if !rootPathInfo.IsDir() {
367377
if processedPaths[rootPath] {
368378
continue
369379
}
@@ -372,7 +382,7 @@ func searchPaths(
372382
ctx,
373383
conf,
374384
rootPath,
375-
fs.FileInfoToDirEntry(fileInfo),
385+
fs.FileInfoToDirEntry(rootPathInfo),
376386
filters,
377387
sortVars,
378388
)

0 commit comments

Comments
 (0)