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: high cpu usage when make changes in package which have some cgo package dependents #50151

Open
zhuah opened this issue Dec 14, 2021 · 15 comments
Labels
gopls/performance Issues related to gopls performance (CPU, memory, etc). 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

@zhuah
Copy link

zhuah commented Dec 14, 2021

gopls version

Build info

golang.org/x/tools/gopls v0.7.3
golang.org/x/tools/[email protected] h1:Lru57ht8vtDMouRskFC085VAjBAZRAISd/lwvwOOV0Q=
github.com/BurntSushi/[email protected] h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/google/[email protected] h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/[email protected] h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/[email protected] h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/[email protected] h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/[email protected] h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/[email protected] h1:hk7xRoeg0CG1nRLsd5BZLDUgVpA9bnKylGk1p2/BPH0=
golang.org/x/[email protected] h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
honnef.co/go/[email protected] h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
mvdan.cc/[email protected] h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
mvdan.cc/xurls/[email protected] h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=

go env

What did you do?

I'm work on a monorepo with some cgo packages in VSCode, when i modify the core package that cgo packages depends on, the cpu usage goes 100%.
I have tried inspect it with

pstree `pgrep gopls`

which shows that there are several clang process created by go list and cgo command.

i must run pkill clang instantly when cpu usage goes 100%, but when i am keep editing, the cpu goes 100% again.
so i'm wondering that is there a way to disable cgo in gopls or let gopls do not compile cgo package.

What did you expect to see?

What did you see instead?

-+- 00827 zhuah /Users/zhuah/go/bin/gopls -mode=stdio
 \-+- 15602 zhuah go list -modfile=TMPDIR/go.5c768b47e65a0399512ae86c5ba9023517f92436988adbfe6f37ff2addbfa6cc.1607909778.mod -mod=readonly -overlay=TMPDIR/gopackages-2412941141/overlay.json -e -json -compiled=true -test=true -export=false -deps=true -find=false -- PACKAGELIST
   |-+- 15634 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b300/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b300/ -g -O2 CGOFILE CGODIR CGOFILE CGOFILE
   | \-+- 15831 zhuah /usr/bin/clang -w -Wno-error -oTMPDIR/go-build833139357/b300//_cgo_.o -gdwarf-2 -c -ferror-limit=0 -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Qunused-arguments -fno-builtin -I TMPDIR/go-build833139357/b300/ -arch x86_64 -m64 -fno-lto -O0 -fdiagnostics-color=never -I . TMPDIR/cgo-gcc-input-2621109915.c
   |   \--- 15833 zhuah /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cgo-gcc-input-2621109915.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=lldb -target-linker-version 609 -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I TMPDIR/go-build833139357/b300/ -I . -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wno-error -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -w -fdebug-compilation-dir CGODIR -ferror-limit 0 -fmessage-length 0 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fno-builtin -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -o TMPDIR/go-build833139357/b300//_cgo_.o -x c TMPDIR/cgo-gcc-input-2621109915.c
   |-+- 15650 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b096/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b096/ -g -O2 -x objective-c CGOFILE
   | \-+- 15808 zhuah /usr/bin/clang -w -Wno-error -oTMPDIR/go-build833139357/b096//_cgo_.o -gdwarf-2 -c -ferror-limit=0 -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Qunused-arguments -fno-builtin -I TMPDIR/go-build833139357/b096/ -x objective-c -arch x86_64 -m64 -fno-lto -O0 -fdiagnostics-color=never -I . TMPDIR/cgo-gcc-input-556442778.c
   |   \--- 15811 zhuah /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cgo-gcc-input-556442778.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=lldb -target-linker-version 609 -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I TMPDIR/go-build833139357/b096/ -I . -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wno-error -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -w -fdebug-compilation-dir CGODIR -ferror-limit 0 -fmessage-length 0 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fno-builtin -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.15.0 -fobjc-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -o TMPDIR/go-build833139357/b096//_cgo_.o -x objective-c TMPDIR/cgo-gcc-input-556442778.c
   |-+- 15656 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b083/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b083/ -g -O2 -Wno-int-to-pointer-cast CGOFILE
   | \-+- 15801 zhuah (clang)
   |   \--- 15802 zhuah (clang)
   |-+- 15657 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b126/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b126/ -g -O2 -x objective-c -ICGOPACKAGE -Wno-deprecated-declarations CGOFILE
   | \-+- 15659 zhuah (clang)
   |   \--- 15665 zhuah (clang)
   |-+- 15677 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b137/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b137/ -g -O2 -x objective-c CGOFILE
   | \-+- 15681 zhuah (clang)
   |   \--- 15684 zhuah (clang)
   |-+- 15746 zhuah /usr/bin/clang++ -I CGODIR -fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=TMPDIR/go-build833139357/b100=/tmp/go-build -gno-record-gcc-switches -fno-common -I TMPDIR/go-build833139357/b100/ -g -O2 -std=c++2a -fno-exceptions -fno-rtti -o TMPDIR/go-build833139357/b100/_x003.o -c CPPFILE
   | \--- 15748 zhuah /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CPPFILE -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 609 -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I CGODIR -I TMPDIR/go-build833139357/b100/ -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O2 -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -std=c++2a -fdeprecated-macro -fdebug-compilation-dir CGODIR -fdebug-prefix-map=TMPDIR/go-build833139357/b100=/tmp/go-build -ferror-limit 19 -fmessage-length 0 -pthread -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fno-rtti -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-implicit-modules -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fno-common -fno-caret-diagnostics -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o TMPDIR/go-build833139357/b100/_x003.o -x c++ CPPFILE
   |-+- 15773 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b138/ -importpath CGOPACKAGE -- -I TMPDIR/go-build833139357/b138/ -g -O2 -x objective-c CGOFILE CGOFILE
   | \-+- 15779 zhuah /usr/bin/clang -E -dM -arch x86_64 -m64 -I TMPDIR/go-build833139357/b138/ -O2 -x objective-c -I . TMPDIR/cgo-gcc-input-2816487396.c
   |   \--- 15781 zhuah /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cgo-gcc-input-2816487396.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 609 -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I TMPDIR/go-build833139357/b138/ -I . -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O2 -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -fdebug-compilation-dir CGODIR -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.15.0 -fobjc-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -vectorize-loops -vectorize-slp -dM -o - -x objective-c TMPDIR/cgo-gcc-input-2816487396.c
   \-+- 15803 zhuah /usr/local/Cellar/gotip/pkg/tool/darwin_amd64/cgo -objdir TMPDIR/go-build833139357/b075/ -importpath IMPORTPATH -- -I TMPDIR/go-build833139357/b075/ -g -O2 -Wno-deprecated-declarations -x objective-c CGOFILE
     \-+- 15810 zhuah /usr/bin/clang -w -Wno-error -oTMPDIR/go-build833139357/b075//_cgo_.o -gdwarf-2 -c -ferror-limit=0 -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Qunused-arguments -fno-builtin -I TMPDIR/go-build833139357/b075/ -Wno-deprecated-declarations -x objective-c -arch x86_64 -m64 -fno-lto -O0 -fdiagnostics-color=never -I . TMPDIR/cgo-gcc-input-1994762254.c
       \--- 15813 zhuah /usr/bin/clang -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cgo-gcc-input-1994762254.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=lldb -target-linker-version 609 -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I TMPDIR/go-build833139357/b075/ -I . -I/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O0 -Wno-error -Wno-unknown-warning-option -Wno-unneeded-internal-declaration -Wno-unused-function -Wno-deprecated-declarations -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -w -fdebug-compilation-dir CGODIR -ferror-limit 0 -fmessage-length 0 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fno-builtin -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.15.0 -fobjc-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -o TMPDIR/go-build833139357/b075//_cgo_.o -x objective-c TMPDIR/cgo-gcc-input-1994762254.c

Editor and settings

Logs

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

zhuah commented Dec 14, 2021

I have compiled a modified version of gopls with cgo disabled:
comment -compiled=true option at https://github.com/golang/tools/blob/master/go/packages/golist.go#L812 and
force SetUsesCgo function to return false at https://github.com/golang/tools/blob/master/internal/typesinternal/types.go#L16

it may better to add a command line option -disable-cgo to gopls.

@hyangah
Copy link
Contributor

hyangah commented Dec 14, 2021

Thanks for the investigation and experiment @zhuah

I wonder what's breaking when cgo is completely disabled. (related to #35721)
It sounds like your are ok with completely disabling cgo. Does it mean that the cgo packages aren't in your active working set of packages?

@findleyr we have multiple cgo related issues (e.g. #43560) and I don't know where we are. what do you think about keeping track of cgo functionality & performance improvement under an uber issue?

@hyangah hyangah added the gopls/performance Issues related to gopls performance (CPU, memory, etc). label Dec 14, 2021
@zhuah
Copy link
Author

zhuah commented Dec 15, 2021

I wonder what's breaking when cgo is completely disabled. (related to #35721)

There are still an issue could not import C (no required module provides package "C") on the import "C" statement.
I have tried but couldn't solve it.

Does it mean that the cgo packages aren't in your active working set of packages?

Yes, but even for cgo packages, it's also acceptable for me without cgo type checking, just like GoLand.

It sounds like your are ok with completely disabling cgo.

Yes, it's already good enough for me. Also, after disabled cgo, there are large improvement of setting workspace speed.

@hyangah hyangah modified the milestones: Unreleased, gopls/on-deck Dec 16, 2021
@findleyr
Copy link
Member

findleyr commented Jan 4, 2022

This needs to be further investigated, but probably won't make the v0.7.5 milestone. Moving to v0.8.0.

@RSWilli
Copy link

RSWilli commented Aug 22, 2024

Hey I'm the maintainer for https://github.com/go-gst/go-gst , the gstreamer bindings for golang. Also the accompanying https://github.com/go-gst/go-glib for the required GLib bindings.

Since gstreamer is quite a large project, both projects contain a lot of C code and go exported functions. A simple compile from scratch takes around 1-2min depending on the machine.

With the current language server this turns into a major headache, because after some typing in the package my machine has around 40-100 gcc processes running and I have to leave the machine to do it's thing, before I can start coding again.

I have auto formatting turned on and auto save on focus change, which wasn't a problem in any other golang projects of mine.

Also this might be a matter of project structure, in that case I'm happy to hear some improvements.

Yes, but even for cgo packages, it's also acceptable for me without cgo type checking, just like GoLand.

Since these is are repos for creating C FFIs, turning of cgo is not really an option for the LSP.

@findleyr
Copy link
Member

@RSWilli I drafted a speculative CL recently that 'fakes' cgo: the type checker just treats C bindings as unknown, but doesn't report issues, and functionality works as normal for non-cgo references. In other words, if I'm in a package that imports "C", gopls won't know anything about "C.Foo" (but won't complain), and other references such as "pkg.Bar" will work as expected.

So, this would theoretically reduce the CPU usage and latency of gopls significantly in your workspace, at the expense of losing information about C dependencies.

Would such a feature be desirable for you?

@RSWilli
Copy link

RSWilli commented Aug 22, 2024

@findleyr not really for the bindings, because I need the type C information. Every struct in that project is a go struct that wraps a C pointer, about every method on the structs uses the unsafe package and calls into C in one way or another.

In projects depending on go-gst the compilation situation is similar though. There such a feature is very useful, because the importing package doesn't touch cgo at all, and only type checking the go method calls is enough.

EDIT: in projects depending on go-gst the situation is not hat bad, because the compiled go-gst is cached after the first run.

@RSWilli
Copy link

RSWilli commented Aug 22, 2024

I don't always need the type info to be there immediately though. If they arrive eventually this would be enough. I have the feeling though that the LSP starts a new "compilation" of the project, even though the last one hasn't finished yet. The old one is also never invalidated, which end up in a big herd of running c compiling steps.

2mins of importing new modules and typing random code, hovering over some symbols leaves me with this:

image

and several gcc processes in htop:

image

After around 30mins of programming I regularly reach 100% load on my machine, mostly coming from gcc.

@mpx
Copy link
Contributor

mpx commented Aug 26, 2024

The worst dependencies I commonly come across are:
github.com/go-gl/gl and github.com/gotk3/gotk3 (and their related packages).
They can cause >15m of compilation on a reasonably fast laptop making gopls mostly unusable.

These compilations are also retried with alarming frequency, it's not clear to me this data is properly cached, or it's too easy to trigger a recompilation. Given the extreme cost it would be great to reuse the data for as long as possible. IIUC - 'gopls' (or the toolchain) shouldn't need to recalculate anything provided the CGO using module and compiler versions don't change.

@fiatjaf
Copy link

fiatjaf commented Aug 26, 2024

The same happens for me with https://github.com/diamondburned/gotk4. It's the only package I remember triggering this gopls behavior. Initial go build compilation takes a long time, but afterwards it's fast, so I think it's being properly cached and that's independent of gopls.

But then often, in unclear circumstances while editing files, my computer would suddenly have >15 cc1 processes running and using all the CPU and the LSP functionality would stop working in the editor -- but if I do killall cc1 everything is killed and one second later I'm happy again.

@RSWilli
Copy link

RSWilli commented Aug 26, 2024

@mpx @fiatjaf Do you have autoformatting enabled? I feel like this issue is split between the autoformatter and the language server.

Since go-gst is a project depending on glib-2.0 (and https://github.com/go-gst/go-glib is a fork of https://github.com/gotk3/gotk3) these are exactly the projects that I struggle with.

htop views like this are common for me:

image

A single gcc compilation of gstreamer/glib isn't cheap.

@RSWilli
Copy link

RSWilli commented Aug 26, 2024

Initial go build compilation takes a long time, but afterwards it's fast, so I think it's being properly cached and that's independent of gopls.

Sadly the caching doesn't work if you tinker in the project itself, then you are stuck with waiting after every change. The LSP/cache seems to be clever though and only really recompile when an import changes.

@fiatjaf
Copy link

fiatjaf commented Aug 26, 2024

Do you have autoformatting enabled?

I use gofumpt for autoformatting, but how can that be related?


I had some things wrong on my comment before, indeed the big number of processes I get are also cc1, not gopls as previously stated.

I also use goimports and maybe that can be somewhat related, because I can remember some correlation between the issue happening and I using some gtk library in a new file where it hadn't been imported before. But I can't reproduce it right now.

@mpx
Copy link
Contributor

mpx commented Aug 27, 2024

The caching issue may just be due to me only interacting with some projects less frequently. While the versions and toolchain haven't changed, it might be a week or so since I last touched them. That said, I do recall situations where it appears to happen multiple times within a week.

Yes, I'm typicaly not attempting to use gopls for anything more than auto formatting on save. This causes saving to be excruciatingly slow - I typically disable gopls so I can make quick edits, or use an editor that doesn't recognise Go.

@RSWilli
Copy link

RSWilli commented Aug 27, 2024

I use gofumpt for autoformatting, but how can that be related?

The language server gopls provides (citing settings) "intellisense, code navigation, refactoring, formatting and diagnostics for go".

"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.formatOnType": true,

This causes saving to be excruciatingly slow

This is the same for me. I think that the formatting and autocompletion are somehow interfering with each other. If I disable formatOnType the high CPU usage seems to be less of a problem for tinkering in the bindings library itself. (not quite gone though, but way better)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/performance Issues related to gopls performance (CPU, memory, etc). 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

7 participants