Skip to content

Commit 45fc3b0

Browse files
committed
scripts/build: set grpcnotrace build-tag to reduce binary size
Reduces the binary size (39810466 - 39289106 => 521360 (521 kb) We only import google.golang.org/grpc as an indirect dependency, and do not make gRPC connections. grpcnotrace avoids importing golang.org/x/net/trace, which in turn enables dead code elimination, which can yield 10-15% improvements in binary size when tracing is not needed. see https://github.com/grpc/grpc-go/blob/v1.81.1/trace_notrace.go#L23-L25 Before: ls -l ./build/docker-darwin-arm64 -rwxr-xr-x 1 thajeztah staff 39810466 May 22 11:44 ./build/docker-darwin-arm64* ls -lh ./build/docker-darwin-arm64 -rwxr-xr-x 1 thajeztah staff 38M May 22 11:44 ./build/docker-darwin-arm64* After: ls -l ./build/docker-darwin-arm64 -rwxr-xr-x 1 thajeztah staff 39289106 May 22 11:45 ./build/docker-darwin-arm64* ls -lh ./build/docker-darwin-arm64 -rwxr-xr-x 1 thajeztah staff 37M May 22 11:45 ./build/docker-darwin-arm64* Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 9712e53 commit 45fc3b0

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

scripts/build/.variables

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ fi
9898
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
9999
GO_LDFLAGS="$GO_LDFLAGS -linkmode external -extldflags -static"
100100
fi
101+
102+
# We only import google.golang.org/grpc as an indirect dependency, and
103+
# do not make gRPC connections.
104+
#
105+
# grpcnotrace avoids importing golang.org/x/net/trace, which in turn enables
106+
# dead code elimination, which can reduce binary size when tracing is not needed.
107+
#
108+
# see https://github.com/grpc/grpc-go/blob/v1.81.1/trace_notrace.go#L23-L25
109+
GO_BUILDTAGS="$GO_BUILDTAGS grpcnotrace"
110+
101111
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ]; then
102112
# compiling statically with CGO enabled requires osusergo and netgo to be set.
103113
GO_BUILDTAGS="$GO_BUILDTAGS osusergo netgo"

0 commit comments

Comments
 (0)