Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111792 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71613 invoked from network); 2 Sep 2020 19:43:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Sep 2020 19:43:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D237F1804D9 for ; Wed, 2 Sep 2020 11:47:55 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f67.google.com (mail-vs1-f67.google.com [209.85.217.67]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 2 Sep 2020 11:47:55 -0700 (PDT) Received: by mail-vs1-f67.google.com with SMTP id s62so290552vsc.7 for ; Wed, 02 Sep 2020 11:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GvJxJnoUvsAwHL8VAD3hvR7wrdS4TfdoMQ1N0cC3Wh0=; b=eZxAvxJgcDhhSs4QK4+fB4PbDEyIz/ZGoXnuE/lM/Ojcv8hCcUFPtle2JCfM6NMLAD v2d7EDwJtQvP0Gz+bl6vmOESk3DiuPN/Ac22Kz94Yeus8H8SgaAedXoTVfnFRIrSDJU9 K2kWGIMt4j2I6kUSp8Mp3QV4GtzZe4wfb7L59kbKYkP7xDF2nbCw8VnjMlP5iNucRBKL iDIq/TBwJB63ZEx3viS4xfC+LR6dTgHvOB4KD4e/b3P0mBMH3D4mVPIKACFmi7EUsgwn 9S5uek6rMAJT5vYf1z4IJ4dtnIzNg9LlO53+n6GLBnjhhvmdkZGXKHaCxhOEWOgcbKsC uLZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GvJxJnoUvsAwHL8VAD3hvR7wrdS4TfdoMQ1N0cC3Wh0=; b=ZyTx7ij5xVJwWs7Nwhhd0xdjWd7a9KehZeSEhsKoWPl1vk1uYBsUX7MpgA0XdGGoI+ QL1IXOIXJtD00BZFUT3jJKmADx1DAri2hzDpNcPhqZ1kKj+u0gBQ7Qqku6CSWoHLfJB6 h9Q+apl0GVz1O2dQyOCtzEIQE9iQcdKetY3ldmgaUqZWV8onmoBwTy/m7gdt3fcZJekL jkoJ0SZdfrP5OgrbkBvB0+xDNgiThl3z8PB6Rgw69TkHEEQO4UEWDlb/yhK+MiymCSrr JtJZHpST28na/0WO2W1hxBjM6y+xnjOpiugqHymST/CGin8FAIlQgA7SyO9tz+kgC7FK veSg== X-Gm-Message-State: AOAM533pbHgKDbNaieJZY11saiZnSrPPMZsOyIv2VKEUoQ2jLJlyH5UW YKhhk+y+NcK7zNRnW4i8z/2NxQxi2nNBgqisbXM= X-Google-Smtp-Source: ABdhPJwsD+XFyCEXUl+llv4O40k1wqp0VMofpwOOuYAsM8hcidjsZytHBRXdk/chLMpD2PJAjrgYajKUHzX7O1NC6Wc= X-Received: by 2002:a67:1e81:: with SMTP id e123mr6101215vse.210.1599072472708; Wed, 02 Sep 2020 11:47:52 -0700 (PDT) MIME-Version: 1.0 References: <89FF9360-609A-439F-BDBE-B3B4C141E00F@newclarity.net> In-Reply-To: Date: Wed, 2 Sep 2020 14:47:40 -0400 Message-ID: To: Nikita Popov Cc: Mike Schinkel , John Bafford , PHP internals Content-Type: multipart/alternative; boundary="000000000000b4bb4405ae59121c" Subject: Re: [PHP-DEV] Draft RFC: foreach iteration of keys without values From: chasepeeler@gmail.com (Chase Peeler) --000000000000b4bb4405ae59121c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Sep 2, 2020 at 1:13 PM Nikita Popov wrote: > On Wed, Sep 2, 2020 at 5:16 AM Mike Schinkel wrote: > > > This is a new thread for John Bafford's RFC which he mentioned on anoth= er > > thread, see below: > > > > https://wiki.php.net/rfc/foreach_void < > > https://wiki.php.net/rfc/foreach_void> > > > > Just like the first time this was discussed, I don't think this RFC makes= a > sufficient case on why we need explicit syntax for this. Just ignoring th= e > value is an existing pattern that works across all PHP versions: > > foreach ($iterable as $key =3D> $_) { ... } > > Introducing special syntax for this has costs, both in terms of language > complexity and in terms of implementation complexity. For example, > implementing this feature will make foreach (whether or not the value is > ignored) marginally slower, because we will have to explicitly distinguis= h > this case. (Or alternatively, we'd have to specialize and increase VM cod= e > size -- it's not free in any case.) > > There were two justifications given in the RFC. The first was that $key =3D= > $value and ignoring the value has performance costs. You indicate there would be a cost to dealing with a special character there in order to know whether or not to ignore the value. So, I think the question is which one causes the bigger performance hit. The other justification was semantic. I don't personally find that justification for the change, especially if we could actually be looking at performance implications. > Iterating over just the keys is not a super common pattern and we already > have a reasonable way to do it (that is imho just as clear, and actually > more concise than the proposed "null" variant). The only potential > advantage to a dedicated syntax that I see is that there could be iterato= rs > that can cheaply produce keys, but have expensive to produce values. That > seems like an edge case of an edge case though, and is a situation where > manually calling ->key() would be justifiable. > > Regards, > Nikita > > > On Aug 31, 2020, at 5:50 PM, John Bafford wrote: > > > > > > Hi Riikka, > > > > > >> On Aug 31, 2020, at 14:13, Riikka Kalliom=C3=A4ki < > > riikka.kalliomaki@riimu.net> wrote: > > >> > > >> A common pattern that I've seen that could dearly use PHP internal > > >> optimization, if possible, would be: > > >> > > >> 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 retrie= ve > > the values at all. > > > > > > In theory, this should be a general performance win any time one need= s > > to iterate over only the keys of an iterable, because it does not requi= re > > 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, whi= ch > > 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 limit= ed > > and I'm not willing to put further time into the code unless an RFC vot= e > > passes. > > > > > > Thoughts anyone? > > > > +1 from me. > > > > BTW, your RFC says "Next PHP 7.x" for Proposed Version; might need to > > update that? > > > > -Mike > --=20 Chase Peeler chasepeeler@gmail.com --000000000000b4bb4405ae59121c--