Skip to content

Commit

Permalink
all: avoid using too many packages.Need bits
Browse files Browse the repository at this point in the history
go/packages.LoadMode exists so that it only asks `go list` for the
information you need, and no more.
Some of those bits are actually pretty expensive to use.
For instance, the Syntax ones will use go/parser to load go/ast files,
and NeedDeps will mean loading info for all dependencies recursively.

We really don't need everything; in fact, our needs are rather simple.
Reduce the LoadMode sets we use, driven by skimming the code and
re-running the tests and generation steps to ensure nothing is broken.

As a very coarse benchmark, the tests for the two packages get faster
on average, running three times before and after:

	cmd/cue/cmd     goes from ~3.2s to ~2.8s
	encoding/gocode goes from ~1.1s to ~0.2s

Overall, `cue get go` and `gocode.Generate` should be slightly faster.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I08cd45b5ecd9bdd9ce84e2ceadc70880f82293c2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/538063
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed May 12, 2022
1 parent 92b9a06 commit aa9b1bd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,8 @@ func initInterfaces() (err error) {
}

cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes |
packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps,
Dir: filepath.Join(tmpDir),
Mode: packages.NeedTypes | packages.NeedTypesInfo,
Dir: filepath.Join(tmpDir),
}

p, err := packages.Load(cfg, fullPkgPath)
Expand Down Expand Up @@ -404,9 +402,9 @@ func extract(cmd *Command, args []string) error {
root := binst.Root

cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes |
packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps |
Mode: packages.NeedName | packages.NeedCompiledGoFiles |
packages.NeedImports | packages.NeedTypes |
packages.NeedSyntax | packages.NeedTypesInfo |
packages.NeedModule,
}
pkgs, err := packages.Load(cfg, args...)
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
log.SetFlags(log.Lshortfile)

cfg := &packages.Config{
Mode: packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo,
Mode: packages.NeedSyntax | packages.NeedTypesInfo,

Tests: true,
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/gocode/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Generate(pkgPath string, inst *cue.Instance, c *Config) (b []byte, err erro

if pkgPath != "" {
loadCfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps,
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo,
}
pkgs, err := packages.Load(loadCfg, pkgPath)
if err != nil {
Expand Down

0 comments on commit aa9b1bd

Please sign in to comment.