From 4e3518700c9886a1bcaa8eb1d795cf32bcc57792 Mon Sep 17 00:00:00 2001 From: Douglas Danger Manley Date: Thu, 24 Jan 2019 02:05:24 +0000 Subject: [PATCH] go/packages: stop parsing files if the context is canceled `stamblerre/gocode`, which is the fork of `mdempsky/gocode` that supports Go 1.11 modules, uses `packages.Load` to load all of the Go code for, among other things, IDE autocomplete support. A common aspect to IDE autocomplete is asking for suggestions very frequently (perhaps at every character typed). Once the user has typed another character, the previous request for autocomplete is invalid and can be canceled. `packages.Load` already stops its `go list` component if the context is canceled, but if the context is canceled afterward, then it will parse all of the files that it found. This change stops the parsing of Go files once it detects that the context has been canceled. When a file has not been processed due to cancelation, its error will be set to that of the context. This change dramatically improves the performance of the `stamblerre/gocode` fork when requests have been canceled. Change-Id: Iba8c1e08eefa59137559ac9108238bfe5ba4ac21 GitHub-Last-Rev: 11a2210c8ce2ed9db9462ddc3e9676476f49f937 GitHub-Pull-Request: golang/tools#72 Reviewed-on: https://go-review.googlesource.com/c/159259 Reviewed-by: Michael Matloob Run-TryBot: Michael Matloob TryBot-Result: Gobot Gobot --- go/packages/packages.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/go/packages/packages.go b/go/packages/packages.go index 5da437fc..f4aac56e 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -773,6 +773,11 @@ func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) { parsed := make([]*ast.File, n) errors := make([]error, n) for i, file := range filenames { + if ld.Config.Context.Err() != nil { + parsed[i] = nil + errors[i] = ld.Config.Context.Err() + continue + } wg.Add(1) go func(i int, filename string) { ioLimit <- true // wait