Skip to content

Commit 7161778

Browse files
milasndeloof
authored andcommitted
watch: use WalkDir to speed up file listing (docker#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.
1 parent 1f5bfe8 commit 7161778

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pkg/watch/watcher_naive.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package watch
44

55
import (
66
"fmt"
7+
"io/fs"
78
"os"
89
"path/filepath"
910
"runtime"
@@ -92,12 +93,12 @@ func (d *naiveNotify) watchRecursively(dir string) error {
9293
return errors.Wrapf(err, "watcher.Add(%q)", dir)
9394
}
9495

95-
return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
96+
return filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
9697
if err != nil {
9798
return err
9899
}
99100

100-
if !mode.IsDir() {
101+
if !info.IsDir() {
101102
return nil
102103
}
103104

@@ -163,7 +164,7 @@ func (d *naiveNotify) loop() {
163164
// because it's a bit more elegant that way.
164165
//
165166
// TODO(dbentley): if there's a delete should we call d.watcher.Remove to prevent leaking?
166-
err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error {
167+
err := filepath.WalkDir(e.Name, func(path string, info fs.DirEntry, err error) error {
167168
if err != nil {
168169
return err
169170
}
@@ -175,7 +176,7 @@ func (d *naiveNotify) loop() {
175176
// TODO(dmiller): symlinks 😭
176177

177178
shouldWatch := false
178-
if mode.IsDir() {
179+
if info.IsDir() {
179180
// watch directories unless we can skip them entirely
180181
shouldSkipDir, err := d.shouldSkipDir(path)
181182
if err != nil {

0 commit comments

Comments
 (0)