Go 1.22 binary size improvements?

229 views
Skip to first unread message

cpu...@gmail.com

unread,
Feb 9, 2024, 6:46:48 AM2/9/24
to golang-nuts
I was surprised to see my application (MacOS binary) go from 93MB to 81MB (no PGO). Release notes don't mention this.

Has anyone made similar experience or knows why that is?

Thanks,
Andi

qiulaidongfeng

unread,
Feb 9, 2024, 7:23:20 AM2/9/24
to golang-nuts
Quick search to find go1.22 as follows  CL  like  https://go-review.googlesource.com/c/go/+/521615   reduces binary size. However, from 93MB to 81MB, I think it is most likely the result of the joint efforts of many CLs.

cpu...@gmail.com

unread,
Feb 9, 2024, 8:11:25 AM2/9/24
to golang-nuts
It's really surprising (positively). Compiling with -ldflags='-s -w' if anyone wants to try this.

cpu...@gmail.com

unread,
Feb 9, 2024, 8:16:26 AM2/9/24
to golang-nuts
Just gave it another try cross-compiling to Arm on Linux. This time binary size stays the same. Sorry if my excitement was too premature.

Brian Candler

unread,
Feb 9, 2024, 8:22:36 AM2/9/24
to golang-nuts
Have you at any point set CGO_ENABLED=0 ?

What does "ldd /path/to/binary" show on both the old (larger) and new (smaller) binaries?  Maybe one is dynamically linked and the other statically linked?

cpu...@gmail.com

unread,
Feb 9, 2024, 9:43:35 AM2/9/24
to golang-nuts
Yes, CGO is disabled. ldd doesn't seem to exist on MacOS:

make build && ls -la ./evcc && otool -L ./evcc

1.21.7

CGO_ENABLED=0 go build -v -tags=release -trimpath -ldflags='-X github.com/evcc-io/evcc/server.Version=0.124.1 -X github.com/evcc-io/evcc/server.Commit=596071b42 -s -w'
-rwxr-xr-x  1 andig  staff  93824418  9 Feb 14:38 ./evcc
./evcc:
    /usr/lib/libSystem.B.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libresolv.9.dylib (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 0.0.0, current version 0.0.0)

1.22.0

CGO_ENABLED=0 go build -v -tags=release -trimpath -ldflags='-X github.com/evcc-io/evcc/server.Version=0.124.1 -X github.com/evcc-io/evcc/server.Commit=596071b42 -s -w'
-rwxr-xr-x  1 andig  staff  81119026  9 Feb 14:37 ./evcc
./evcc:
    /usr/lib/libSystem.B.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libresolv.9.dylib (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 0.0.0, current version 0.0.0)

Brian Candler

unread,
Feb 9, 2024, 10:15:41 AM2/9/24
to golang-nuts
Sorry you're right, it's "otool -L" for macOS.

And you say that if you cross-compile from Linux with 1.22.0 to build a macOS binary, you get a size approx 93.8MB as well? Very odd.

Reply all
Reply to author
Forward
0 new messages