open-amt-cloud-toolkit/rpc-go

Reduce Size of Go Binary and DLL/SO Files

tongsean9807 opened this issue · 1 comments

Problem Statement

This request aims to reduce the size of the Go binary (rpc-go.exe) and the associated DLL/SO files. By decreasing the file sizes, we can improve the user experience, especially for users working with corporate laptops that may have limited disk space and have plenty of IT installed applications.

Go Build with -ldflags "-w -s"

Compile with -ldflags="-w -s": Implementing this flag combination will strip debug and symbol table information from the binaries without affecting the functionality of rpc-go.exe. This will significantly reduce the size of the binaries while maintaining full functionality.

ldflags

The go build command allows the use of linker flags to modify the way the final binary is created. Two particularly useful flags for reducing binary size are:

-w: This flag omits the DWARF symbol table, effectively removing debugging information.
-s: This strips the symbol table and debug information from the binary.

More information:
The -w turns off DWARF debugging information: you will not be able to use gdb on the binary to look at specific functions or set breakpoints or get stack traces, because all the metadata gdb needs will not be included. You will also not be able to use other tools that depend on the information, like pprof profiling.

The -s turns off generation of the Go symbol table: you will not be able to use go tool nm to list the symbols in the binary. strip -s is like passing -s to -ldflags but it doesn't strip quite as much. go tool nm might still work after strip -s. I am not completely sure.

Before reduction
image

After reduction
image

Reference:

  1. https://www.codingexplorations.com/blog/reducing-binary-size-in-go-strip-unnecessary-data-with-ldflags-w-s#:~:text=The%20go%20build%20command%20allows,debug%20information%20from%20the%20binary.
  2. https://stackoverflow.com/questions/22267189/what-does-the-w-flag-mean-when-passed-in-via-the-ldflags-option-to-the-go-comman

Hey! Thanks for the feedback. We actually already implement this for our Release artifacts. You can see the line here. As Mike replied in the PR, the areas modified in your PR are just part of our validation efforts. Starting with RPC-Go v2.26.0 in early February, we stopped including debug symbols as part of the Release artifacts. You'll see that our file sizes are roughly equal between your screenshot and the artifacts (~7mb).

Although, we do not currently release Library .dll/.so artifacts. That is something in our backlog that we want to tackle.

I will make a note to include these optional build flags in our documentation to give folks the choice if they manually opt to build the executable or library rather than use the tagged release artifacts. I'll tag this issue when updated.