Skip to content

Commit

Permalink
parallelize dir traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 26, 2024
1 parent acedd85 commit f4834cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions walk/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//flag",
"//rule",
"@com_github_bmatcuk_doublestar_v4//:doublestar",
"@org_golang_x_sync//errgroup",
],
)

Expand Down
33 changes: 21 additions & 12 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"path/filepath"
"sort"
"strings"
"sync"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"golang.org/x/sync/errgroup"
)

// Mode determines which directories Walk visits and which directories
Expand Down Expand Up @@ -366,25 +368,33 @@ func resolveFileInfo(wc *walkConfig, dir, rel string, ent fs.DirEntry) fs.DirEnt

func buildTrie(c *config.Config, isBazelIgnored isIgnoredFunc) (*pathTrie, error) {
trie := &pathTrie{}
mu := sync.Mutex{}

eg := errgroup.Group{}
eg.SetLimit(1000) // TODO: make this configurable? calculate based on number of CPUs?
eg.Go(func() error {
return walkDir(c.RepoRoot, "", &eg, func(rel string, d fs.DirEntry) error {
if isBazelIgnored(rel) {
return walkSkipDir
}

err := walkDir(c.RepoRoot, "", func(rel string, d fs.DirEntry) error {
if isBazelIgnored(rel) {
return walkSkipDir
}
mu.Lock()
defer mu.Unlock()

trie.Put(rel, &d)
return nil
trie.Put(rel, &d)
return nil
})
})

return trie, err
return trie, eg.Wait()
}

var walkSkipDir error = fs.SkipDir

type walkDirFunc func(rel string, d fs.DirEntry) error

// walkDir recursively descends path, calling walkDirFn.
func walkDir(root, rel string, walkDirFn walkDirFunc) error {
func walkDir(root, rel string, eg *errgroup.Group, walkDirFn walkDirFunc) error {
dirs, err := os.ReadDir(filepath.Join(root, rel))
if err != nil {
return err
Expand All @@ -400,10 +410,9 @@ func walkDir(root, rel string, walkDirFn walkDirFunc) error {
}

if d1.IsDir() {
// TODO: parallelize
if err := walkDir(root, path1, walkDirFn); err != nil {
return err
}
eg.Go(func() error {
return walkDir(root, path1, eg, walkDirFn)
})
}
}
return nil
Expand Down

0 comments on commit f4834cb

Please sign in to comment.