go/packages: deduplicate file parsing

When loading packages from source, many files are being parsed
repeatedly, for example due to test variants. While the median number of
times a file gets parsed is 2, it is significantly higher (up to 28
times) when parsing the standard library, because of test variant
shenanigans.

By caching file contents and their parsed representations we can cut
down on processing time and garbage produced. When loading individual
packages or 3rd party projects, the effect is rather small. However when
loading the entire standard library, the effect is substantial.

name       old time/op    new time/op    delta
Jaeger-8      2.95s ± 7%     2.84s ± 8%     ~     (p=0.089 n=10+10)
Std-8         4.96s ± 7%     4.23s ± 3%  -14.62%  (p=0.000 n=9+9)
Strconv-8     892ms ±34%     877ms ±21%     ~     (p=0.853 n=10+10)

name       old alloc/op   new alloc/op   delta
Jaeger-8     1.22GB ± 0%    1.21GB ± 0%   -0.84%  (p=0.000 n=10+10)
Std-8        2.57GB ± 0%    2.20GB ± 0%  -14.61%  (p=0.000 n=10+8)
Strconv-8     201MB ± 1%     200MB ± 1%     ~     (p=0.105 n=10+10)

name       old allocs/op  new allocs/op  delta
Jaeger-8      12.7M ± 0%     12.4M ± 0%   -1.82%  (p=0.000 n=9+10)
Std-8         26.4M ± 0%     17.3M ± 0%  -34.62%  (p=0.000 n=9+9)
Strconv-8     1.94M ± 0%     1.91M ± 0%   -1.50%  (p=0.000 n=10+10)

When loading std, peak RSS decreases from 1.96 GB to 1.57 GB.

While we're here, we simplify our ParseFile implementation. The contract
of ParseFile specifies that implementations must use src for parsing,
and use filename only for display purposes. As such, we mustn't ever
call it with a nil src, making the check for a nil src in our own
implementation superfluous.

Change-Id: I33daac20fc52ccdb3187a336633f712d01b71d86
Reviewed-on: https://go-review.googlesource.com/c/tools/+/171377
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
1 file changed
tree: 231dc272118af71c6a943421cbdc39a3fc294f33
  1. benchmark/
  2. blog/
  3. cmd/
  4. container/
  5. cover/
  6. go/
  7. godoc/
  8. imports/
  9. internal/
  10. playground/
  11. present/
  12. refactor/
  13. .gitattributes
  14. .gitignore
  15. AUTHORS
  16. codereview.cfg
  17. CONTRIBUTING.md
  18. CONTRIBUTORS
  19. go.mod
  20. go.sum
  21. LICENSE
  22. PATENTS
  23. README.md
README.md

Go Tools

This subrepository holds the source for various packages and tools that support the Go programming language.

Some of the tools, godoc and vet for example, are included in binary Go distributions.

Others, including the Go guru and the test coverage tool, can be fetched with go get.

Packages include a type-checker for Go and an implementation of the Static Single Assignment form (SSA) representation for Go programs.

Download/Install

The easiest way to install is to run go get -u golang.org/x/tools/.... You can also manually git clone the repository to $GOPATH/src/golang.org/x/tools.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the tools repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/tools/(your subdir):” in the subject line, so it is easy to find.