rfc:fiber

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
rfc:fiber [2018/02/10 09:31]
lvht
rfc:fiber [2018/06/10 13:54]
lvht
Line 2: Line 2:
   * Version: 0.1   * Version: 0.1
   * Date: 2017-09-13   * Date: 2017-09-13
-  * Author: Haitao Lvi@lvht.net +  * Author: Haitao Lv<i@lvht.net>, Dmitry Stogov<dmitry@zend.com>, Martin Schröder<m.schroeder2007@googlemail.com> 
-  * Status: Under Discussion+  * Status: Voting
   * First Published at: http://wiki.php.net/rfc/fiber   * First Published at: http://wiki.php.net/rfc/fiber
  
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't make sense.
 +
 ==== Implementation ==== ==== Implementation ====
  
 +=== Proposed API ===
 <code php> <code php>
 final class Fiber { final class Fiber {
Line 25: Line 28:
   public const STATUS_DEAD      = 4;   public const STATUS_DEAD      = 4;
  
 +  /**
 +   * @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::resume's return value.
 +   */
   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::yield's return value.
 +   */
   public function resume($arg1...) {}   public function resume($arg1...) {}
-  public function throw(Exception $e) {} +   
- +  /** 
-  /** @throws \UnexpectedValueException */ +   Throw an exception into the fiber. 
-  public function __wakeup(): void {} +   *  
- +   * You code can use try/catch to process error in the 
-  /** @throws \Error */ +   * top level function call. Some framework make heavy 
-  private function __clone() {}+   * usage of this feature. 
 +   */ 
 +  public function throw(Throwable $e) {}
 } }
 </code> </code>
  
 +=== Usage Demo ===
 <code php> <code php>
 function sub1() function sub1()
Line 55: Line 79:
 echo $fiber->resume("hello "); // echo "hello world" echo $fiber->resume("hello "); // echo "hello world"
 </code> </code>
 +
 +=== Implementation Detail ===
 +In our simple implementation, we only backup/restore the **zend stack**. We **cannot** pause a Fiber during internal function call like `array_map`.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 80: Line 107:
  
 ===== Open Issues ===== ===== Open Issues =====
-From Nikita +<blockquote>Why not support backup/restore the stack?</blockquote> 
-<blockquote>What happens if there are internal calls on the call stack?Say something like array_map(function() { await; }, [1, 23]); inside a fiber. Internal calls (using the C stack rather than the VM stack) are usually the problem with this kind of endeavor+Martin Schröder is working on this at https://github.com/fiberphp/fiber-ext/pull/30.  
-</blockquote>+ 
 +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)
 +|Supported Architecturs|any platform|x86 at this time| 
 +|Yield in Internal Function|unsupported|supported| 
 +|4 Yield in Iterator|unsupported|supported| 
 + 
 +Stackless fiber use less memory and are not platform-dependend (1 & 2) which makes them very portable and efficientThey do however lack support for anything that involves internal function calls (3) including opcode handlers (4, e.g. foreach loop). 
 + 
 +Native fibers are very platform-dependend (2) and use more memory because they do need to allocate a 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. 
 + 
 +<blockquote>Why not introduce helper like **Fiber::alive(),Fiber::running()**?</blockquote> 
 + 
 +And as a language feature, Fiber should only offer the essential API. User can implement these methods in user land code easily. 
 + 
 +<blockquote>Why not introduce a dedicate method other than **Fiber::resume()** for Fiber initialization?</blockquote> 
 + 
 +Both Ruby's Fiber and Lua's coroutine using the same **resume()** API to **init** and **resume** their coroutine.
  
-Fiber does not support yielding during the internal call. Calling Fiber::yield in a internal call will cause core dump. 
-However, avoiding yield in the internal still make sense and useful 
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
 None None
  
 ===== 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.+ 
 +Syntax like async/await can be implemented in the future, but it's out of the scope of this RFC.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Simple 50%+1 majority vote.+2/3+1 voting majority 
 +<doodle title="RFC name here" auth="your_username_here" voteType="single" closed="false"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-coming soonCurrent developing at https://github.com/fiberphp/fiber-ext +  * https://github.com/php/php-src/pull/3203 
- +  * <del>[[https://github.com/php/php-src/pull/2723]]</del> 
-  * <del>[[https://github.com/php/php-src/pull/2733|GitHub PR #2733]]</del>+  * <del>[[https://github.com/php/php-src/pull/2733]]</del> 
 +  * <del>[[https://github.com/php/php-src/pull/2886]]</del> 
 +  * <del>[[https://github.com/php/php-src/pull/2902]]</del>
  
 ===== Implementation ===== ===== Implementation =====
Line 113: Line 163:
  
 ===== Rejected Features ===== ===== Rejected Features =====
-Keep this updated with features that were discussed on the mail lists. 
rfc/fiber.txt · Last modified: 2018/06/12 07:40 by krakjoe