Skip to content

Commit

Permalink
refactor: change pathTrie per-file to per-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Oct 27, 2024
1 parent 84320ee commit 572327d
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,10 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,
func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[string]bool, updateRels *UpdateFilter, trie *pathTrie, wf WalkFunc, rel string, updateParent bool) {
haveError := false

ents := make([]fs.DirEntry, 0, len(trie.children))
for _, node := range trie.children {
ents = append(ents, *node.entry)
}

sort.Slice(ents, func(i, j int) bool {
return ents[i].Name() < ents[j].Name()
})

// Absolute path to the directory being visited
dir := filepath.Join(c.RepoRoot, rel)

f, err := loadBuildFile(c, rel, dir, ents)
f, err := loadBuildFile(c, rel, dir, trie.files)
if err != nil {
log.Print(err)
if c.Strict {
Expand All @@ -165,28 +156,38 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri
return
}

var subdirs, regularFiles []string
for _, ent := range ents {
// Filter and collect files
var regularFiles []string
for _, ent := range trie.files {
base := ent.Name()
entRel := path.Join(rel, base)
if wc.isExcluded(entRel) {
continue
}
ent := resolveFileInfo(wc, dir, entRel, ent)
switch {
case ent == nil:
if ent := resolveFileInfo(wc, dir, entRel, ent); ent != nil {
regularFiles = append(regularFiles, base)
}
}

// Filter and collect subdirectories
var subdirs []string
var subdirTries []*pathTrie
for _, t := range trie.children {
base := t.entry.Name()
entRel := path.Join(rel, base)
if wc.isExcluded(entRel) {
continue
case ent.IsDir():
}
if ent := resolveFileInfo(wc, dir, entRel, t.entry); ent != nil {
subdirs = append(subdirs, base)
default:
regularFiles = append(regularFiles, base)
subdirTries = append(subdirTries, t)
}
}

shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
for i, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, cexts, knownDirectives, updateRels, trie.children[sub], wf, subRel, shouldUpdate)
visit(c, cexts, knownDirectives, updateRels, subdirTries[i], wf, subRel, shouldUpdate)
}
}

Expand Down Expand Up @@ -361,19 +362,20 @@ func resolveFileInfo(wc *walkConfig, dir, rel string, ent fs.DirEntry) fs.DirEnt
}

type pathTrie struct {
children map[string]*pathTrie
entry *fs.DirEntry
entry fs.DirEntry
files []fs.DirEntry
children []*pathTrie
}

// Basic factory method to ensure the entry is properly copied
func newTrie(entry fs.DirEntry) *pathTrie {
return &pathTrie{entry: &entry}
return &pathTrie{
entry: entry,
}
}

func buildTrie(c *config.Config, isIgnored isIgnoredFunc) (*pathTrie, error) {
trie := &pathTrie{
children: map[string]*pathTrie{},
}
trie := &pathTrie{}

// A channel to limit the number of concurrent goroutines
limitCh := make(chan struct{}, 100)
Expand All @@ -397,6 +399,12 @@ func walkDir(root, rel string, eg *errgroup.Group, limitCh chan struct{}, isIgno
return err
}

// Sort trie entries once before processing to ensure
// a consistent order of traversal and deterministic output.
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name() < entries[j].Name()
})

for _, entry := range entries {
entryName := entry.Name()
entryPath := path.Join(rel, entryName)
Expand All @@ -406,14 +414,14 @@ func walkDir(root, rel string, eg *errgroup.Group, limitCh chan struct{}, isIgno
continue
}

entryTrie := newTrie(entry)
trie.children[entry.Name()] = entryTrie

if entry.IsDir() {
entryTrie.children = map[string]*pathTrie{}
entryTrie := newTrie(entry)
trie.children = append(trie.children, entryTrie)
eg.Go(func() error {
return walkDir(root, entryPath, eg, limitCh, isIgnored, entryTrie)
})
} else {
trie.files = append(trie.files, entry)
}
}
return nil
Expand Down

0 comments on commit 572327d

Please sign in to comment.