From aa9b1bd076a6b54fcdcef92838f73c982a356d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 11 May 2022 16:52:13 +0100 Subject: [PATCH] all: avoid using too many packages.Need bits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I08cd45b5ecd9bdd9ce84e2ceadc70880f82293c2 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/538063 Unity-Result: CUEcueckoo TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe --- cmd/cue/cmd/get_go.go | 12 +++++------- cue/testdata/gen.go | 2 +- encoding/gocode/generator.go | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/cmd/cue/cmd/get_go.go b/cmd/cue/cmd/get_go.go index b67bdf4e7f2..2fa95dcae1c 100644 --- a/cmd/cue/cmd/get_go.go +++ b/cmd/cue/cmd/get_go.go @@ -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) @@ -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...) diff --git a/cue/testdata/gen.go b/cue/testdata/gen.go index ef96b8280a2..0f3cf786ac3 100644 --- a/cue/testdata/gen.go +++ b/cue/testdata/gen.go @@ -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, } diff --git a/encoding/gocode/generator.go b/encoding/gocode/generator.go index 5f50267b17e..8b8dafe69dd 100644 --- a/encoding/gocode/generator.go +++ b/encoding/gocode/generator.go @@ -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 {