News for hackers. Stuff that matters.
Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @12:52PM (#8356341)
> That isn't how PHP natively works.

Yes it is.

> It has to be done per every single request

Yes? And the loop will most likely loop multiple times per request, which is my point.

> If you use something like HipHop or PHP accelerator then you can get cached OPCodes

HHVM is a completely separate and unrelated product. And you don't need "PHP accelerator" because the Zend VM bundles a bytecode cache since 5.5[0], not that it has any relevance to my point.

> But you cannot assume PHP will be executed in those environments.

Good thing I wasn't assuming anything then.

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

Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @01:15PM (#8356501)
Wait, let's backtrack here.

I said that natively PHP doesn't cache and the OPCodes are generated every request. You said "Yes it is." meaning presumably you don't feel like that is the case.

You then go on to link to Zend Opcache which is, by all accounts, an extension which provides caching and which wasn't even bundled with PHP until 5.5 (according to yourself). So from 0.1 through 5.4 PHP shipped with zero caching and thus a core part of the language at that time was that it was generated every request.

Now, 5.5 was released in June 2013.

So for a lot of users PHP remains a language which is interpreted when a user makes a request. And even users on 5.5+, the first request per page is effectively interpreted as the cache isn't pre-populated.

So really what is it that we are even discussing? Seems like a debate about semantics at this stage.

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @01:53PM (#8356829)
> I said that natively PHP doesn't cache and the OPCodes are generated every request. You said "Yes it is." meaning presumably you don't feel like that is the case.

Since when do you backtrack to a completely different subtread (or to a subthread which does not exist)?

* I said that the detection (optimisation) would be performed while generating the bytecode

* You asserted that it's not how PHP works by default

* I replied that it is

You've asserted that Zend PHP does not use an intermediate bytecode during normal operation, do you stand by it? Because that's where we end up if we backtrack, not about the fact that it doesn't cache bytecode by default which is not actually relevant to my point (the existence and widespread use of bytecode cache merely supports said point by further improving the ROI).

Re:PHP: why GOTO instead of recursion? by Someone1234 on Tuesday September 23, 2014 @02:29PM (#8357153)
> You've asserted that Zend PHP does not use an intermediate bytecode during normal operation, do you stand by it?

I never made that claim, and no I don't stand by a claim I never made.

You've asserted that PHP is actually JavaScript with style, do you stand by it?

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @02:40PM (#8357243)
> I never made that claim

Sorry to report that yes you did, let me quote exactly (rather than paraphrase):

>> The detection is only done one while generating the bytecode

> That isn't how PHP natively works.

Since "the detection" is obviously a hypothetical assumed in case the compiler ever got a peephole optimizer, the only part of the phrase you could object to is that Zend PHP generates intermediate bytecode.

Re:PHP: why GOTO instead of recursion? by ajanuary on Tuesday September 23, 2014 @01:28PM (#8356614)
Bear in mind any new optimizations would be added to 5.5+, which would presumably have opcode caching by default?

[Edit] Maybe a whole new world of possible optimizations has just opened up.

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @01:53PM (#8356838)
5.5 bundles an opcode cache in the default distribution, but the cache isn't enabled by default AFAIK.
Re:PHP: why GOTO instead of recursion? by ajanuary on Tuesday September 23, 2014 @01:16PM (#8356507)
To be clear, what optimization are you proposing? Replacing "JMPNZ true" with "JMP"?

So every time a loop is compiled (which by default is every request) there is the added overhead of "if opcode is JMPNZ and the first operand is the literal true, replace opcode with JMP and remove first operand".

This saves the "if operand is non-zero" check for all iterations of loops where the first operand is a constant "true".

Which means you've increased the cost of every loop on every request by a constant amount to decrease the cost of each iteration of "while (\true)".

Obviously you'd have to measure the actual costs, but it doesn't seem worth it to me.

Re:PHP: why GOTO instead of recursion? by masklinn on Tuesday September 23, 2014 @02:05PM (#8356953)
> To be clear, what optimization are you proposing? Replacing "JMPNZ true" with "JMP"?

Yup. And remove FETCH_CONSTANT as well.

> So every time a loop is compiled (which by default is every request) there is the added overhead of "if opcode is JMPNZ and the first operand is the literal true, replace opcode with JMP and remove first operand".

Sure, or perform it while compiling the bytecode itself.

> This saves the "if operand is non-zero" check for all iterations of loops where the first operand is a constant "true".

It also saves loading the operand, because I expect most PHP code trying to loop forever doesn't use `\true`, and it can be applied to `for(;;)` to boot (a pretty common construct for those coming from C).

> Obviously you'd have to measure the actual costs

Obviously.