
PHP: why GOTO instead of recursion? - napolux
https://github.com/igorw/retry/issues/3
======
Guvante
So the short version is "PHP doesn't perform optimizations so I had to
manually include them".

~~~
kstrauser
That whole thread made me cringe. Yes, it's a clever microoptimization, but he
never mentions whether a profiler showed it to actually be beneficial. That
might save 5 cycles out of a billion, for instance, at the cost of making the
code "weird".

Don't try to out-clever the compiler. You might be able to do it, but there's
no guarantee that tomorrow's compiler update won't be smarter than your
today's workaround, leaving you with code that is both "weird" and slow.

~~~
Crito
> _" there's no guarantee that tomorrow's compiler update won't be smarter
> than your today's workaround, leaving you with code that is both "weird" and
> slow."_

Yeah, there is no _guarantee_ , but let's be realistic here. That bug for
PHP's incorrect parsing of octal integer literals has been unfixed for what, a
_decade_ now?

~~~
kstrauser
Point taken.

------
realusername
I knew that the php interpreter was not very clever but the most surprising
part is this one :

There is actually a difference between "while (true)" and "while (\true)", and
it's not even capable to optimize this as a standard JMP. I can't believe that
really simple things like this are still not optimized in the interpreter.

~~~
psuter
From the reply: "This requires doing a namespace lookup against igorw\true".

It has been a long time since I last wrote PHP code, but is it possible to
dynamically redefine the value of `true`? That would (partially) explain why
you can't statically evaluate it to `\true`.

~~~
jbrooksuk
I've never tried, but this ([http://us2.php.net/manual/en/function.runkit-
constant-redefi...](http://us2.php.net/manual/en/function.runkit-constant-
redefine.php)) seems relevant.

~~~
richardwhiuk
So if you redefine true to false:

runkit_constant_redefine("true", false) == false in success and failure (as
success results in true, which equals false now).

------
martin_
To add context, igorw is a contributor on php-internals has committed multiple
patches/improvements[1] to php-src as recent as PHP 5.6

Great explanation!

[https://github.com/php/php-
src/commits?author=igorw](https://github.com/php/php-src/commits?author=igorw)

------
excitom
It seems to me that if you're this worried about performance you should be
looking at something like HipHop.

------
peterwwillis
Longest explanation of "function calls [and loops] are expensive" ever. Also
the best example of why over-optimization is a completely valid thing to do if
you're bored and have a debugger handy.

~~~
masklinn
The explanation is mostly "Zend's bytecode compiler is retarded and can't do
trivial optimisations so you have to handroll them".

------
seccess
In my experience, PHP can have really unexpected runtime performance. This
site has some interesting benchmarks highlighting this:
[http://www.phpbench.com/](http://www.phpbench.com/)

I'd like to hear of any other PHP performance gotchas people know of. One of
my favorites: passing function arguments by value is faster than passing by
reference.

------
powera
The real question is: if you care _that much_ about performance, why would you
use the un-optimized PHP runtime?

~~~
thathonkey
Sometimes people optimize for fun, out of sheer curiosity, or some combination
of things like that. Not everything we do as programmers has to be for some
solely practical purpose. Having some fun with opcode optimization is
certainly permissible and if you program a lot in PHP, learning how things
work under the hood will make you a better PHP programmer in general.

------
krazydad
I like including a few GOTOs just to piss off the Dijkstra acolytes, one of
whom is quite vocal in the thread.

~~~
jtc331
Since I see you're referencing my posts in that thread, you'll notice that I
also point out that I understand that GOTO is not always bad, and, in fact,
have used it before in production code.

------
Piskvorrr
Very...clever[1].

[http://www.catb.org/jargon/html/K/kluge.html](http://www.catb.org/jargon/html/K/kluge.html)

------
wyager
If you need to use GOTOs in your code for speed and you're not coding in C,
you should probably find a faster platform.

~~~
quotemstr
Not all useful control flow graphs can be described with while and if. It's
perfectly fine to use goto. Look at some of the contortions people need to go
through when avoiding it. Avoiding goto often makes programs harder to follow.

Why is it inexperienced programmers go through a phase where they think goto
is evil incarnate? I wish Dijkstra had never written that damn paper.

~~~
wyager
>Not all useful control flow graphs can be described with while and if.

They can be described with if, while, for, case, pattern matching, recursion,
etc. There is almost always a better alternative.

>Why is it inexperienced programmers go through a phase where they think goto
is evil incarnate?

It seems to me that most people who support general usage of GOTO are
inexperienced and don't necessarily realize it. They lack knowledge of better
control structures. See the blub paradox.

Besides, the justification here is performance, not readability. I'm saying
that performance in particular is a bad justification for using GOTO.

------
justincormack
Languages without tail recursion elimination are very annoying. Looking at you
JavaScript.

~~~
phkahler
>> Languages without tail recursion elimination are very annoying. Looking at
you JavaScript.

Programmers who use tail recursion instead of a loop are very annoying.
Looking at you justincormack ;-)

Seriously, when I first saw recursion explained using the fibonacci sequence I
was really scratching my head. I wondered why anyone would do that. Now that
I'm older and wiser, I can see that it's natural to a mathematician. Why
anyone who claims to be a programmer or compsci person would do it is beyond
me. I suppose it's a simple example, but I'm a fan of introducing concepts
with examples that really _want_ the technique in question as a solution.

~~~
emodendroket
How about a depth-first search, then?

~~~
crimsonalucard
That's not tail recursion! It's tree recursion.

~~~
emodendroket
Well, good point, but so is the naive implementation of Fibonacci, isn't it?

------
Hawkee
Great read, but I think it's a few years too late. When you compare how PHP is
trending against languages like Python it's following a similar path as perl,
[https://www.google.com/trends/explore#q=php%2C%20javascript%...](https://www.google.com/trends/explore#q=php%2C%20javascript%2C%20python%2C%20perl&cmpt=q)
\- I'm surprised JavaScript isn't having much of an upturn lately.

~~~
goshx
On a side note, Monty Python (or the snake) are not programming languages.
Check the "Related searches" section at the bottom and click "Python".

~~~
Hawkee
Silly me. I forget that not everybody lives in the world of development.
Considering this it seems every language is experiencing a downturn. To me
this looks more like a fault in Google's trend calculation. Maybe it doesn't
consider search inflation from year to year.

