Messages in this thread | | | Date | Wed, 20 Jan 1999 22:36:27 -0500 (EST) | From | Alexander Viro <> | Subject | Re: Structure vs purism ? |
| |
On Wed, 20 Jan 1999, Dave Jones. wrote:
> Gcc will generate the same 'short jmp' x86 instruction for a goto, or a > break; The only time code is generated differently is when a function is > created out of code which is goto'd from several places. This type of code > is quite spaghetti like, and a more structured approach can only generate > better code than this tangled mess of short jmp's. Even if extra functions > are created, if short enough gcc can be told to inline them, resulting in > almost-identical result to what we currently have, but with more readable > source code.
The difference being that not all gotos are equal. If you have something like if (improbable_condition) foo(); else bar(); baz();
yo can translate it different ways. The best one being: <test the condition> jump to l1 if true; code for foo(); l2: code for baz(); return; l1: code for bar(); jump to l2;
That way the expectation value of execution time becomes minimal. If the code for foo() and baz() is cheap and codition is really improbable we get a serious speedup. Compiler can't tell the relative probability of code paths. The standard way to achieve this effect being:
if (improbable_condition) goto off_the_main_path_we_go; foo(); cleanup: baz(); return; off_the_main_path_we_go: bar(); goto cleanup;
> Then 2.2.0pre8 arrived, which had goto's in some places where there were > not before, whilst the average goto count per file remained roughly the > same.
Which reminds me of average temperature of patients per floor in hospital...
[snip] > The idea of goto's being a 'bad thing' is possibly the first thing that > gets taught on pretty much every programming course I've ever been on. > Some might argue, that this ideology is just for purists, but compilers > have become a lot more advanced. The use of constructs such as goto are > outdated crutches used by people too lazy to write a more structured > solution.
It's not a question of structured vs. spaghettish. It's a question of implementing two constructs missing in C: asymmetric if and (gaack!) very limited sort of exceptions. Compilers might get advanced and all such, but in C we have no way to inform compiler on relative probability of branches (and it can't figure it out for itself - AI-complete problem). Notice that ten levels of nested ifs are not more readable than a jungle of gotos. There is a logic behind the rejection of gotos. In many situations it leads to winnitude. But if you'll look at this one you'll get the opposite result.
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/
| |