Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100480 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14006 invoked from network); 9 Sep 2017 06:27:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Sep 2017 06:27:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=i@lvht.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=i@lvht.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lvht.net from 54.254.200.128 cause and error) X-PHP-List-Original-Sender: i@lvht.net X-Host-Fingerprint: 54.254.200.128 smtpbgsg2.qq.com Received: from [54.254.200.128] ([54.254.200.128:43365] helo=smtpbgsg2.qq.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 85/64-10715-0E983B95 for ; Sat, 09 Sep 2017 02:27:46 -0400 X-QQ-mid: bizesmtp5t1504938457tp5zpo1xd Received: from [192.168.1.3] (unknown [218.81.64.210]) by esmtp4.qq.com (ESMTP) with id ; Sat, 09 Sep 2017 14:27:36 +0800 (CST) X-QQ-SSF: 0130000000200020F340B00A0000000 X-QQ-FEAT: O7WJv9PNEqUAK11JoKCRvscBoTRC1nq6e2CM2BkCB3CQvgg8S8ymqC7zmUnHK wbUeVEsrjuptJCH475J0VQyhfuWnFjq0KwZY5o2EZrGQJOoh778VPM/9Sltqxp3FuxAuzWz 75ZZL/1wzKJ52jiY/Ry6uywaxQQXhU8+YudLDXYwz/8oRnUw0YiW6LkiH8EzmysajHlauwd 0BAml4yT25T9sGYl5/sG9I2WTT4+NyUKiyXD4aUIAmEJ9rqgAnyBB0ekFh7ukc60CG3OKXa MUUA== X-QQ-GoodBg: 0 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) In-Reply-To: Date: Sat, 9 Sep 2017 14:27:44 +0800 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <4D64625B-AA30-4493-84F0-F523ED459D60@lvht.net> References: <7E991FBB-C115-4AB9-B904-EBE7C0F24089@lvht.net> <8DB5D7F2-CB7A-4324-AB9F-D9934DD9C108@lvht.net> To: Rowan Collins X-Mailer: Apple Mail (2.3273) X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:lvht.net:qybgforeign:qybgforeign4 X-QQ-Bgrelay: 1 Subject: Re: [PHP-DEV] add Fiber (sackful coroutine) support From: i@lvht.net (Haitao Lv) > On 2 Sep 2017, at 20:19, Rowan Collins = wrote: >=20 > On 1 September 2017 14:02:29 BST, Haitao Lv wrote: >>=20 >> Fiber is a lightweight thread. Please see >> https://en.wikipedia.org/wiki/Fiber_(computer_science) >> And ruby support Fiber. Please see >> https://ruby-doc.org/core-2.4.1/Fiber.html >=20 > Ah, thanks, that makes more sense now. >=20 > I note that the examples there all implement it not as a keyword, but = as a library function, which maybe makes more sense: whereas "yield" = turns a function declaration into a generator declaration, = "Fiber\yield", as we might call it, is just a function call which can = happen anywhere and manipulates some global state. As the Zend Engine does not offer API to pause execution, I cannot = implement the await without introducing a new keyword (just like yield). > The choice of "await" also feels odd: you're not awaiting the thing on = the right-hand side of the keyword, you're sending it somewhere and = awaiting something else. await is chosen because we cannot reuse the `yield` keyword. Maybe we = can choose a more proper keyword. >> You can see the await as a resumable return. The value after it >> will be returned and the function >> will be paused. >=20 > Again, this explanation doesn't make sense if you can call foo() = without any Fiber code. If there isn't an active Fiber to pause, the = "await" line can't "return" anything; it will presumably throw an error = of some sort. >=20 > function foo($x) { > $y =3D Fiber\yield($x); > return $x + $y; > } > echo foo(1); >=20 > The 1 isn't "returned" to the echo, it's passed off to a global = function for further processing. This makes much more sense to me than = implying that foo() is no longer a normal function. await is not yield. It need a separate vm stack to work. So call the foo = function without a Fiber will throw a runtime error exception. >=20 >>> Your ->resume() doesn't seem to do anything a normal generator = can't, >> except that the yield is nested deeper. >>=20 >> The await is a nested deeper yield! >=20 > Yes, I understand that, but that seems to be completely unrelated to = the API design of having resume() act as start, continue, and final = result all rolled into one. As I say, I didn't entirely follow the = reasoning for allowing return in generators, but if Fibers are used for = the same purpose, I would expect the same concerns to arise. >=20 > However, I think the comparison to generators may be more distracting = than useful, as they seem to be very different solutions to the same or = related problems. If fiber were implemented, the generator would be a special fiber.