Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111790 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60979 invoked from network); 2 Sep 2020 18:17:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Sep 2020 18:17:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 94202180549 for ; Wed, 2 Sep 2020 10:22:00 -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-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.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 10:21:59 -0700 (PDT) Received: by mail-lf1-f67.google.com with SMTP id y17so215004lfa.8 for ; Wed, 02 Sep 2020 10:21:59 -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=47rJKnKpN0pIjRVNzoAEWLIhjVSrzbdNHgQ5pksP1As=; b=scSShHf9psanCcuyaTNxdNK/X+zgPWQOolFQAe5GwzT+mBTwMDq/x1vXpdfXf3Rsl4 RBaLgjhYvYVRyj60STWafcFmo6Tlp6+N69VQ9oKErmToYBUIkG45KTsSXnDaO/GlFX9I cuQaDr4S2+Tc5zZSsB0BvQoCinBRRcENHjKmFllJP+SM3MVFXpCN1tWihHvveV8ct7b7 Kc8zQI/FiEBCceC6h2xgQpNQX8p5DYuu7BfSoQbNPfDH18/n2tXnN66lNbekkrsmrUfk k1yWv3Z9JT/1ckhJrzaGoLhVd9+FZILYXTwf+ReESVB1jIpJ702BSctvkmW5XmZHvoRG D7uw== 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=47rJKnKpN0pIjRVNzoAEWLIhjVSrzbdNHgQ5pksP1As=; b=LHZYSRTTTiFbFr+MDzoiUPi2j6QvOQUfViUEWwaaDp/Dirdqm4KtHSmKt+MCXnewVV WvBCMehaIoD9QvotAIPTz7K0D/hLupT/kpVb19OorskpQn62D9fwT46WdzNP2bSdkFkO Kh0BYLl3BeNcZ+fezu48Pg8KzDfsdILxdKRmCZzu0hiKiru7CZJdCB9aIX7YfaygqJVh akTK3IYZt+bf2g/URkYmZD0aH/hcXxfCjdTJ6t5/P0oBsOV/myVti2i2S2ZFuqHp/AvY kij7YOwbuhX7FZ0P3WsZSH6iusmybcUyAeR4PW8f4TH2hVMkpqJwfvpvWJiwhxvkvUFV 9LtQ== X-Gm-Message-State: AOAM532+pnsthprpUsH2plKszpJTiXkUXNHx7GrY/L9aAja+hhvdLb7K xtUeQqD0v8ntA0gg5XJT0inv0lQOq5Tvbh+FCQU= X-Google-Smtp-Source: ABdhPJzTlPVmH77D3x3auQROi+Ug/oFJLdq+GQghMF5z7llNUABlHDWJwx6ZpXIaw3uOHHgYqM6DxxL7gFZegvWhSTM= X-Received: by 2002:a19:6411:: with SMTP id y17mr3832915lfb.199.1599067316072; Wed, 02 Sep 2020 10:21:56 -0700 (PDT) MIME-Version: 1.0 References: <89FF9360-609A-439F-BDBE-B3B4C141E00F@newclarity.net> In-Reply-To: Date: Wed, 2 Sep 2020 20:21:44 +0300 Message-ID: To: Nikita Popov Cc: Mike Schinkel , John Bafford , PHP internals Content-Type: multipart/alternative; boundary="00000000000058b53905ae57df5a" Subject: Re: [PHP-DEV] Draft RFC: foreach iteration of keys without values From: benas.molis.iml@gmail.com (Benas IML) --00000000000058b53905ae57df5a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable +1. I'd also like to mention that other languages don't have any kind of syntactic sugar/special syntax for iterating over keys. That is achieved using a function though. This proposal to me seems a bit like the "return if/guard" RFC i. e. I see no value in it. On Wed, Sep 2, 2020, 8: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.) > > 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 > --00000000000058b53905ae57df5a--