-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(cli): avoid use of os.Stat for checking if file exists (#7173)
We should not be using `os.Stat` to check if files exist. It is very expensive and gazelle has already read the full list of entries for the directory. The gazelle patch exposes that list. This is similar to bazel-contrib/bazel-gazelle#1737 which was done at airtable for similar reasons. On one major repo this reduces the time by almost 2s which is ~15% and well worth the patch imo. --- ### Changes are visible to end-users: no - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: yes Reduce fs io of `aspect configure` during fs traversal. ### Test plan - Covered by existing test cases GitOrigin-RevId: b17d52b2723d3b7e4d5ee33fc294f491062ded5a
- Loading branch information
Showing
7 changed files
with
104 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
diff --git a/config/config.go b/config/config.go | ||
index 1841650..f34ad5e 100644 | ||
--- a/config/config.go | ||
+++ b/config/config.go | ||
@@ -122,6 +122,7 @@ type MappedKind struct { | ||
|
||
const ASPECT_WALKSUBDIR = "__aspect:walksubdir" | ||
const ASPECT_GITIGNORE = "__aspect:gitignore" | ||
+const ASPECT_DIR_ENTRIES = "__aspect:direntries" | ||
|
||
func New() *Config { | ||
return &Config{ | ||
diff --git a/walk/walk.go b/walk/walk.go | ||
index 1ba1b47..19e99ef 100644 | ||
--- a/walk/walk.go | ||
+++ b/walk/walk.go | ||
@@ -144,6 +144,14 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri | ||
return ents[i].Name() < ents[j].Name() | ||
}) | ||
|
||
+ // PATCH(fs.DirEntry map) --- | ||
+ entsMap := make(map[string]fs.DirEntry, len(trie.children)) | ||
+ for _, node := range trie.children { | ||
+ entsMap[(*node.entry).Name()] = *node.entry | ||
+ } | ||
+ c.Exts[config.ASPECT_DIR_ENTRIES] = entsMap | ||
+ // END PATCH(fs.DirEntry map) --- | ||
+ | ||
// Absolute path to the directory being visited | ||
dir := filepath.Join(c.RepoRoot, rel) | ||
|
||
@@ -158,7 +166,7 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri | ||
haveError = true | ||
} | ||
|
||
- c = configure(cexts, knownDirectives, c, rel, f) | ||
+ configure(cexts, knownDirectives, c, rel, f) | ||
wc := getWalkConfig(c) | ||
|
||
if wc.isExcluded(rel) { | ||
@@ -203,7 +211,7 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri | ||
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) { | ||
// PATCH --- | ||
// Merge the returned 'subFiles' if 'mergeFiles' is true | ||
- subFiles, mergeFiles := visit(c, cexts, knownDirectives, updateRels, trie.children[sub], wf, subRel, shouldUpdate) | ||
+ subFiles, mergeFiles := visit(c.Clone(), cexts, knownDirectives, updateRels, trie.children[sub], wf, subRel, shouldUpdate) | ||
if mergeFiles { | ||
for _, f := range subFiles { | ||
regularFiles = append(regularFiles, path.Join(sub, f)) | ||
@@ -330,10 +338,7 @@ func loadBuildFile(c *config.Config, pkg, dir string, ents []fs.DirEntry) (*rule | ||
return rule.LoadFile(path, pkg) | ||
} | ||
|
||
-func configure(cexts []config.Configurer, knownDirectives map[string]bool, c *config.Config, rel string, f *rule.File) *config.Config { | ||
- if rel != "" { | ||
- c = c.Clone() | ||
- } | ||
+func configure(cexts []config.Configurer, knownDirectives map[string]bool, c *config.Config, rel string, f *rule.File) { | ||
if f != nil { | ||
for _, d := range f.Directives { | ||
if !knownDirectives[d.Key] { | ||
@@ -349,7 +354,6 @@ func configure(cexts []config.Configurer, knownDirectives map[string]bool, c *co | ||
for _, cext := range cexts { | ||
cext.Configure(c, rel, f) | ||
} | ||
- return c | ||
} | ||
|
||
func findGenFiles(wc *walkConfig, f *rule.File) []string { |