switch labels in sub-blocks

php.internals

Ard Biesheuvel

22 years ago
Hi all, is there a specific reason why nested blocks in switch statements are not supported ? It can be very useful if you want to jump into the middle of the first iteration of a loop (like fetching rows from a result set where the first row might or might not be already present) Eg. switch ($i) { case 0: do { // iteration default: } while ( /* some condition */ ); }
-- Ard

Ilya Sher

22 years ago
Ard Biesheuvel wrote:
> Hi all, > > is there a specific reason why nested blocks in switch statements are > not supported ? It can be very useful if you want to jump into the > middle of the first iteration of a loop (like fetching rows from a > result set where the first row might or might not be already present) > > Eg. > > switch ($i) { > > case 0: > do { > // iteration > default: > } while ( /* some condition */ ); > } >
It looks like using "goto" to me. Messy. That's probably the reason it is not allowed. Or maybe other people like myself failed to understand how it is really useful. Real example from you would help here. Anyway, are you really sure you want to do that? #### (what it would be with "goto") #### $i=7; $t=0; switch($i) { case 0: do { echo "($t)"; label1: echo "[$t]"; } while(++$t<3); default: goto label1; } #### Just my 2 agoras...

Hartmut Holzgraefe

22 years ago
Ilya Sher wrote:
> It looks like using "goto" to me. Messy. > That's probably the reason it is not allowed. > > Or maybe other people like myself failed to > understand how it is really useful. Real example > from you would help here.
it's a valid performance trick in C http://www.jargon.net/jargonfile/d/Duffsdevice.html
-- Hartmut Holzgraefe <hartmut@php.net>

Ilya Sher

22 years ago
Hartmut Holzgraefe wrote:
> Ilya Sher wrote: > >> It looks like using "goto" to me. Messy. >> That's probably the reason it is not allowed. >> >> Or maybe other people like myself failed to >> understand how it is really useful. Real example >> from you would help here. > > > it's a valid performance trick in C
Thanks for the info.
> > http://www.jargon.net/jargonfile/d/Duffsdevice.html >
I still fail to understand how that is useful. Please, explain me. _Real_ example is welcome. The example there just puts into *to one item referenced by *from with offset count. (Correct me if I'm wrong) Assuming *to will be replaced with *to++ it will be plain memcpy() without any "interesting" functionality. (Again, correct me if I'm wrong)

Michael Walter

22 years ago
Ilya Sher wrote:
> Hartmut Holzgraefe wrote: > >> Ilya Sher wrote: >> >>> It looks like using "goto" to me. Messy. >>> That's probably the reason it is not allowed. >>> >>> Or maybe other people like myself failed to >>> understand how it is really useful. Real example >>> from you would help here. >> >> >> >> it's a valid performance trick in C > > Thanks for the info. > >> >> http://www.jargon.net/jargonfile/d/Duffsdevice.html >> > I still fail to understand how that is useful. > Please, explain me. _Real_ example is welcome.
It's an unrolled loop. See http://www.lysator.liu.se/c/duffs-device.html for more information for more elaboration from the author (google works for you, too, btw). Cheers, Michael

Ilya Sher

22 years ago
Michael Walter wrote: [snip]
> It's an unrolled loop. > > See http://www.lysator.liu.se/c/duffs-device.html for more information > for more elaboration from the author (google works for you, too, btw).
Thanks. Got it now. [Somehow did not think of google. My fault. Was kind of confused. The first link given should be yours and not to jargon. It's much better IMHO.] Well, now that I think I understand what is it for, I'm pretty sure it's not very appropriate for PHP(, unless someone will provides an example of the opposite of course). It's kind of clarity vs efficiency. The more the basic operation ("*to = *from++" in the original) is getting bigger (slower) the efficiency gain becomes smaller (correct?) so I would prefer clarity here. I'm not even sure real example in PHP will have close to 50% speed gain.

Sara Golemon

22 years ago
> Well, now that I think I understand what is it for, > I'm pretty sure it's not very appropriate for PHP(, > unless someone will provides an example of the opposite > of course). > > It's kind of clarity vs efficiency. > The more the basic operation ("*to = *from++" in the > original) is getting bigger (slower) the efficiency gain becomes > smaller (correct?) so I would prefer clarity here. > > I'm not even sure real example in PHP will have close to 50% > speed gain. >
In the case of "Duff's Device" it certainly isn't applicable to PHP....It much for efficient for the engine to process a complex single opcode than to process many simple opcodes in succession. That still doesn't speak to whether or not there aren't truly applicable examples. Functionally the parser doesn't want to see overlapping structures like this and it feel non-trivial to implement. But I havn't looked at the switch implementation close enough to be certain yet. For the sake of readability I'm -1. I'd sooner see goto implemented than this. And I don't care for that either. -Sara

Ilya Sher

22 years ago
Sara Golemon wrote:
>>Well, now that I think I understand what is it for, >>I'm pretty sure it's not very appropriate for PHP(, >>unless someone will provides an example of the opposite >>of course). >> >>It's kind of clarity vs efficiency. >>The more the basic operation ("*to = *from++" in the >>original) is getting bigger (slower) the efficiency gain becomes >>smaller (correct?) so I would prefer clarity here. >> >>I'm not even sure real example in PHP will have close to 50% >>speed gain. >> > > In the case of "Duff's Device" it certainly isn't applicable to PHP
That was my guess, you approved it.
>....It > much for efficient for the engine to process a complex single opcode than to > process many simple opcodes in succession. That still doesn't speak to > whether or not there aren't truly applicable examples.
It looks like it worked just fine for that example in _C_.
> > Functionally the parser doesn't want to see overlapping structures like this > and it feel non-trivial to implement. But I havn't looked at the switch > implementation close enough to be certain yet. > > For the sake of readability I'm -1.
Agreed on that. That's what I was originally talking about. It was phrased as "messy".