Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104846 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73665 invoked from network); 21 Mar 2019 15:37:17 -0000 Received: from unknown (HELO mail-io1-f53.google.com) (209.85.166.53) by pb1.pair.com with SMTP; 21 Mar 2019 15:37:17 -0000 Received: by mail-io1-f53.google.com with SMTP id x3so4995360iol.10 for ; Thu, 21 Mar 2019 05:29:26 -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; bh=HDu4P8HEJyo1/iw5lrhVkIbXLMbVsBwUSySstwcfBDE=; b=irX1uVdZ2aQ15xZfgLWGcw110BKO1+3vPlXMJexgN9v7voP3gUUejrortnz9EQ7J9c YEbNP0t99VENZG3pzjfSHP8/RztaIhHBzQ1jbN1fqSYw3RlKfgxIsXVzJNOsE3Lp5nWR yKTMxgS+bZAAVKzE1Qv1UrOTMB/JHRGPFUfFTx5svv/KI8M5sE0K/48hvYF+CZyyz0Z9 mMIltYUE35/UQakjbhHNrvfPOTBOoHscLUlyew7MUtIBIMaV9GE4jtKlOwybog3z25jc cJ09ssIyvBv8XWGUanjWIy+yPQtOfN7Q0PRqnIpVwPiM3GdGxQJx+aKL4rskRLNtWKfC qe3Q== 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; bh=HDu4P8HEJyo1/iw5lrhVkIbXLMbVsBwUSySstwcfBDE=; b=lbBNWg4S6sM3wHqMTbHYbTZdPivYZSNhUxrmBcYpMJR/3ebYdGS2YW/JsfzSKsOxVx yqehCO3wzZBWPLDWldUhS6aVpaiXcze3pk0yqchlZBa9tP+IH0j/Po3FUKbedL4v+4mx 3junDsDOlRGY3YhiBnhAdBXtZ/5/OWWVO6zjBHJuGMLeuza9/5dz/QOqihJ/SR6YPXEb G/BDgck0Y6JPsjMH7/z5icFPlUlVXfWhhvr6WwZnl8390iiVmmuq0xtdV+/eyhyl9ZtY PvEAcv3syzXrRunlQGii+hocZ28v8jeh2gFznFNdX5MLxtFxS6yJ4WVg0T+H/NuQlnn/ QaTA== X-Gm-Message-State: APjAAAVAAIR8OdNi3E3LqtHpGm02v7XAGxZVUBE1t9AC97W0wF7LFVl4 i+dbH3oN01AVxlDjpLJNgVnfTVlpive/LfckvpEuRt87 X-Google-Smtp-Source: APXvYqwKrYsWccbrVAYTnTRJDPp5e3KiKK9+ngz7y4JQsLPDKA8kjnSq57sDNDjVvfjEuzniapc7bUAitbiNMdS/yYo= X-Received: by 2002:a6b:6d15:: with SMTP id a21mr2193245iod.235.1553171365367; Thu, 21 Mar 2019 05:29:25 -0700 (PDT) MIME-Version: 1.0 References: <85417a73-a391-c9ad-49aa-5b88c65b0900@gmail.com> <5c8ea43d.1c69fb81.e50f1.7d18SMTPIN_ADDED_MISSING@mx.google.com> <0d34b3b1-866e-4bab-9a8f-3c70f1fc9721@www.fastmail.com> <752c83ab-0e6f-d8f5-ff15-e1f75b5d04b5@gmail.com> <301552a9-6f01-3177-0e57-3d9aac54cc0c@gmail.com> <7A288F12-1227-4E78-8F9A-0959432F02D8@gmail.com> In-Reply-To: Date: Thu, 21 Mar 2019 12:29:13 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000821522058499e3d5" Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: rowan.collins@gmail.com (Rowan Collins) --000000000000821522058499e3d5 Content-Type: text/plain; charset="UTF-8" On Thu, 21 Mar 2019 at 11:00, Robert Hickman wrote: > I was only making a point for where a non-generator version of > comprehensions could be useful, under the premise "Therefore, a > lazy-evaluating syntax is more powerful, in that it can do everything > an eager-evaluating one can do *and more*.". This implies that it > dosn't have any downsides, whereas performance may be a downside. > Ah, OK, I see where you're coming from. I'm no expert, but I'm not sure how much difference it would actually make. Whether the comprehension is eagerly evaluated internally, or by a call to iterator_to_array, the input will always need to looped over exactly once, with each element being tested against the condition and run through the output mapping. A manual eager implementation (with no output mapping, for simplicity) would look like this: foreach ( $array as $element ) { if ( someCondition ) $newArray[] = $element; } Which is basically equivalent to this: while ( $element = $arrayIterator->next() ) { if ( someCondition ) $newArray[] = $element; } A lazy implementation just moves the if check into the iterator; it might look like this: while ( $element = $filteredArrayIterator->next() ) { if ( someCondition ) $newArray[] = $element; } But $filteredArrayIterator->next() is actually $arrayIterator->next() with a built-in if check, so you could also picture it as doing this: while ( $element = $arrayIterator->nextIf ( someCondition ) ) { $newArray[] = $element; } So the underlying operations are the same, they just happen in different orders. The only real difference I can see is that under a very specific set of circumstances, it could be compiled into a tight loop on the CPU. It would require a JIT to check all of the following held, though: - The input is a plain array, not any other iterable - The condition can be inlined (e.g. not a function call like "if ( checkSomething($x) )") - The output mapping can be inlined (e.g. not a function call like "yield doSomething($x)") Returning a generator by default just requires an extra check in that list: - The expression is contained within iterator_to_array (or whatever syntax shortcut we come up with for that). I might be wrong, though, in which case this would indeed be an argument in favour of having a dedicated array-returning syntax, either alongside or instead of a generator-returning one. Regards, -- Rowan Collins [IMSoP] --000000000000821522058499e3d5--