Skip to content

Commit

Permalink
cue/load: cache which directories are missing in importPkg
Browse files Browse the repository at this point in the history
This avoids repeated calls to os.Stat via fileSystem.isDir.
It reduces the number of syscalls from

    $ strace -f -t -e trace=file cue fmt ./internal/ci |& grep '/home/mvdan/src' | wc -l
    90
    $ strace -f -t -e trace=file cue fmt ./... |& grep '/home/mvdan/src' | wc -l
    5224

by about 3% and 20% respectively to

    $ strace -f -t -e trace=file cue fmt ./internal/ci |& grep '/home/mvdan/src' | wc -l
    87
    $ strace -f -t -e trace=file cue fmt ./... |& grep '/home/mvdan/src' | wc -l
    4162

Updates #3155.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I51e09a0adf233788c5c92c62c20a743b7873e73e
  • Loading branch information
mvdan committed May 29, 2024
1 parent cfdd6b3 commit b641321
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
9 changes: 0 additions & 9 deletions cue/load/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,6 @@ func (fs *fileSystem) makeAbs(path string) string {
return filepath.Join(fs.cwd, path)
}

func (fs *fileSystem) isDir(path string) bool {
path = fs.makeAbs(path)
if fs.getDir(path, false) != nil {
return true
}
fi, err := os.Stat(path)
return err == nil && fi.IsDir()
}

func (fs *fileSystem) readDir(path string) ([]iofs.DirEntry, errors.Error) {
path = fs.makeAbs(path)
m := fs.getDir(path, false)
Expand Down
9 changes: 7 additions & 2 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"cmp"
"fmt"
"io"
"io/fs"
"os"
pathpkg "path"
"path/filepath"
Expand Down Expand Up @@ -141,13 +142,17 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
// since a package foo/bar/baz inherits from parent packages foo/bar and foo.
// See https://cuelang.org/docs/concept/modules-packages-instances/#instances.
for _, d := range dirs {
for dir := filepath.Clean(d[1]); ctxt.isDir(dir); {
dir := filepath.Clean(d[1])
for {
sd, ok := l.dirCachedBuildFiles[dir]
if !ok {
sd = l.scanDir(dir)
l.dirCachedBuildFiles[dir] = sd
}
if err := sd.err; err != nil {
if errors.Is(err, fs.ErrNotExist) {
break
}
return retErr(errors.Wrapf(err, token.NoPos, "import failed reading dir %v", dir))
}
p.UnknownFiles = append(p.UnknownFiles, sd.unknownFiles...)
Expand Down Expand Up @@ -208,7 +213,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
func (l *loader) scanDir(dir string) cachedFileFiles {
sd := cachedFileFiles{}
files, err := l.cfg.fileSystem.readDir(dir)
if err != nil && !os.IsNotExist(err) {
if err != nil {
sd.err = err
return sd
}
Expand Down

0 comments on commit b641321

Please sign in to comment.