Skip to content

Commit

Permalink
remove use of lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 13, 2024
1 parent 508ef8e commit 3df4f70
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
26 changes: 6 additions & 20 deletions walk/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 4 additions & 12 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"path/filepath"
"sort"
"strings"
"sync"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
Expand Down Expand Up @@ -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
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 3df4f70

Please sign in to comment.