Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111754 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39748 invoked from network); 31 Aug 2020 22:45:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Aug 2020 22:45:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5365A1804AA for ; Mon, 31 Aug 2020 14:50:23 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RDNS_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from nebula.zort.net (unknown [96.241.205.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 31 Aug 2020 14:50:22 -0700 (PDT) Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by nebula.zort.net (Postfix) with ESMTPSA id B31EA2005D639; Mon, 31 Aug 2020 17:50:21 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.11.0 nebula.zort.net B31EA2005D639 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zort.net; s=zort; t=1598910621; bh=pM42dANIIgjc1BvDcu57yjE0y6j/W8oYn6h18t5ltfY=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=NWM0/tUWMAgfbQB75YMgiv+nA1AdtZHFRm/4gyPfG/wYmtMT1/mJMW8Y1zv7BjQss 1YO8Hzf55fPHIqkfK5lOlxLW7SComvtMMmMKL3iuDYNmNUIKZmeihNmRbr0Zlqle8S xt4IHZClgjOnK4hCrjm+7hfd4ZKwvDUvYwV6Y+xc= Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) In-Reply-To: Date: Mon, 31 Aug 2020 17:50:21 -0400 Cc: PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: <34212284-827A-41E1-B86B-F8B28214219C@zort.net> References: To: =?utf-8?Q?Riikka_Kalliom=C3=A4ki?= X-Mailer: Apple Mail (2.3608.120.23.2.1) Subject: Re: [PHP-DEV] Request for couple memory optimized array improvements From: jbafford@zort.net (John Bafford) Hi Riikka, > On Aug 31, 2020, at 14:13, Riikka Kalliom=C3=A4ki = wrote: >=20 > A common pattern that I've seen that could dearly use PHP internal > optimization, if possible, would be: >=20 > foreach (array_keys($array) as $key) { > } I have a draft RFC (https://wiki.php.net/rfc/foreach_void) and patch = (https://github.com/jbafford/php-src/tree/foreachvoid against php 7.0, I = think) that helps with this, by allowing the following syntax: foreach($iterable as $key =3D> void) { ... } This would iterate over the keys of the iterable, and does not retrieve = the values at all. In theory, this should be a general performance win any time one needs = to iterate over only the keys of an iterable, because it does not = require the use of an O(n) iteration and building of the array that = array_keys() would, plus it works on non-array types (such as generators = or iterators). It also is more performant than foreach($iterable as $key = =3D> $_) {}, because it omits the opcode that instructs the engine to = retrieve the value. (Presumably, a future direction could include using = this request to inform generators or iterators to only return keys, and = not values, which could further improve performance, especially if value = generation is expensive. But that is out of scope for this RFC.) If this is something we'd like for PHP 8.1 and there are no major = objections to the idea, then after 8.0 is released, I can move the RFC = out of Draft and into Under Discussion, and presuming a vote passes, = I'll update the patch as necessary to work against 8.0. But my time is = limited and I'm not willing to put further time into the code unless an = RFC vote passes. Thoughts anyone? -John