Generating more efficient code for while-loop

From: Date: Fri, 16 Jan 2015 07:56:59 +0000
Subject: Generating more efficient code for while-loop
Groups: php.internals 
Request: Send a blank email to internals+get-80621@lists.php.net to get a copy of this message
Hello, Please consider the following PHP code: while ( <CONDITION> ) { <STATEMENT> } It currently compiles to something like this for the Zend Engine: 0: <CONDITION> 1: JMPZ -> 4 2: <STATEMENT> 3: JMP -> 0 4: ... This can easily be rewritten in an equivalent but much more efficient form: 0: JMP -> 2 1: <STATEMENT> 2: <CONDITION> 3: JMPNZ -> 1 The trick is to jump to the conditional expression before the first iteration and then only having to continue with just on conditional jump on every subsequent cycle. This would result in only 1 jump per loop iteration compared to effectively 2 jumps needed per iteration with the current implementation. It would also make the loop tighter as upon entering the loop (after first conditional evaluation), one less opcode remains relevant to the loop. An analogous approach can be taken for the for-loop. Thanks, Ben PS: This is my first post on a PHP mailinglist - I am very excited! :) -- Benjamin Coutu Zeyon Technologies Inc. http://www..zeyos.com

« previous php.internals (#80621) next »