News for hackers. Stuff that matters.
Re:PHP: why GOTO instead of recursion? by canadaj on Tuesday September 23, 2014 @12:00PM (#8355987)
Sure looks like it to me. Compiler can't figure out how to replace 'true' with '\true'? Better do it myself.
Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @12:07PM (#8356025)
In the context there was no compiler, there was an interpreter. PHP compilers (which do exist) might remove this type of redundancy, the PHP interpreter likely won't for reasons stated elsewhere in the comments.
Re:PHP: why GOTO instead of recursion? by kyberias on Tuesday September 23, 2014 @12:13PM (#8356060)
But doesn't PHP compile the source code into byte code and then interpret that? The problem described here was in the _compiler_ part of PHP, not in the byte code interpreter.
Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @12:37PM (#8356235)
All of which needs to occur after the user makes a request but before the request is returned.

Regardless of which word you wish to use (compiler, interpreter, lexer, etc) or which part of the process you wish to shift the workload into, that work (finding redundancies and removing them) is still being performed in real time after a request arrives (i.e. a user has to wait while we do this).

If the redundancy check costs more than the redundancy itself then removing them is utterly fruitless. The only way to avoid that is to take it out of that pipeline (i.e. pre-optimise the PHP OPCodes before a request arrives).

PHP pre-compilers and other optimisers absolutely exist and are readily used (e.g. HipHop). Within which case it might not matter if you used GOTO or While(true) as they'd both likely get optimised down to the GOTO OPCodes. However if you're using the original PHP interpreter then leaving them in might be the most optimal/cheap path to follow.

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @12:48PM (#8356311)
> The only way to avoid that is to take it out of that pipeline (i.e. pre-optimise the PHP OPCodes before a request arrives).

Most production installations of PHP pretty much mandate a bytecode cache and the official distribution bundles one since 5.5[0], so the redundancy check is done once but the optimisation works for any request thereafter.

> PHP pre-compilers and other optimisers absolutely exist and are readily used (e.g. HipHop).

HHVM is a completely separate VM which does not use (or consume, or care for in any way) the Zend bytecode produced by the Zend VM. HHVM has its own separate and incompatible bytecode format called HHBC[1]. It's neither a "pre-compiler" nor an "optimiser" for the Zend VM.

[0] http://php.net/manual/en/opcache.installation.php

[1] https://github.com/facebook/hhvm/blob/master/hphp/doc/byteco...

Re:PHP: why GOTO instead of recursion? by chriswarbo on Tuesday September 23, 2014 @02:10PM (#8356996)
> HHVM is a completely separate VM... It's neither a "pre-compiler" nor an "optimiser" for the Zend VM.

"HHVM" is a VM. "HipHop [for PHP]" was a compiler[0], although it compiled down to C++ rather than Zend's VM (much like ShedSkin for Python[1])

[0] http://en.wikipedia.org/wiki/HipHop_for_PHP [1] http://en.wikipedia.org/wiki/Shed_Skin

Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @01:03PM (#8356423)
> It's neither a "pre-compiler" nor an "optimiser" for the Zend VM.

Nobody claimed otherwise.

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @01:56PM (#8356857)
> Nobody claimed otherwise.

You provided HHVM as an example[0] of "PHP pre-compilers and other optimisers".

[0] that's literally what 'e.g.' means, it's an abbreviation for "exempli gratia", latin for "for example"

Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @02:23PM (#8357109)
Indeed I did, and indeed it is.

You tacked on "for the Zend VM." Which was not something I said.

I said:

> PHP pre-compilers and other optimisers absolutely exist and are readily used (e.g. HipHop).

You then said:

> HHVM is a completely separate VM which does not use (or consume, or care for in any way) the Zend bytecode produced by the Zend VM. HHVM has its own separate and incompatible bytecode format called HHBC[1]. It's neither a "pre-compiler" nor an "optimiser" for the Zend VM.

Which is really answering a point nobody made. It is just completely out of the blue. My post literally does not contain the term "Zend." Nor does it make any claim to what OPCode formats different things use (and really the only reason you could even talk about HipHop was that it was given as a unqualified example).

So to this:

> It's neither a "pre-compiler" nor an "optimiser" for the Zend VM.

Is answering a point nobody made. It is like me saying, "You're wrong, the White House isn't blue, it is white!" When no claims to the blueness of the White House were made by yourself.

Re:PHP: why GOTO instead of recursion? by jiggy2011 on Tuesday September 23, 2014 @12:36PM (#8356227)
Why is this downvoted? it is a reasonable question.
Re:PHP: why GOTO instead of recursion? by mackal on Tuesday September 23, 2014 @12:54PM (#8356349)
Because its more aruging about semantics than anything else. At the end of the day, it needs to do this in real time and optimizing would cause a loss of speed for sometimes faster executed code, but since it all counts every time, it would be a waste.
Re:PHP: why GOTO instead of recursion? by jiggy2011 on Tuesday September 23, 2014 @01:27PM (#8356591)
Opcodes can be cached until the source file changes. There may be specific instances when caching cannot happen, but the poster seems to not be clear as to why that would apply in this case, hence the question.
Re:PHP: why GOTO instead of recursion? by DonHopkins on Tuesday September 23, 2014 @02:01PM (#8356918)
PHP compilers are useful for optimizing SQL injection attacks.
Re:PHP: why GOTO instead of recursion? by collyw on Tuesday September 23, 2014 @02:48PM (#8357310)
Anywhere I can find out more about this?
Re:PHP: why GOTO instead of recursion? by alayne on Tuesday September 23, 2014 @12:37PM (#8356230)
Igorw's post uses the word compiler four times.
Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @12:48PM (#8356307)
PHP compiles but acts as an interpreter. The Wikipedia article covers this[0]:

> The Zend Engine compiles PHP source code on-the-fly into an internal format that it can execute, thus it works as an interpreter.

I won't get drawn into a highly dull debate on the definition of what an interpreter is or isn't. Instead let's just agree that PHP by default generates the OPCodes every single time a request comes in. That's all we need to understand for this context (that OPCode generation is in-line with requests, not pre-compiled or otherwise cached).

Also, yes, there are tons and tons of implementations of PHP caching/precompilation/JIT/etc.

[0] https://en.wikipedia.org/wiki/PHP#Implementations

Re:PHP: why GOTO instead of recursion? by kyberias on Tuesday September 23, 2014 @02:11PM (#8357006)
The original author wanted to generate byte code that executes as efficiently as possible. Clearly scoped goal. :)

Because PHP's compiler component, which he refers to as "compiler", did not generate efficient byte code for the interpreter, he had to use GOTO to force the compiler to generate it.

Whether the byte codes get cached or not might be irrelevant to his goals. We cannot be certain what proportion of time is wasted in network, compilation or serving the request. It could be one way or another.

USUALLY this is a waste of time. The developer's time that is.

Re:PHP: why GOTO instead of recursion? by Implicated on Tuesday September 23, 2014 @12:03PM (#8356004)
I stand corrected. Compiler.
Re:PHP: why GOTO instead of recursion? by kyberias on Tuesday September 23, 2014 @12:09PM (#8356036)
Compiler.
Re:PHP: why GOTO instead of recursion? by alayne on Tuesday September 23, 2014 @12:32PM (#8356209)
You should not be getting downvoted. The article is clearly discussing the compiler. Compilers and interpreters are not mutually exclusive. Just because a compiler produces bytecode (virtual instructions) instead of machine instructions (like x86), doesn't somehow make it not a compiler.
Re:PHP: why GOTO instead of recursion? by astrodust on Tuesday September 23, 2014 @12:51PM (#8356335)
Exactly. A compiler transforms abstract source code into machine-readable instructions.

These machine-readable instructions can be executable directly, but not necessarily. Is a cross-compiler not a true compiler because you can't execute the resulting code directly? Not at all. So by the same token, one that produces byte code is no different.

Re:PHP: why GOTO instead of recursion? by kyberias on Tuesday September 23, 2014 @02:16PM (#8357050)
I'll argue with myself here a bit if you allow... When considering PHP, we can think of The PHP Interpreter as a hybrid of two components: 1) a translator/compiler and 2) a bytecode interpreter. Therefore, when someone says "PHP Interpreter" he may indeed refer to that hybrid. If this is true, it would cause these kind of confusions. ;)
Re:PHP: why GOTO instead of recursion? by astrodust on Thursday September 25, 2014 @04:57PM (#8369703)
Yeah. I'd suggest that most "interpreters" today consist of a compiler and a virtual machine that runs those instructions. Historically, the earliest interpreted languages had a parser which immediately executed whatever it discovered, like BASIC, where each line was an independent statement that was parsed and executed.

It didn't take long for this to prove itself to be terribly inefficient.