Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80647 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21446 invoked from network); 16 Jan 2015 16:12:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2015 16:12:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.178 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.178 mail-vc0-f178.google.com Received: from [209.85.220.178] ([209.85.220.178:41086] helo=mail-vc0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F3/63-00273-47839B45 for ; Fri, 16 Jan 2015 11:12:37 -0500 Received: by mail-vc0-f178.google.com with SMTP id hq11so6996853vcb.9 for ; Fri, 16 Jan 2015 08:12:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=xE9Vtnje3rAT15Tt3Ed2nBOCUGf5JDRX8wiJ0sh9rfk=; b=fqo3O9VVc/hPrGM2FArsmyAPIEw8pcBcluKxZKFHtIzRNzHQ8e2bAxXs3Un1i0sNJZ y/gxPPY6kTUuyJlNRNti1ydDa+FJik3pkpHsoEM/DuI6qQJhkFelKYZf+31Pf0THFkv+ f7Rmp/YyeMD87lnW8I/r0Vel9kw12OdlC7EWKiW+m4hDJ+SNVQP7RXZpupQ7KcDglyo2 lu/ljW795xuy/pEwubDR2fEtsRqmNPDbw0AWnJEJVslyGPbsSrFlL3Wn9bAsrUrySCz5 PyJhR/jIF9KG7x1p7vNAZ9ZroKlyzUrYd4kUJMzVUFZvqn0j/AbanuNzE80Bg8ciLJ3+ zjZQ== X-Gm-Message-State: ALoCoQnDG4Vig4TnhxjIxwb95k7OOYRPcm5AA7XDhXjswOM7FvufrpYO36x68BdhSf+Ao276TEVKYpQ1B8omoNm1ZtVnjRSNp5CjBEPnwYshzEgTBWilE/GLlmAjfQ2nh9fH5b+y8RQJXKGz/4jlBAkZw9ds2XlBew== MIME-Version: 1.0 X-Received: by 10.52.252.3 with SMTP id zo3mr6508896vdc.51.1421424754197; Fri, 16 Jan 2015 08:12:34 -0800 (PST) Received: by 10.52.26.40 with HTTP; Fri, 16 Jan 2015 08:12:34 -0800 (PST) In-Reply-To: <20150116075700.D39C25F7F7@mx.zeyon.net> References: <20150116075700.D39C25F7F7@mx.zeyon.net> Date: Fri, 16 Jan 2015 20:12:34 +0400 Message-ID: To: Benjamin Coutu Cc: PHP Internals , Xinchen Hui Content-Type: multipart/alternative; boundary=001a1133e3b88c9e01050cc73d2d Subject: Re: [PHP-DEV] Generating more efficient code for while-loop From: dmitry@zend.com (Dmitry Stogov) --001a1133e3b88c9e01050cc73d2d Content-Type: text/plain; charset=UTF-8 Hi Benjamin, Thanks for idea. With PHP7 AST compiler, it's quite easy to implement this (it took us 15 minutes to try :) However, it doesn't make big improvement on our benchmarks. We will take a look into possibilities to apply your idea to other patterns (e.g. for and foreach loops). Anyway, it makes full sense to include this optimization into PHP7. I just like to try other opportunities first. Thanks. Dmitry. On Fri, Jan 16, 2015 at 10:56 AM, Benjamin Coutu wrote: > Hello, > > Please consider the following PHP code: > > while ( ) { > > } > > It currently compiles to something like this for the Zend Engine: > > 0: > 1: JMPZ -> 4 > 2: > 3: JMP -> 0 > 4: ... > > This can easily be rewritten in an equivalent but much more efficient form: > > 0: JMP -> 2 > 1: > 2: > 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 > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a1133e3b88c9e01050cc73d2d--