Computed gotos on Reddit

bearophile bearophileHUGS at lycos.com
Mon Jul 23 15:23:50 PDT 2012


Walter Bright:

> dmd currently doesn't do that, but that's not the language's 
> fault, it's a quality of implementation issue.

I understand the difference between what compilers are able to do 
(today or in future), and what the language specs allow compiler 
writers to do. So in theory I agree.

In practice there is also the well known fallacy of the 
"sufficiently smart compiler":
http://c2.com/cgi/wiki?SufficientlySmartCompiler

This fallacy implies that if you want to actually see a compiler 
able to perform a certain optimization, such optimization must be 
rather "easy", this means it must be easy for the compiler to 
infer as true all the conditions necessary to apply that 
optimization (and then you need someone to actually implement it, 
in a community as small as the D one optimizations can't be top 
priority).

The other problem with optimizations is that often if you can't 
rely on them, that means you can't be certain they are used in 
the code you are writing, then it's like they don't exist. A good 
example of this is the Scheme standard requiring all Scheme 
compilers to implement the tail call optimization.


> Since the final switch does not allow a 'default' case, the 
> check can be omitted, and the generated code is a simple 
> index-jump, just like the computed goto example.

You have seen the asm I have shown in the next post. You see 
those "jmp *%ecx" at the end of each case. Computed gotos in this 
case are not just a single index-jump, there is an index-jump at 
the end of each case. Is your future hypothetical D compiler able 
to do that?

Bye,
bearophile


More information about the Digitalmars-d mailing list