Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96227 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71230 invoked from network); 4 Oct 2016 15:24:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Oct 2016 15:24:08 -0000 Authentication-Results: pb1.pair.com smtp.mail=thruska@cubiclesoft.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=thruska@cubiclesoft.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain cubiclesoft.com designates 149.56.142.28 as permitted sender) X-PHP-List-Original-Sender: thruska@cubiclesoft.com X-Host-Fingerprint: 149.56.142.28 28.ip-149-56-142.net Received: from [149.56.142.28] ([149.56.142.28:36136] helo=28.ip-149-56-142.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0B/F5-06241-799C3F75 for ; Tue, 04 Oct 2016 11:24:07 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: thruska@cubiclesoft.com) with ESMTPSA id D12DC3E848 To: Pascal KISSIAN , PHP Development References: <004101d21e33$124edf90$36ec9eb0$@lool.fr> Message-ID: <79b96cf5-dd10-11e9-8b8c-5fd763fb4796@cubiclesoft.com> Date: Tue, 4 Oct 2016 08:24:02 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 In-Reply-To: <004101d21e33$124edf90$36ec9eb0$@lool.fr> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Feature Request: inline pseudo-instruction From: thruska@cubiclesoft.com (Thomas Hruska) On 10/4/2016 4:33 AM, Pascal KISSIAN wrote: > Hi everybody, > > I have an application where a small file is included at multiple places. > > So far so good. > > The problem is that this include consists in a small piece of code which is > inside a multi-level loop. > > The include is done about an average of 100.000 times . > > When I manually replace the include with the code which is inside, the > performance boost is about a 50-100 factor.. > (with opcache enabled, it is far worst without opcache). I'm not surprised. What's wrong with using PHP functions and/or classes? Use 'require_once' outside the loops, which brings in the file containing the function/class, and then execute the function inside the loop. Loading a file repeatedly incurs a lot of processing time to parse it into opcodes, which, as you noticed, the opcode cache helps significantly with. You are also already getting the performance benefits of the file already being in RAM thanks to whatever OS level in-memory file caching system your OS uses. Basically, your code is (ab)using system caching as a performance crutch, which means it is time to refactor your code. Once you switch to using a function, there are additional techniques that can be used to speed up the function itself - prefer PHP keywords over PHP functions (e.g. array_key_exists() is many times slower than 'isset'), find an alternate algorithm and/or shortcuts with the current algorithm, write a PHP extension, etc. The 'include' and 'require' keywords invite abuse similar to what you just described and, when abused, will have performance metrics similar to what you described. You aren't the first person to use those keywords in a loop and likely won't be the last. The documentation should be updated to reflect a preference for the 'require_once' and 'include_once' keywords, which would help to avoid the misunderstanding that no one should use any of the code inclusion keywords in a loop. Personally, I use 'require_once' exclusively for a wide variety of reasons. I've rarely seen a need to use the other include/require keywords. Most of the time I see 'include', I usually think, "That code could be refactored to use 'require_once' and the author would be better off for it." -- Thomas Hruska CubicleSoft President I've got great, time saving software that you will find useful. http://cubiclesoft.com/