Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100967 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56343 invoked from network); 27 Oct 2017 14:14:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Oct 2017 14:14:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=dennis@birkholz.biz; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@dennis.birkholz.biz; sender-id=pass Received-SPF: pass (pb1.pair.com: domain birkholz.biz designates 144.76.185.232 as permitted sender) X-PHP-List-Original-Sender: dennis@birkholz.biz X-Host-Fingerprint: 144.76.185.232 yukon.nexxes.net Received: from [144.76.185.232] ([144.76.185.232:58964] helo=mx01.nexxes.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D5/CE-28573-52F33F95 for ; Fri, 27 Oct 2017 10:13:59 -0400 Received: from [192.168.178.30] (xdsl-89-1-51-159.netcologne.de [89.1.51.159]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: dennis@birkholz.biz) by mx01.nexxes.net (Postfix) with ESMTPSA id D70A8482569 for ; Fri, 27 Oct 2017 16:13:54 +0200 (CEST) To: internals@lists.php.net References: <20171027121225.2AAC95FB05@mx.zeyos.com> Message-ID: Date: Fri, 27 Oct 2017 16:13:54 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <20171027121225.2AAC95FB05@mx.zeyos.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Apply substr() optimization to array_slice() From: php@dennis.birkholz.biz (Dennis Birkholz) Hello Benjamin, Am 27.10.2017 um 14:12 schrieb Benjamin Coutu: > Hello everyone, > > Please consider these two statements: > > substr($string, 0, $length); > array_slice($array, 0, $length, true); > > Currently, with substr(), if $offset is zero and $length is smaller or equal to the original string length we just increase the reference count of the original string and return it via RETURN_STR_COPY. > In that case we completely save the allocation of a new string. the optimization actually only returns the same string if the supplied length is the length of the string, see: https://lxr.room11.org/xref/php-src%40master/ext/standard/string.c#2436 > Now, array_slice() could be optimized similarly, but currently (unless the resulting array is expected to be empty) we always create a new array no matter if we actually have to. > The same mechanism as used with substr() could be applied to array_slice(), given that $offset is zero and of course only if $preserve_keys is true. > > A patch would look like this: > > if (length <= num_in && offset == 0 && preserve_keys) { > /* Copy the original array */ > ZVAL_COPY(return_value, input); > return; > } So the array_slice optimization should only do the some, otherwise the returned array would be longer than desired. Greets, Dennis