Build time slow with cgo dependencies
Asked Answered
R

0

6

I have a Go program that uses the Qt wrapper library https://github.com/therecipe/qt. Unfortunately, the build time gets extremely high with it (assuming it's the go part)

go build -i .  // takes about 14 seconds
go run .       // takes about 8 seconds

After running either of the above commands I get the pre-compiled dependencies in my $GOPATH/pkg/linux_amd64/github.com/therecipe/qt as .a files so they are not rebuilding each time.

I've tried to use ccache and Gold linker /usr/bin/ld.gold as described in https://github.com/therecipe/qt/wiki/Faster-builds-(Linux) but it didn't improve anything. Also this Qt wrapper ships with its own build tool qtdeploy which I tried but it is roughly the same build time.

System I'm running on:

go version go1.14.4 linux/amd64
Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
16GB Ram

Would anyone know if it's possible to improve the build time at least a bit?

EDIT:

Running go build -x . shows that the biggest time consumer is the following command

~/.go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=k8lYa6JYqRdCY9Gyt0jX/16myMybByG5X6rOfaRpS/WHdW2kCTfMCZs2I4x9WE/k8lYa6JYqRdCY9Gyt0jX -extld=g++ ~/.cache/go-build/b5/b5e47b7f77c2df06ba69937dc8b1399b1289b7c90d2e08b3341fdf13e119c860-d
Raskin answered 5/8, 2020 at 10:51 Comment(10)
Can you run go build -x and inspect the build log? -x makes go build write out every step it takes to produce the executable image.Fernando
@Fernando I updated the question with the command that consumes the most time from go build -xRaskin
try CGO_ENABLED=0 go build . Misusage
-extld=g++ hints at that it's gcc's linker which is slow. I have no immediate idea on how to time it (except for wrapping g++ with a custom script in the PATH which would somehow instrument a call to the real g++ to help to find out where it's spending).Fernando
@kozmo, how disabling cgo is supposed to work when the program explicitly uses C or C++ library code?Fernando
Have you tried running perf to analyze what g++ is doing?Carin
https://mcmap.net/q/600869/-why-is-compiling-with-cgo_enabled-0-slowerMisusage
@Misusage Building with teh CGO_ENABLED=0 flag will result in an output (without doing any compiling): package test imports github.com/therecipe/qt/core: build constraints exclude all Go files in /home/dan/Golang/Programs/src/github.com/therecipe/qt/core. As @Fernando already pointed out, how would that even work if it uses C/C++ library code?Raskin
@GustavoKawamoto how would I analyze what g++ is actually doing?Raskin
perf is a linux tool for profiling, you can find it in the linux-tools package in Ubuntu. You can use it by recording the events while running your command (perf record) or see in real time (perf top), both using root. The perf tool will profile the calls and give you an idea where your application is hogging. For more info, see perf.wiki.kernel.org/index.php/TutorialCarin

© 2022 - 2024 — McMap. All rights reserved.