On Wednesday, 25 November 2020 at 19:04:45 UTC, H. S. Teoh wrote:
FWIW, D's switch statement is flexible enough to directly write
Duff's device.
How good is optimization in ldc2, gdc, dmd at compiling
chained jumps into one jump each time?
I'm pretty sure ldc2 and gdc will optimize away any such
chained jumps. But if performance is important to you, I
recommend *not* bothering with dmd.
Yes this is about efficiency.
Is there a good way to simulate computed goto in D?
With a switch statement. ;)
OK, so I took a look at switch documentation and tried out
something. It looked like switch can have computed goto using
case labels.
case 5:
//...
goto case <expression>; //doesn't compile when <expression> is
not a constant
So to simulate computed goto have to
1. wrap switch(x) in a loop [ while(0) ]
2. inside each case recompute x (instead of jump to computed y)
3. jump back to execute switch again [ continue ]
It does look as if a nested switch can contain case labels from
an outer switch which is very good. Did not try this out.
Any more ideas, advice?