# switch labels in sub-blocks

- Thread: https://externals.io/message/10162
- Original archive: https://news-web.php.net/php.internals/10162
- Started by Ard Biesheuvel on 2004-05-28T16:09:10+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10162 — Ard Biesheuvel — 2004-05-28T16:09:10+00:00

URL: https://externals.io/message/10162#10162

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

---

## #10165 — Ilya Sher — 2004-05-28T21:05:08+00:00

URL: https://externals.io/message/10162#10165 · In reply to: #10162

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...

---

## #10166 — Hartmut Holzgraefe — 2004-05-28T21:53:47+00:00

URL: https://externals.io/message/10162#10166 · In reply to: #10165

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

---

## #10167 — Ilya Sher — 2004-05-29T06:31:32+00:00

URL: https://externals.io/message/10162#10167 · In reply to: #10166

Hartmut Holzgraefe wrote:
> Ilya Sher wrote:
> 
> > […]
> 
> 
> 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)

---

## #10168 — Michael Walter — 2004-05-29T08:03:38+00:00

URL: https://externals.io/message/10162#10168 · In reply to: #10167

Ilya Sher wrote:
> Hartmut Holzgraefe wrote:
> 
> > […]
> 
> Thanks for the info.
> 
> > […]
> 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

---

## #10169 — Ilya Sher — 2004-05-29T09:47:07+00:00

URL: https://externals.io/message/10162#10169 · In reply to: #10168

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.


> 
> Cheers,
> Michael

---

## #10172 — Sara Golemon — 2004-05-29T14:15:47+00:00

URL: https://externals.io/message/10162#10172 · In reply to: #10169

> 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

---

## #10173 — Ilya Sher — 2004-05-29T15:16:35+00:00

URL: https://externals.io/message/10162#10173 · In reply to: #10172

Sara Golemon wrote:
> > […]
> 
> 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".
> I'd sooner see goto implemented than
> this.  And I don't care for that either.
> 
> -Sara
