Skip to content

Commit

Permalink
cue/load: cache file names only
Browse files Browse the repository at this point in the history
A cache that makes sense across both the cue/load logic and the mod/...
logic cannot be implemented in terms of cue/load abstractions such as
`build.File` because we don't want the module code to depend on
cue/build.

Instead, it makes more sense to cache more basic operations such the
names inside a directory.

Creating a `*build.File` from a filename is now cheaper since the
previous change (https://review.gerrithub.io/c/cue-lang/cue/+/1197557),
so taking both CLs into account, performance actually
slightly improves overall.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I043a4229f9b78dbccb5fbe140e57765a8a11b5f8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197521
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
rogpeppe committed Jul 11, 2024
1 parent fed43b0 commit 4a8f673
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
51 changes: 27 additions & 24 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"io/fs"
"os"
pathpkg "path"
"path/filepath"
"slices"
Expand Down Expand Up @@ -155,12 +154,17 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
}
return retErr(errors.Wrapf(err, token.NoPos, "import failed reading dir %v", dir))
}
p.UnknownFiles = append(p.UnknownFiles, sd.unknownFiles...)
for _, f := range sd.buildFiles {
bf := *f
fp.add(dir, &bf, importComment)
for _, name := range sd.filenames {
file, err := filetypes.ParseFileAndType(name, "", filetypes.Input)
if err != nil {
p.UnknownFiles = append(p.UnknownFiles, &build.File{
Filename: name,
ExcludeReason: errors.Newf(token.NoPos, "unknown filetype"),
})
} else {
fp.add(dir, file, importComment)
}
}

if p.PkgName == "" || !inModule || l.cfg.isModRoot(dir) || dir == d[0] {
break
}
Expand Down Expand Up @@ -210,33 +214,32 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
return all
}

func (l *loader) scanDir(dir string) cachedFileFiles {
sd := cachedFileFiles{}
func (l *loader) scanDir(dir string) cachedDirFiles {
files, err := l.cfg.fileSystem.readDir(dir)
if err != nil {
sd.err = err
return sd
return cachedDirFiles{
err: err,
}
}
filenames := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
continue
}
if f.Name() == "-" {
if _, err := l.cfg.fileSystem.stat("-"); !os.IsNotExist(err) {
continue
}
}
file, err := filetypes.ParseFile(f.Name(), filetypes.Input)
if err != nil {
sd.unknownFiles = append(sd.unknownFiles, &build.File{
Filename: f.Name(),
ExcludeReason: errors.Newf(token.NoPos, "unknown filetype"),
})
continue // skip unrecognized file types
name := f.Name()
if name == "-" {
// The name "-" has a special significance to the file types
// logic, but only when specified directly on the command line.
// We don't want an actual file named "-" to have special
// significant, so avoid that by making sure we don't see a naked "-"
// even when a file named "-" is present in a directory.
name = "./-"
}
sd.buildFiles = append(sd.buildFiles, file)
filenames = append(filenames, name)
}
return cachedDirFiles{
filenames: filenames,
}
return sd
}

func setFileSource(cfg *Config, f *build.File) error {
Expand Down
22 changes: 10 additions & 12 deletions cue/load/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,25 @@ type loader struct {
// multiple different build instances in the same directory hierarchy.
syntaxCache *syntaxCache

// dirCachedBuildFiles caches the work involved when reading a directory
// and determining what build files it contains.
// It is keyed by directory name.
// When we descend into subdirectories to load patterns such as ./...
// we often end up loading parent directories many times over;
// this cache amortizes that work.
dirCachedBuildFiles map[string]cachedFileFiles
// dirCachedBuildFiles caches the work involved when reading a
// directory. It is keyed by directory name. When we descend into
// subdirectories to load patterns such as ./... we often end up
// loading parent directories many times over; this cache
// amortizes that work.
dirCachedBuildFiles map[string]cachedDirFiles
}

type cachedFileFiles struct {
err errors.Error
buildFiles []*build.File
unknownFiles []*build.File
type cachedDirFiles struct {
err errors.Error
filenames []string
}

func newLoader(c *Config, tg *tagger, syntaxCache *syntaxCache, pkgs *modpkgload.Packages) *loader {
return &loader{
cfg: c,
tagger: tg,
pkgs: pkgs,
dirCachedBuildFiles: map[string]cachedFileFiles{},
dirCachedBuildFiles: make(map[string]cachedDirFiles),
syntaxCache: syntaxCache,
}
}
Expand Down

0 comments on commit 4a8f673

Please sign in to comment.