4 ms·
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
by realusername 12y ago
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 12y agoFrom 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 12y agoI've never tried, but this (http://us2.php.net/manual/en/function.runkit-constant-redefine.php http://us2.php.net/manual/en/function.runkit-constant-redefi...) seems relevant.
- richardwhiuk 12y agoSo 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).
- masklinn 12y ago> 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)
- Someone1234 12y agoThe key word there is interpreter. When you compile you have near infinite time to try and optimise and remove redundancy. When you interpret (i.e. real-time compilation) you have to decide if the redundancy check is more expensive than removing the redundancy. 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.
- masklinn 12y ago> 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. 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).
- Someone1234 12y ago> The detection is only done one while generating the bytecode 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.
- masklinn 12y ago> 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 http://php.net/manual/en/book.opcache.php