feat: cache module name even when read dir to improve perf

main
sudo pacman -Syu 2023-01-17 23:50:28 +07:00
parent c55bc093be
commit c2e4dc5abf
1 changed files with 14 additions and 0 deletions

View File

@ -142,6 +142,13 @@ func (ft *Formatter) formatDir(path string) error {
}
if dirEntry.IsDir() {
// Get module name ASAP to cache it
moduleName, err := ft.moduleName(path)
if err != nil {
return err
}
ft.log("formatFile: moduleName: [%s]\n", moduleName)
return nil
}
@ -401,6 +408,13 @@ func (ft *Formatter) moduleName(path string) (string, error) {
dirPath := filepath.Clean(path)
var goModPath string
for {
ft.muModuleNames.RLock()
if pkgName, ok := ft.moduleNames[dirPath]; ok {
ft.muModuleNames.RUnlock()
return pkgName, nil
}
ft.muModuleNames.RUnlock()
goModPath = filepath.Join(dirPath, "go.mod")
fileInfo, err := os.Stat(goModPath)
if err == nil && !fileInfo.IsDir() {