cmd/guru: optimize global pkglevel referrer queries

Global, package-level queries can be done without typechecking.
This makes it significantly cheaper.

Instead of typechecking, consider all packages that directly
import the query package. In each of those packages, filter out:

* files that don't contain the query name
* files that don't import the query package

Fully parse the remaining files and look for selector expressions
matching the query package and name.

There are a few twists (dot imports, the query package, xtests).
They are described, along with how they are handled,
in the large comment at the beginning of globalReferrersPkgLevel.

On my machine and GOPATH, this reduces typical time
needed to find referrers to encoding/json.MarshalIndent
from (roughly)

real	0m39.946s
user	2m27.844s
sys	0m54.774s

to (roughly)

real	0m5.687s
user	0m15.793s
sys	0m16.001s

The processing of packages could be parallelized;
that is left for a future change, to ease reviewing.

Benefits from that will be limited;
building the reverse import graph accounts for 73%
of the runtime, and that is dominated by syscalls.

Optimization idea from Alan Donovan.

This work supported by Sourcegraph.

Change-Id: Ib19e25fcdcb27673fb03d7300dba2a53198901ad
Reviewed-on: https://go-review.googlesource.com/97800
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
1 file changed
tree: 7fb46757d09675104e8c30dfb519629267914380
  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. third_party/
  14. .gitattributes
  15. .gitignore
  16. AUTHORS
  17. codereview.cfg
  18. CONTRIBUTING.md
  19. CONTRIBUTORS
  20. LICENSE
  21. PATENTS
  22. 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.