From 7161778ccf4feadaa37f24c0500b057fcee19843 Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Mon, 28 Jun 2021 10:30:53 -0400 Subject: [PATCH] watch: use WalkDir to speed up file listing (#4684) `WalkDir` is new in Go 1.16 and avoids calling `os.Lstat` on every visited file and directory. In most cases, we don't need that info, so this will help reduce I/O when listing files, which can be helpful for particularly big monorepos. --- pkg/watch/watcher_naive.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/watch/watcher_naive.go b/pkg/watch/watcher_naive.go index f66d5ad52..101703a21 100644 --- a/pkg/watch/watcher_naive.go +++ b/pkg/watch/watcher_naive.go @@ -4,6 +4,7 @@ package watch import ( "fmt" + "io/fs" "os" "path/filepath" "runtime" @@ -92,12 +93,12 @@ func (d *naiveNotify) watchRecursively(dir string) error { return errors.Wrapf(err, "watcher.Add(%q)", dir) } - return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error { + return filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error { if err != nil { return err } - if !mode.IsDir() { + if !info.IsDir() { return nil } @@ -163,7 +164,7 @@ func (d *naiveNotify) loop() { // because it's a bit more elegant that way. // // TODO(dbentley): if there's a delete should we call d.watcher.Remove to prevent leaking? - err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error { + err := filepath.WalkDir(e.Name, func(path string, info fs.DirEntry, err error) error { if err != nil { return err } @@ -175,7 +176,7 @@ func (d *naiveNotify) loop() { // TODO(dmiller): symlinks 😭 shouldWatch := false - if mode.IsDir() { + if info.IsDir() { // watch directories unless we can skip them entirely shouldSkipDir, err := d.shouldSkipDir(path) if err != nil {