Take this example, it is one extra OP Code instruction, which might generate a handful of ASM that actually gets run. How many ASM instructions would it take to detect this (and make sure there are no externalities) and remove it? I'd imagine considerably more.
And worse still you're also performing all of these redundancy checks even when there is nothing to remove. So now you've increased the cost to ALL loops, not just static ones with no purpose.
TL;DR: It is more expensive to detect and remove than to ignore.
The detection is only done one while generating the bytecode, but the extraneous bytecode instructions have to be executed every time the loop… loops.
Not only that but it's a very simple optimisation to apply (even CPython is able to discover it).
That isn't how PHP natively works. It is an interpreted language with compiler addons. It has to be done per every single request (as the CGI process may collapses after each one).
If you use something like HipHop or PHP accelerator then you can get cached OPCodes, and can also perform optimisation at that stage. But you cannot assume PHP will be executed in those environments.
PHP is not Java.
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.
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.
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).
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?
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.
[Edit] Maybe a whole new world of possible optimizations has just opened up.
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.
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.
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`.
Yes, but that only requires a guard clause. That's what CPython 2 does in the same situation:
>>> import dis
>>> def foo():
... while True:
... print "ok"
...
>>> dis.dis(foo)
2 0 SETUP_LOOP 15 (to 18)
>> 3 LOAD_GLOBAL 0 (True)
6 POP_JUMP_IF_FALSE 17
3 9 LOAD_CONST 1 ('ok')
12 PRINT_ITEM
13 PRINT_NEWLINE
14 JUMP_ABSOLUTE 3
>> 17 POP_BLOCK
>> 18 LOAD_CONST 0 (None)
21 RETURN_VALUE
(in Python 3, True and False are keywords so the loop prelude can be elided entirely)runkit_constant_redefine("true", false) == false in success and failure (as success results in true, which equals false now).
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.