Skip to content

Commit

Permalink
internal/mod/modpkgload: don't use modimports.PackageFiles twice
Browse files Browse the repository at this point in the history
This reduces the number of syscalls we do when loading one or all
packages by about 10% and 2% respectively, going from

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

to

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

Updates #3155.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I8a5bbe33b0d9656805a20a082be90faedd679122
  • Loading branch information
mvdan committed May 30, 2024
1 parent 7a66f6c commit eb7b3c8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions internal/mod/modpkgload/pkgload.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,16 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
importsMap := make(map[string]bool)
foundPackageFile := false
for _, loc := range pkg.locs {
// TODO(mvdan): using the PackageFiles iterator twice below
// causes twice as many io/fs operations on loc.FS.
pkgFileIter := modimports.PackageFiles(loc.FS, loc.Dir, pkgQual)
pkgFileIter(func(_ modimports.ModuleFile, err error) bool {
foundPackageFile = err == nil
return false
})
// Layer an iterator whose yield function keeps track of whether we have seen
// a single valid CUE file in the package directory.
// Otherwise we would have to iterate twice, causing twice as many io/fs operations.
pkgFileIter := func(yield func(modimports.ModuleFile, error) bool) {
yield2 := func(mf modimports.ModuleFile, err error) bool {
foundPackageFile = err == nil
return yield(mf, err)
}
modimports.PackageFiles(loc.FS, loc.Dir, pkgQual)(yield2)
}
imports, err := modimports.AllImports(pkgFileIter)
if err != nil {
pkg.err = fmt.Errorf("cannot get imports: %v", err)
Expand Down

0 comments on commit eb7b3c8

Please sign in to comment.