diff --git a/walk/trie.go b/walk/trie.go index 3371e7f26..0271db823 100644 --- a/walk/trie.go +++ b/walk/trie.go @@ -43,26 +43,12 @@ func (t *pathTrie) Get(key string) *pathTrie { return node } -func (t *pathTrie) Put(key string, entry *fs.DirEntry) bool { - node := t - for part, i := pathSegmenter(key, 0); part != ""; part, i = pathSegmenter(key, i) { - child := node.children[part] - if child == nil { - if node.children == nil { - node.children = map[string]*pathTrie{} - } - - var child *pathTrie - if i < 0 { - child = &pathTrie{children: nil, entry: entry} - } else { - child = &pathTrie{} - } - - node.children[part] = child - } - node = child +func (t *pathTrie) AddChild(entry fs.DirEntry) *pathTrie { + if t.children == nil { + t.children = map[string]*pathTrie{} } - return true + child := &pathTrie{entry: &entry} + t.children[entry.Name()] = child + return child } diff --git a/walk/walk.go b/walk/walk.go index fcad3c7c9..6312def83 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -25,7 +25,6 @@ import ( "path/filepath" "sort" "strings" - "sync" "github.com/bazelbuild/bazel-gazelle/config" "github.com/bazelbuild/bazel-gazelle/rule" @@ -368,25 +367,18 @@ func resolveFileInfo(wc *walkConfig, dir, rel string, ent fs.DirEntry) fs.DirEnt func buildTrie(c *config.Config, isIgnored isIgnoredFunc) (*pathTrie, error) { trie := &pathTrie{} - mu := sync.Mutex{} eg := errgroup.Group{} eg.SetLimit(100) eg.Go(func() error { - return walkDir(c.RepoRoot, "", &eg, isIgnored, func(rel string, d fs.DirEntry) { - mu.Lock() - defer mu.Unlock() - trie.Put(rel, &d) - }) + return walkDir(c.RepoRoot, "", &eg, isIgnored, trie) }) return trie, eg.Wait() } -type walkDirFunc func(rel string, d fs.DirEntry) - // walkDir recursively descends path, calling walkDirFn. -func walkDir(root, rel string, eg *errgroup.Group, isIgnored isIgnoredFunc, walkDirFn walkDirFunc) error { +func walkDir(root, rel string, eg *errgroup.Group, isIgnored isIgnoredFunc, trie *pathTrie) error { dirs, err := os.ReadDir(path.Join(root, rel)) if err != nil { return err @@ -401,11 +393,11 @@ func walkDir(root, rel string, eg *errgroup.Group, isIgnored isIgnoredFunc, walk continue } - walkDirFn(path1, d1) + childTrie := trie.AddChild(d1) if d1.IsDir() { eg.Go(func() error { - return walkDir(root, path1, eg, isIgnored, walkDirFn) + return walkDir(root, path1, eg, isIgnored, childTrie) }) } }