Skip to content

Commit c59089b

Browse files
authored
Do not load unnecessary package information (#203)
- Add benchmark test for registry.New
1 parent 188eb89 commit c59089b

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

internal/registry/registry.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package registry
33
import (
44
"errors"
55
"fmt"
6+
"go/ast"
67
"go/types"
78
"path/filepath"
89
"sort"
@@ -16,38 +17,40 @@ import (
1617
// imports and ensures there are no conflicts in the imported package
1718
// qualifiers.
1819
type Registry struct {
19-
srcPkg *packages.Package
20-
moqPkgPath string
21-
aliases map[string]string
22-
imports map[string]*Package
20+
srcPkgName string
21+
srcPkgTypes *types.Package
22+
moqPkgPath string
23+
aliases map[string]string
24+
imports map[string]*Package
2325
}
2426

2527
// New loads the source package info and returns a new instance of
2628
// Registry.
2729
func New(srcDir, moqPkg string) (*Registry, error) {
2830
srcPkg, err := pkgInfoFromPath(
29-
srcDir, packages.NeedName|packages.NeedSyntax|packages.NeedTypes|packages.NeedTypesInfo|packages.NeedDeps,
31+
srcDir, packages.NeedName|packages.NeedSyntax|packages.NeedTypes,
3032
)
3133
if err != nil {
3234
return nil, fmt.Errorf("couldn't load source package: %s", err)
3335
}
3436

3537
return &Registry{
36-
srcPkg: srcPkg,
37-
moqPkgPath: findPkgPath(moqPkg, srcPkg),
38-
aliases: parseImportsAliases(srcPkg),
39-
imports: make(map[string]*Package),
38+
srcPkgName: srcPkg.Name,
39+
srcPkgTypes: srcPkg.Types,
40+
moqPkgPath: findPkgPath(moqPkg, srcPkg.PkgPath),
41+
aliases: parseImportsAliases(srcPkg.Syntax),
42+
imports: make(map[string]*Package),
4043
}, nil
4144
}
4245

4346
// SrcPkg returns the types info for the source package.
4447
func (r Registry) SrcPkg() *types.Package {
45-
return r.srcPkg.Types
48+
return r.srcPkgTypes
4649
}
4750

4851
// SrcPkgName returns the name of the source package.
4952
func (r Registry) SrcPkgName() string {
50-
return r.srcPkg.Name
53+
return r.srcPkgName
5154
}
5255

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

176-
func findPkgPath(pkgInputVal string, srcPkg *packages.Package) string {
179+
func findPkgPath(pkgInputVal string, srcPkgPath string) string {
177180
if pkgInputVal == "" {
178-
return srcPkg.PkgPath
181+
return srcPkgPath
179182
}
180-
if pkgInDir(srcPkg.PkgPath, pkgInputVal) {
181-
return srcPkg.PkgPath
183+
if pkgInDir(srcPkgPath, pkgInputVal) {
184+
return srcPkgPath
182185
}
183-
subdirectoryPath := filepath.Join(srcPkg.PkgPath, pkgInputVal)
186+
subdirectoryPath := filepath.Join(srcPkgPath, pkgInputVal)
184187
if pkgInDir(subdirectoryPath, pkgInputVal) {
185188
return subdirectoryPath
186189
}
@@ -195,9 +198,9 @@ func pkgInDir(pkgName, dir string) bool {
195198
return currentPkg.Name == pkgName || currentPkg.Name+"_test" == pkgName
196199
}
197200

198-
func parseImportsAliases(pkg *packages.Package) map[string]string {
201+
func parseImportsAliases(syntaxTree []*ast.File) map[string]string {
199202
aliases := make(map[string]string)
200-
for _, syntax := range pkg.Syntax {
203+
for _, syntax := range syntaxTree {
201204
for _, imprt := range syntax.Imports {
202205
if imprt.Name != nil && imprt.Name.Name != "." && imprt.Name.Name != "_" {
203206
aliases[strings.Trim(imprt.Path.Value, `"`)] = imprt.Name.Name

internal/registry/registry_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package registry_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/matryer/moq/internal/registry"
7+
)
8+
9+
func BenchmarkNew(b *testing.B) {
10+
for i := 0; i < b.N; i++ {
11+
registry.New("../../pkg/moq/testpackages/example", "")
12+
}
13+
}

0 commit comments

Comments
 (0)