go/internal/lsp/source/completion
Alan Donovan b230791f2d internal/lsp/cache: move PosTo{Decl,Field} out of cache
Before, these methods of the Source interface used to use
a cache of buildASTCache, which built a Pos-keyed map for
a whole file, but the necessary algorithm is essentially
a binary search which is plenty fast enough to avoid the
need for cache.

This change implements that algorithm and moves
both methods out of the interface into a single function,
source.FindDeclAndField.

--

I measured the duration of all calls to astCacheData (before)
and FindDeclAndField (after) occurring within this command:

  $ go test -bench=TestBenchmarkConfiguredCompletion -v ./gopls/internal/regtest/bench -completion_workdir=$HOME/w/kubernetes -completion_file=../kubernetes/pkg/generated/openapi/zz_generated.openapi.go -completion_regexp=Get

(The numbers reported by this benchmark are problematic,
which is why I measured call times directly; see
https://github.com/golang/go/issues/53798.)

Results:
before (n=4727) max =  21ms, 90% = 4.4ms, median = 19us
after  (n=6282) max = 2.3ms, 90% = 25us,  median = 14us

The increased number of calls to the function after the change
is due to a longstanding bug in the benchmark: each iteration of
the b.N loop doesn't do a fixed amount of work, it does as much
as it can in 10s. Thus making the code faster simply causes
the benchmark to spend the same amount of time on other parts of
the program--such as the loop that calls FindDeclAndField.

See https://go-review.googlesource.com/c/tools/+/221021 for
background on the previous implementation.

Change-Id: I745ecc4e65378fbe97f456228cafba84105b7e49
Reviewed-on: https://go-review.googlesource.com/c/tools/+/416880
Auto-Submit: Alan Donovan <adonovan@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2022-07-13 14:49:57 +00:00
..
builtin.go lsp/completion: support completing to Elem() types 2021-06-09 15:58:30 +00:00
completion.go internal/span: make NewRange accept File, not FileSet 2022-07-06 19:40:50 +00:00
deep_completion.go all: fix spelling 2022-04-04 19:50:34 +00:00
deep_completion_test.go
definition.go internal/span: make NewRange accept File, not FileSet 2022-07-06 19:40:50 +00:00
format.go internal/lsp/cache: move PosTo{Decl,Field} out of cache 2022-07-13 14:49:57 +00:00
fuzz.go internal/completion: default to regular completion for f.Fuzz without f.Add 2022-03-16 01:41:57 +00:00
keywords.go internal/lsp/source/completion: remove "completion_" prefix from files 2020-10-30 17:45:15 +00:00
labels.go internal/lsp/source/completion: remove "completion_" prefix from files 2020-10-30 17:45:15 +00:00
literal.go lsp/completion: fix func literals with type params 2022-04-26 14:19:52 +00:00
package.go internal/span: make NewRange accept File, not FileSet 2022-07-06 19:40:50 +00:00
package_test.go internal/lsp/source/completion: suggest only valid package names 2021-04-27 19:53:45 +00:00
postfix_snippets.go gopls: add range over channel postfix completion 2022-07-01 14:13:36 +00:00
printf.go all: fix spelling 2022-04-04 19:50:34 +00:00
printf_test.go internal/lsp/source/completion: remove "completion_" prefix from files 2020-10-30 17:45:15 +00:00
snippet.go internal/lsp: add snippet completion for function type parameters 2022-03-09 15:55:38 +00:00
statements.go gopls, internal/lsp: gofmt 2022-04-12 17:41:31 +00:00
util.go internal/span: make NewRange accept File, not FileSet 2022-07-06 19:40:50 +00:00
util_test.go