Skip to content

Commit

Permalink
watch: use WalkDir to speed up file listing (#4684)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
milas committed Jun 28, 2021
1 parent 229fbb9 commit cff2cc8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/build/path_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (m PathMapping) PrettyStr() string {

func (m PathMapping) Filter(matcher model.PathMatcher) ([]PathMapping, error) {
result := make([]PathMapping, 0)
err := filepath.Walk(m.LocalPath, func(currentLocal string, info os.FileInfo, err error) error {
err := filepath.WalkDir(m.LocalPath, func(currentLocal string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/tiltfile/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io

import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -139,7 +140,7 @@ func listdir(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple,
}

var files []string
err = filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error {
err = filepath.WalkDir(localPath, func(path string, info fs.DirEntry, err error) error {
if path == localPath {
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions internal/watch/watcher_naive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package watch

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit cff2cc8

Please sign in to comment.