What are the performance benefits or penalties of using goto
with a modern C++ compiler?
I am writing a C++ code generator and use of
The part of a compiler that would be affected works with a flow graph. The syntax you use to create a particular flow graph will normally be irrelevant -- if you create something like a while
loop using a goto
instead of an actual while
statement, it's not going to affect the optimizer at all (by that point, the syntax that produced the flow graph will be long gone).
It is possible, however, to produce a flow graph with goto
s that couldn't be produced by any normal flow control statements (loops, switch, etc.) In such a case, you may produce an irreducible flow graph, and when/if you do, that will often limit the ability of the compiler to optimize the code.
In other words, if (for example) you took code that was written with normal for
, while
, switch
, etc., and converted it to use goto
in every case, but retained the same structure, almost any reasonably modern compiler would probably produce essentially identical code either way. If, however, you use goto
s to produce the mess of spaghetti like much of the FORTRAN I had to look at decades ago, then the compiler probably won't be able to do much with it.