GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing go.dev — Finding unreachable functions with deadcode - The Go Programming Language The Go BlogFinding unreachable functions with deadcode
Alan Donovan Functions that are part of your project’s source code but can never be
reached in any execution are called “dead code”, and they exert a drag
on codebase maintenance efforts.
Today we’re pleased to share a tool named
ExampleOver the last year or so, we’ve been making a lot of changes to the structure of gopls, the language server for Go that powers VS Code and other editors. A typical change might rewrite some existing function, taking care to ensure that its new behavior satisfies the needs of all existing callers. Sometimes, after putting in all that effort, we would discover to our frustration that one of the callers was never actually reached in any execution, so it could safely have been deleted. If we had known this beforehand our refactoring task would have been easier. The simple Go program below illustrates the problem:
When we execute it, it says hello:
It’s clear from its output that this program executes the When we run deadcode on this program, the tool tells us that the
With this knowledge, we can safely remove both functions,
along with the The tool can also explain why the
The output is designed to be easy to read on a terminal, but you can
use the How it worksThe It then uses an algorithm called
Rapid Type Analysis (RTA)
to build up the set of functions that are reachable,
which is initially just the entry points of each RTA looks at the statements in the body of each reachable function to gather three kinds of information: the set of functions it calls directly; the set of dynamic calls it makes through interface methods; and the set of types it converts to an interface. Direct function calls are easy: we just add the callee to the set of reachable functions, and if it’s the first time we’ve encountered the callee, we inspect its function body the same way we did for main. Dynamic calls through interface methods are trickier, because we don’t
know the set of types that implement the interface. We don’t want
to assume that every possible method in the program whose type matches
is a possible target for the call, because some of those types may
be instantiated only from dead code! That’s why we gather the set of
types converted to interfaces: the conversion makes each of these
types reachable from This leads to a chicken-and-egg situation. As we encounter each new reachable function, we discover more interface method calls and more conversions of concrete types to interface types. But as the cross product of these two sets (interface method calls × concrete types) grows ever larger, we discover new reachable functions. This class of problems, called “dynamic programming”, can be solved by (conceptually) making checkmarks in a large two-dimensional table, adding rows and columns as we go, until there are no more checks to add. The checkmarks in the final table tells us what is reachable; the blank cells are the dead code. The main function causes Helloer to be
instantiated, and the g.Greet calldispatches to the Greet method of each type instantiated so far.
Dynamic calls to (non-method) functions are treated similar to
interfaces of a single method.
And calls made using reflection
are considered to reach any method of any type used in an interface
conversion, or any type derivable from one using the TestsRTA is a whole-program analysis. That means it always starts from a
main function and works forward: you can’t start from a library
package such as However, most library packages have tests, and tests have main
functions. We don’t see them because they are generated behind the
scenes of If this reports that a function in a library package is dead, that’s
a sign that your test coverage could be improved.
For example, this command lists all the functions in
(The SoundnessAll static analysis tools necessarily produce imperfect approximations of the possible dynamic behaviors of the target program. A tool’s assumptions and inferences may be “sound”, meaning conservative but perhaps overly cautious, or “unsound”, meaning optimistic but not always correct. The deadcode tool is no exception: it must approximate the set of targets of dynamic calls through function and interface values or using reflection. In this respect, the tool is sound. In other words, if it reports a function as dead code, it means the function cannot be called even through these dynamic mechanisms. However the tool may fail to report some functions that in fact can never be executed. The deadcode tool must also approximate the set of calls made from
functions not written in Go, which it cannot see.
In this respect, the tool is not sound.
Its analysis is not aware of functions called exclusively from
assembly code, or of the aliasing of functions that arises from
the Try it outWe run With the dead code laid to rest, you can focus on eliminating code whose time has come to an end but that stubbornly remains alive, continuing to drain your life force. We call such undead functions “vampire code”! Please try it out:
We’ve found it useful, and we hope you do too.
Next article: Share your feedback about developing with Go |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |