I'm using go 1.11.2 and the build time for a small project (less than 10k 
LOC) is about 2 seconds when I just run `go build main.go`


I tried searching online for tips and tricks to improve compile times, but 
most advice is outdated. For example, some people recommend passing '-i' or 
something to make the compiler cache intermediate results and reuse them 
for subsequent builds, but the recent versions of the compiler now do this 
by default.


Of course, if we count all the libraries I'm using (vendored) the LOC count 
probably goes above 100k, but I don't think this is an issue, because of 
caching, and because when I run `go build -x` I found out that the majority 
of the build time is spent waiting on the linker. The actual compilation 
happens reasonably quickly; it's the linker that's taking most of the time.


After some Googling, I found I could pass '-s -w' to the linker to make it 
skip on embedding debug information (Documented here 
<https://golang.org/cmd/link/>)


    go build -ldflags '-s -w' main.go


Doing this, the build times dropped from 2 seconds to around 1 second.


I'm not sure what other options are available (if any?) to reduce the time 
required for the linker.


Is there perhaps a way to use another linker that is faster? At least 
during development.


I'd really like the whole process to finish in under 400ms, so that the 
edit-build-run cycle can be made to feel *almost* as if building is 
near-instant.


If it helps, by passing '-v' to the linker, it outputs something like this:


    HEADER = -H1 -T0x1001000 -D0x0 -R0x1000

    0.00 deadcode

    0.06 pclntab=2652352 bytes, funcdata total 516696 bytes

    0.06 dodata

    0.07 symsize = 0

    0.07 symsize = 0

    0.08 dynreloc

    0.10 reloc

    0.11 asmb

    0.11 codeblk

    0.13 datblk

    0.14 sym

    0.15 headr

    0.24 host link: "clang" "-m64" "-Wl,-headerpad,1144" "-Wl,-no_pie" 
"-Wl,-pagezero_size,4000000" "-o" "[....]/exe/a.out" "-Qunused-arguments" 
"[....]/go-link-718013919/go.o" "[....]/go-link-718013919/000000.o" 
"[....]/000001.o" "[....]/000002.o" "[....]/000003.o" "[....]/000004.o" 
"[....]/000005.o" "[....]/000006.o" "[....]/go-link-718013919/000007.o" 
"[....]/go-link-718013919/000008.o" "[....]/go-link-718013919/000009.o" 
"[....]/go-link-718013919/000010.o" "[....]/go-link-718013919/000011.o" 
"[....]/go-link-718013919/000012.o" "[....]/go-link-718013919/000013.o" 
"[....]/go-link-718013919/000014.o" "[....]/go-link-718013919/000015.o" 
"[....]/go-link-718013919/000016.o" "[....]/go-link-718013919/000017.o" 
"[....]/go-link-718013919/000018.o" "[....]/go-link-718013919/000019.o" 
"[....]/go-link-718013919/000020.o" "[....]/go-link-718013919/000021.o" 
"[....]/go-link-718013919/000022.o" "[....]/go-link-718013919/000023.o" 
"[....]/go-link-718013919/000024.o" "[....]/go-link-718013919/000025.o" 
"[....]/go-link-718013919/000026.o" "[....]/go-link-718013919/000027.o" 
"[....]/go-link-718013919/000028.o" "[....]/go-link-718013919/000029.o" 
"[....]/go-link-718013919/000030.o" "[....]/go-link-718013919/000031.o" 
"-g" "-O2" "-g" "-O2" "-g" "-O2" "-g" "-O2" "-lpthread" "-g" "-O2" 
"-framework" "CoreFoundation" "-framework" "Security" "-no-pie"

    0.39 cpu time

    144230 symbols

    321736 liveness data


It seems like this line `0.39 cpu time` indicates the total time spent by 
the linker.


Incidentally, when I pass `-v` only without `-s -w`, the output of the 
linker becomes `1.52 cpu time`, So `-s -w` shaves off a whole second (and 
then some).



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to