Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111789 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59029 invoked from network); 2 Sep 2020 18:08:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Sep 2020 18:08:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A81B718053F for ; Wed, 2 Sep 2020 10:13:39 -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-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (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 10:13:39 -0700 (PDT) Received: by mail-lj1-f180.google.com with SMTP id t23so69856ljc.3 for ; Wed, 02 Sep 2020 10:13:39 -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=vfoHQthL7kP30incBaQ/oBOD1UktL8xBrxsMGOq2kDY=; b=T8UegxlqWqZcnGOxHh3/iwuf8t6hX+M9BYjoAGly1sxYWIn04QvnlbJe826GDns5mB By7G4balIm4bpwehW3yRLTW6eNP0x4Wp9qbbP9iVc8kIrL4c6Z8oT8w7Wxnc9t13A+DF 7mK8YrFdRrjOqGMnr4pCf5oAZJ4W+PKZZSa5s8MwWALDXEskBuYwn/ED0IMdZ1EK5FW0 Z5zra03R8yvP5Nie6fSPWfduGW02bGiQ1h0LlyXe8gLH7ePem/KIZwoxjJFw5sK/j6Ui 4CUmCzrZqHsrU+UL6m1dkma95nsYbXhQOHo6W2UH/MTiJFK3idsrxuCVfN7Xlx9vyTjE OHrA== 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=vfoHQthL7kP30incBaQ/oBOD1UktL8xBrxsMGOq2kDY=; b=Z7GoifOzCdQ7KC5vfxXgLYYbaUIk23fCAa8IAJvtJjx6ASM7GA6neeJPAORvKG4A4T IsK/1W5R90wgJXVJS3vxRJWbZALigkW0O/dgJlz7V+olUyU6NIk8x13YaxQVizNZqRjY gqfduSpLPSl/72hnfwbsPNxc+xIq4jHzv+MOR96KQC5M3y3t5vX3gWMdBWfRozt3PEt4 2c/go7WTeU9JbOgGgv5f4Jj1aw0cvqpsIDSvEKNHvNby1uqcAp7/wmFOhOkn1L6TbP6o gnUp4yBKlE9qmnpeQRMAqV11S0guQo4bG3Co/5FhUKDsbItrGGXL9Wht/f4EztArb33N g30A== X-Gm-Message-State: AOAM531MZMUxGRdFxu7UM/007Ohr9eABOBwmPsWjcLaqmw3IdI39cCZP EQ4JsvGAy6/I6aco6YjYO8RA6H0ep30cOg0Rsf8= X-Google-Smtp-Source: ABdhPJznD5KVDwEkCkOA/LFvmUPvH2F1STyrYlIQksVbBq5pQ7MmIJ3M4joCDgpj3O8hdnlVZ2cd37p1ZwecVb46RKc= X-Received: by 2002:a2e:968c:: with SMTP id q12mr4017567lji.345.1599066815902; Wed, 02 Sep 2020 10:13:35 -0700 (PDT) MIME-Version: 1.0 References: <89FF9360-609A-439F-BDBE-B3B4C141E00F@newclarity.net> In-Reply-To: <89FF9360-609A-439F-BDBE-B3B4C141E00F@newclarity.net> Date: Wed, 2 Sep 2020 19:13:20 +0200 Message-ID: To: Mike Schinkel Cc: John Bafford , PHP internals Content-Type: multipart/alternative; boundary="00000000000088b7e505ae57c1b6" Subject: Re: [PHP-DEV] Draft RFC: foreach iteration of keys without values From: nikita.ppv@gmail.com (Nikita Popov) --00000000000088b7e505ae57c1b6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 another > 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 the 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 distinguish this case. (Or alternatively, we'd have to specialize and increase VM code size -- it's not free in any case.) 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 iterators 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 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 t= o > 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 ou= t > 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? > > +1 from me. > > BTW, your RFC says "Next PHP 7.x" for Proposed Version; might need to > update that? > > -Mike --00000000000088b7e505ae57c1b6--