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.

Place
Golab #1
Length
60 min
When
November 21st, 2023
17:00

Abstract

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.

Slides

Video

Made by DEVELER

GoLab is a conference made by Develer.
Develer is a company based in Campi Bisenzio, near Florence. Our motto is : "Technology to give life to your products". We produce hardware and software to create exceptional products and to improve industrial processes and people's well being.
In Develer we have passion for the new technologies and we offer our clients effective solutions that are also efficient, simple and safe for the end users. We also believe in a friendly and welcoming environment where anybody can give their contribution. This passion and this vision are what we've been driven to organize our conference "made by developers for developers".


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.
©2024 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS