rfc:fiber
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:fiber [2018/04/13 00:06] – lvht | rfc:fiber [2018/06/12 07:40] (current) – move back to discussion krakjoe | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2017-09-13 | * Date: 2017-09-13 | ||
- | * Author: Haitao Lv, i@lvht.net | + | * Author: Haitao Lv<i@lvht.net>, Dmitry Stogov< |
* Status: Under Discussion | * Status: Under Discussion | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 16: | Line 16: | ||
===== Proposal ===== | ===== Proposal ===== | ||
==== Why not make it as a Extension? ==== | ==== Why not make it as a Extension? ==== | ||
+ | Fiber is a major language feature, that allows significant benefits for asynchronous frameworks. Providing it as an optional extension, just doesn' | ||
+ | |||
==== Implementation ==== | ==== Implementation ==== | ||
+ | === Proposed API === | ||
<code php> | <code php> | ||
final class Fiber { | final class Fiber { | ||
Line 25: | Line 28: | ||
public const STATUS_DEAD | public const STATUS_DEAD | ||
+ | /** | ||
+ | * @param callable $callable any php callable to be paused | ||
+ | * @param int $stack_size fiber stack init size | ||
+ | */ | ||
public function __construct(callable $callable = null, int stack_size = null) {} | public function __construct(callable $callable = null, int stack_size = null) {} | ||
+ | /** | ||
+ | * pause the current fiber and ~return~ the $arg1 | ||
+ | * as the Fiber:: | ||
+ | */ | ||
public static function yield($arg1) {} | public static function yield($arg1) {} | ||
+ | | ||
+ | /** | ||
+ | * Start or resume a fiber. | ||
+ | | ||
+ | * If the fiber is not started, call resume will init | ||
+ | * the $callable with all args. | ||
+ | * | ||
+ | * If the fiber is paused, call resume will send the first arg | ||
+ | * as the last Fiber:: | ||
+ | */ | ||
public function resume($arg1...) {} | public function resume($arg1...) {} | ||
+ | | ||
+ | /** | ||
+ | * Throw an exception into the fiber. | ||
+ | | ||
+ | * You code can use try/catch to process error in the | ||
+ | * top level function call. Some framework make heavy | ||
+ | * usage of this feature. | ||
+ | */ | ||
public function throw(Throwable $e) {} | public function throw(Throwable $e) {} | ||
- | |||
- | /** @throws \UnexpectedValueException */ | ||
- | public function __wakeup(): void {} | ||
- | |||
- | /** @throws \Error */ | ||
- | private function __clone() {} | ||
} | } | ||
</ | </ | ||
+ | === Usage Demo === | ||
<code php> | <code php> | ||
function sub1() | function sub1() | ||
Line 55: | Line 79: | ||
echo $fiber-> | echo $fiber-> | ||
</ | </ | ||
+ | |||
+ | === Implementation Detail === | ||
+ | In our simple implementation, | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 80: | Line 107: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | < | + | < |
- | </ | + | Martin Schröder is working on this at https://github.com/fiberphp/fiber-ext/pull/30. |
- | + | ||
- | Fiber does not support yielding during the internal call. Calling Fiber:: | + | |
- | + | ||
- | < | + | |
- | + | ||
- | Offering methods like < | + | |
- | + | ||
- | < | + | |
- | + | ||
- | The Fiber:: | + | |
- | + | ||
- | < | + | |
- | Both Ruby's Fiber and Lua's coroutine using the **resume()** API to **init** and **resume** their coroutine. There is no need to offer a **dedicate init** API. | + | And here is the comparison. |
+ | ^Property^Stackless Fiber^Native Fiber^ | ||
+ | |1 Minimum Memory Usage|VM stack only (4 KB)|VM & C stack (4 KB + 4 KB)| | ||
+ | |2 Supported Architecturs|any platform|x86 at this time| | ||
+ | |3 Yield in Internal Function|unsupported|supported| | ||
+ | |4 Yield in Iterator|unsupported|supported| | ||
- | < | + | Stackless fiber use less memory and are not platform-dependend |
- | You will get a Fatal Error like | + | Native fibers are very platform-dependend (2) and use more memory because they do need to allocate a C call stack (1). While memory allocation will be done using mmap() it will still reserve virtual memory (can be problematic for a large number of fibers on 32 bit systems due to limited virtual memory addressing). The big advantage is that all kinds of internal function call (3 & 4) are supported without any changes to the existing codebase. |
- | Fatal error: Uncaught Error: Cannot call Fiber::yield out of Fiber | + | < |
- | < | + | And as a language feature, Fiber should only offer the essential API. User can implement these methods |
- | Introducing new keywords like await/emit does not offer any essential benefit but only cause BC impact. | + | < |
- | Both Ruby's Fiber and Lua's coroutine | + | Both Ruby's Fiber and Lua's coroutine |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 113: | Line 133: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | ||
- | ===== Proposed Voting Choices ===== | + | Syntax like async/await can be implemented in the future, but it's out of the scope of this RFC. |
- | Simple 50%+1 majority vote. | + | |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 138: | Line 156: | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | * await/async keyword | ||
- | * dedicated Fiber::init API |
rfc/fiber.1523577990.txt.gz · Last modified: 2018/04/13 00:06 by lvht