Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: The completion feature of gopls has high latency when dealing with very large packages. #69631

Open
pand5a opened this issue Sep 26, 2024 · 10 comments
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@pand5a
Copy link

pand5a commented Sep 26, 2024

gopls version

golang.org/x/tools/gopls v0.16.2

go env

GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH=''
GOPRIVATE=''
GOROOT='/usr/lib/go'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1211819663=/tmp/go-build -gno-record-gcc-switches'

What did you do?

The completion feature of gopls has high latency when dealing with very large packages. Below are the completion latencies observed in the sql package of the cockroachdb source code.

The code statistics for the sql package in cockroachdb are as follows:

cloc cockroach/pkg/sql             
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                            2974         148944         228093        1355540
-------------------------------------------------------------------------------
SUM:                          3504         152684         236151        1424410
-------------------------------------------------------------------------------

The distribution of the main function latencies in gopls completion is as follows:

completion.go:54: elapsed 489.202836ms,        Completion entry, 
completion.go:484: elapsed 477.769576ms,            NarrowestPackageForFile, 
snapshot.go:87: elapsed 477.709921ms,                   TypeCheck, 
check.go:415: elapsed 37.059356ms,                          getPackageHandles,
check.go:422: elapsed  51.8002ms,                           getImportGraph, 
check.go:606: elapsed   194.72µs,                           getImportPackage
check.go:629: elapsed 387.29934ms,                          checkPackage, 
check.go:1614: elapsed 385.05347ms,                             check.Files

The most time-consuming part is ** check.go:1614: elapsed 385.05347ms, check.Files, 288 **, which takes around 385 milliseconds. Adding other operations, the total latency reaches 489.202836ms. This results in noticeable lag during use.

What did you see happen?

During each completion, a TypeCheck is performed on the package containing the currently modified file, and this process is very time-consuming for large packages.

What did you expect to see?

Is it possible to only re-parse the current file during completion, rather than performing a TypeCheck on the entire package? This would allow completion to be completed in milliseconds even for large packages.

Editor and settings

nvim

Logs

No response

@pand5a pand5a added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Sep 26, 2024
@gopherbot gopherbot added this to the Unreleased milestone Sep 26, 2024
@pand5a
Copy link
Author

pand5a commented Sep 26, 2024

Normal speed: in cockroachdb pkg/cmd/dev/main.go
ok

Slow speed: in cockroachdb pkg/sql package
slow

@findleyr
Copy link
Member

Thanks. There is currently a hard speed-limit for this completion, based on the fact that we cannot start streaming partial results (at least for VS Code: microsoft/vscode#105870), and therefore must await type checking of this very large package.

However, I'm currently working on a series of optimizations that will help a bit, getting us closer to the theoretical speed limit. You might try installing gopls at master (git clone https://go.googlesource.com/tools && cd tools/gopls && go install), as it should be a bit better.

I'm also curious if your vim lsp client has partial results support. Is that something you could investigate?

@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Sep 26, 2024
@pand5a
Copy link
Author

pand5a commented Sep 28, 2024

Thank you for your reply. I am on a long vacation. When I come back, I will try the code on the master.

I'm also curious if your vim lsp client has partial results support. Is that something you could investigate?

ok i will try it.

@pand5a
Copy link
Author

pand5a commented Sep 28, 2024

@pand5a
Copy link
Author

pand5a commented Sep 29, 2024

@findleyr
One thing I don't quite understand is why typecheck is required for every completion. Is it to check if there are any errors?

@findleyr
Copy link
Member

@pand5a sorry for the slow response. Type checking is required to resolve the lexical scope of the current package.

We could theoretically go faster if we only type checked the current function body+declarations, but we don't support that (it would require quite a hack around the current type checker APIs).

@pand5a
Copy link
Author

pand5a commented Nov 4, 2024

Okay, I understand. Thank you very much.

@jheroy
Copy link

jheroy commented Nov 28, 2024

We could theoretically go faster if we only type checked the current function body+declarations, but we don't support that (it would require quite a hack around the current type checker APIs).

@findleyr Could you provide an idea or approach for a hack? I'd like to implement it myself for temporary personal use, Thanks.

@jheroy
Copy link

jheroy commented Dec 11, 2024

Thanks. There is currently a hard speed-limit for this completion, based on the fact that we cannot start streaming partial results (at least for VS Code: microsoft/vscode#105870), and therefore must await type checking of this very large package.

I'm also curious if your vim lsp client has partial results support. Is that something you could investigate?

Saghen/blink.cmp#395 (comment)
It seems that partial result is not implemented in gopls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants