Getting the most out of dead code elimination

The Go linker optimizes the size of your binaries by removing functions that are never called, a process called "deadcode elimination". Some uses of reflection can, however, partially disable it. Learn how you can determine if your program is suffering from this problem and how you can solve it.

Golab #1
60 min
November 21st, 2023


The Go linker tries to remove functions that are never called in a step called "deadcode elimination". During this step the linker visits the symbol graph, starting from the entry point of the program, and marks every reachable symbol, i.e. everything that is called either directly or indirectly, through a function pointer or through an interface. All unreachable symbols are then removed and will not be included in the final executable.

However if during this process if the linker sees that one of the following methods are reachable:
- reflect.Value.Method
- reflect.Value.MethodByName
- reflect.Type.Method
- reflect.Type.MethodByName

then a more relaxed version of deadcode elimination is run and every exported method of any reachable type is considered reachable. This is done because, when using one of those methods, it is possible for the program to call an arbitrary method of any reachable type, dependent on input seen at runtime.

It is possible to determine if this degraded version of deadcode elimination is being used by examining the output of the `-dumpdep` flag of the linker. This can also be used to determine where the problem lies in a program with many dependencies.

In many cases these problematic reflection methods can be avoided without loss of functionality and without breaking backwards compatibility. I will show how to do this using the real world examples of three popular projects: Delve, Cobra and Starlark.

Stay connected

You can subscribe to our newsletter to stay updated on the latest news and announcements. Follow us on social media to join our community and engage with other attendees, speakers, and staff. We're on Twitter, Mastodon, Youtube and Facebook. Don't forget to use our official hashtag #GoLab2023 to share your experience and connect with other attendees on social media.

Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2023 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS