4 ms·
Goto doesn't have a performance benefit over other control flow structures with a modern compiler.
by centimeter 6y ago
Goto doesn't have a performance benefit over other control flow structures with a modern compiler.
- akira2501 6y agoYes, but it does reduce the level of indentation required in most functions that use it. That consideration is part of the kernel style guidelines.
- the_duke 6y agoIn C GOTO is often by far the cleanest solution for error handling. Everything else turns into an ugly, unreadable mess in functions that need to free resources or do some kind of cleanup.
- Uptrenda 6y agoI've seen the goto error-handling approach used now in SDKs written by developers from companies like Microsoft, Nvidia, and Nordic Semiconductor. It really does seem to be the best way to do it. And it's simple. Mess something up... goto cleanup... Seems fine to me!
- centimeter 6y agoMy comment has nothing to do with error handling. I was addressing a false claim about performance.
- asddubs 6y agothat's definitely not categorically true
- mhh__ 6y agoCan you think of a counterexample? I wouldn't be surprised if you could trick a compiler with a goto but optimizing control flow is very much the type of optimization that Knuth was referring to. Now that almost all compilers use SSA I'd be surprised if it was the case outside of fairly deep control flow
- Out_of_Characte 6y agoError handling was already mentioned ITT. Goto use seems like a litmus test for bad programming. The presence of it doesn't prove bad programmings but lots of goto statements could be reasonably be expected to be replaced with safer switch statements. Extremely nested functions are another area that is best served with a goto. Breaking out of many functions has performance implications I believe.
- viraptor 6y agoThe question was about performance, not necessarily other aspects.
- eloff 6y agoIn system C code its usually used for error and retry logic to make the code more readable. It's rarely ever used for performance.