Skip to content

Commit

Permalink
Merge pull request #14 from aktsk/cache-loaded-packages
Browse files Browse the repository at this point in the history
Cache results of packges.Load() to shorten execution time
  • Loading branch information
sachaos authored May 13, 2019
2 parents 06a1541 + e6c3cde commit b9f7ee1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,25 @@ func isRelativePath(path string) bool {
return strings.HasPrefix(path, ".")
}

var packageCache = map[string][]*packages.Package{}

func validateRouterFuncs(routerFuncs []*RouterFunc, program []*packages.Package) error {
conf := &packages.Config{Mode: packages.LoadAllSyntax}
for _, routerFunc := range routerFuncs {
pkgs, err := packages.Load(conf, "net/http", routerFunc.PackagePath)
if err != nil {
return errors.WithStack(err)
path := routerFunc.PackagePath
if packageCache[path] == nil {
pkgs, err := packages.Load(conf, "net/http", path)
if err != nil {
return errors.WithStack(err)
}
packageCache[path] = pkgs
}

pkgs := packageCache[path]
handlerObj := pkgs[0].Types.Scope().Lookup("Handler")
funcObj := pkgs[1].Types.Scope().Lookup(routerFunc.Name)

err = validateRouterFuncObj(handlerObj, funcObj, routerFunc)
err := validateRouterFuncObj(handlerObj, funcObj, routerFunc)
if err != nil {
return err
}
Expand Down

0 comments on commit b9f7ee1

Please sign in to comment.