Re: Draft RFC: foreach iteration of keys without values
| From: | Mike Schinkel | Date: | Thu, 03 Sep 2020 04:06:32 +0000 |
| Subject: | Re: Draft RFC: foreach iteration of keys without values | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111800@lists.php.net to get a copy of this message | ||
> On Sep 2, 2020, at 5:54 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
>
> Hi!
>
>>> 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 => $_) {}, 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
>
> To me, it looks like the case of premature micro-optimization. Unless
> there's a benchmark that proves it achieves speedup in a real-life
> application (not tight-loop synthetic benchmark) I think
> foreach($iterable as $key => $_) {} is completely fine. My opinion has
> been and remains, absent new data, that if your code performance hinges
> on a couple of simple opcodes being there, maybe you'd better implement
> that part in C, but in most real-life applications it is not and any
> performance gain you might get from such syntax is microscopic.
If it adds a micro-optimization, great, but allowing a developer to explicitly signal intent is the
primary argument for adding void. IMO.
-Mike