Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not load unnecessary package information #203

Merged
merged 2 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"errors"
"fmt"
"go/ast"
"go/types"
"path/filepath"
"sort"
Expand All @@ -16,38 +17,40 @@ import (
// imports and ensures there are no conflicts in the imported package
// qualifiers.
type Registry struct {
srcPkg *packages.Package
moqPkgPath string
aliases map[string]string
imports map[string]*Package
srcPkgName string
srcPkgTypes *types.Package
moqPkgPath string
aliases map[string]string
imports map[string]*Package
}

// New loads the source package info and returns a new instance of
// Registry.
func New(srcDir, moqPkg string) (*Registry, error) {
srcPkg, err := pkgInfoFromPath(
srcDir, packages.NeedName|packages.NeedSyntax|packages.NeedTypes|packages.NeedTypesInfo|packages.NeedDeps,
srcDir, packages.NeedName|packages.NeedSyntax|packages.NeedTypes,
Copy link
Contributor Author

@samherrmann samherrmann Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs:

// NeedTypesInfo adds TypesInfo.
NeedTypesInfo

// NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
NeedDeps

Neither TypesInfo or Imports field is used, therefore there is no need to load this information.

)
if err != nil {
return nil, fmt.Errorf("couldn't load source package: %s", err)
}

return &Registry{
srcPkg: srcPkg,
moqPkgPath: findPkgPath(moqPkg, srcPkg),
aliases: parseImportsAliases(srcPkg),
imports: make(map[string]*Package),
srcPkgName: srcPkg.Name,
srcPkgTypes: srcPkg.Types,
moqPkgPath: findPkgPath(moqPkg, srcPkg.PkgPath),
aliases: parseImportsAliases(srcPkg.Syntax),
imports: make(map[string]*Package),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

srcPkg only has the fields populated that were instructed to be populated by the packages.LoadMode (line 31). It is therefore not ideal to let srcPkg outside of this New function because it opens up the possibility for other areas of the code to depend on fields that were not loaded. It is also more difficult to see if other areas of the code actually require certain packages.LoadMode flags. By not passing srcPkg outside of this New function, it is now easier to see which packages.LoadMode flags are truly needed.

}, nil
}

// SrcPkg returns the types info for the source package.
func (r Registry) SrcPkg() *types.Package {
return r.srcPkg.Types
return r.srcPkgTypes
}

// SrcPkgName returns the name of the source package.
func (r Registry) SrcPkgName() string {
return r.srcPkg.Name
return r.srcPkgName
}

// LookupInterface returns the underlying interface definition of the
Expand Down Expand Up @@ -173,14 +176,14 @@ func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package,
return pkgs[0], nil
}

func findPkgPath(pkgInputVal string, srcPkg *packages.Package) string {
func findPkgPath(pkgInputVal string, srcPkgPath string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the argument so that we don't pass more into the function than what's needed.

if pkgInputVal == "" {
return srcPkg.PkgPath
return srcPkgPath
}
if pkgInDir(srcPkg.PkgPath, pkgInputVal) {
return srcPkg.PkgPath
if pkgInDir(srcPkgPath, pkgInputVal) {
return srcPkgPath
}
subdirectoryPath := filepath.Join(srcPkg.PkgPath, pkgInputVal)
subdirectoryPath := filepath.Join(srcPkgPath, pkgInputVal)
if pkgInDir(subdirectoryPath, pkgInputVal) {
return subdirectoryPath
}
Expand All @@ -195,9 +198,9 @@ func pkgInDir(pkgName, dir string) bool {
return currentPkg.Name == pkgName || currentPkg.Name+"_test" == pkgName
}

func parseImportsAliases(pkg *packages.Package) map[string]string {
func parseImportsAliases(syntaxTree []*ast.File) map[string]string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the argument so that we don't pass more into the function than what's needed.

aliases := make(map[string]string)
for _, syntax := range pkg.Syntax {
for _, syntax := range syntaxTree {
for _, imprt := range syntax.Imports {
if imprt.Name != nil && imprt.Name.Name != "." && imprt.Name.Name != "_" {
aliases[strings.Trim(imprt.Path.Value, `"`)] = imprt.Name.Name
Expand Down
13 changes: 13 additions & 0 deletions internal/registry/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package registry_test

import (
"testing"

"github.com/matryer/moq/internal/registry"
)

func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
registry.New("../../pkg/moq/testpackages/example", "")
}
}