Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80658 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42105 invoked from network); 16 Jan 2015 18:14:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2015 18:14:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=ben.coutu@zeyos.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ben.coutu@zeyos.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zeyos.com designates 109.70.220.166 as permitted sender) X-PHP-List-Original-Sender: ben.coutu@zeyos.com X-Host-Fingerprint: 109.70.220.166 unknown Received: from [109.70.220.166] ([109.70.220.166:40420] helo=mx.zeyon.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/93-23044-00559B45 for ; Fri, 16 Jan 2015 13:14:25 -0500 Received: from localhost (mx.zeyon.net [127.0.0.1]) by mx.zeyon.net (Postfix) with ESMTP id 3870A5F8DD for ; Fri, 16 Jan 2015 19:14:21 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mx.zeyon.net Received: from mx.zeyon.net ([127.0.0.1]) by localhost (mx.zeyon.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MryoL4V+X+Rp for ; Fri, 16 Jan 2015 19:14:20 +0100 (CET) Received: from cloud.zeyos.com (unknown [109.70.220.163]) by mx.zeyon.net (Postfix) with ESMTPA id 745A35F89F; Fri, 16 Jan 2015 19:14:18 +0100 (CET) Date: Fri, 16 Jan 2015 19:14:18 +0100 To: Dmitry Stogov Cc: PHP Internals , Xinchen Hui MIME-Version: 1.0 X-Mailer: ZeyOS Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Message-ID: <20150116181421.3870A5F8DD@mx.zeyon.net> Subject: Re: [PHP-DEV] Generating more efficient code for while-loop From: ben.coutu@zeyos.com (Benjamin Coutu) Hi Dimitry,=0A=0AThank you for the acknowledgment. I am a big fan of your w= ork.=0A=0AI think adding the suggested changes to foreach will be a good im= provement. With foreach one cold actually merge the jump-into-opcode (line = 0 in the while-loop example) with FE_RESET.=0A=0AOn another note, I think t= here are many simple compiler optimizations (without the need for real code= analysis) that should now be possible, especially in light of AST. The fol= lowing samples of common patterns come to mind: if/while (! ...) BOOL_NOT+JMPZ -> JMPNZ if/while ( ... &&/|| ... ) BOOL+JMPZ -> JMPZ (BOOL is obsolete) =0Acase (...)=0ACASE+JMPZ -> CASE (add JMPZ functionality to CASE operation= )=0A I understand that these aren't huge improvements per se, but they are very = common and could at least help reduce the generated bytecode size (thinking= OpCache).=0A=0ACheers,=0A=0ABen=0A=0A=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Origin= al =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0AFrom: Dmitry Stogov = =0ATo: Benjamin Coutu =0ADate: Fri, 16 Jan 2015 17:12:= 34 +0100=0ASubject: Re: [PHP-DEV] Generating more efficient code for while-= loop=0A=0AHi Benjamin,=0A=0AThanks for idea.=0AWith PHP7 AST compiler, it's= quite easy to implement this (it took us 15=0Aminutes to try :)=0AHowever,= it doesn't make big improvement on our benchmarks.=0AWe will take a look i= nto possibilities to apply your idea to other patterns=0A(e.g. for and fore= ach loops).=0A=0AAnyway, it makes full sense to include this optimization i= nto PHP7.=0AI just like to try other opportunities first.=0A=0AThanks. Dmit= ry.=0A=0AOn Fri, Jan 16, 2015 at 10:56 AM, Benjamin Coutu =0Awrote:=0A=0A> Hello,=0A>=0A> Please consider the following PHP code:= =0A>=0A> while ( ) {=0A> =0A> }=0A>=0A> It current= ly compiles to something like this for the Zend Engine:=0A>=0A> 0: =0A> 1: JMPZ -> 4=0A> 2: =0A> 3: JMP -> 0=0A> 4: ...=0A>=0A> = This can easily be rewritten in an equivalent but much more efficient form:= =0A>=0A> 0: JMP -> 2=0A> 1: =0A> 2: =0A> 3: JMPNZ -> = 1=0A>=0A> The trick is to jump to the conditional expression before the fir= st=0A> iteration and then only having to continue with just on conditional = jump on=0A> every subsequent cycle.=0A>=0A> This would result in only 1 jum= p per loop iteration compared to=0A> effectively 2 jumps needed per iterati= on with the current implementation.=0A>=0A> It would also make the loop tig= hter as upon entering the loop (after first=0A> conditional evaluation), on= e less opcode remains relevant to the loop.=0A>=0A> An analogous approach c= an be taken for the for-loop.=0A>=0A> Thanks,=0A>=0A> Ben=0A>=0A> PS: This = is my first post on a PHP mailinglist - I am very excited! :)=0A>=0A> --=0A= >=0A> Benjamin Coutu=0A> Zeyon Technologies Inc.=0A> http://www.zeyos.com= =0A>=0A>=0A> --=0A> PHP Internals - PHP Runtime Development Mailing List=0A= > To unsubscribe, visit: http://www.php.net/unsub.php=0A>=0A>