3v4l.org

run code in 300+ PHP versions simultaneously
<?php class FiberPool { /** @var Fiber[] */ private array $fibers = []; private int $max = 10; private array $results = []; public function __construct( private \Generator $generator, ) { } public function wait(): void { while ($this->fibers || $this->generator->valid()) { // Fill up the pending fibers while (count($this->fibers) < $this->max && $this->generator->valid()) { $this->startNextItem(); } foreach ($this->fibers as $key => $fiber) { // TBD: We could try/catch this and set the exception to the results array. if ($fiber->isSuspended()) { $fiber->resume(); } else if (!$fiber->isStarted()) { $fiber->start(); } if ($fiber->isTerminated()) { $this->results[$key] = $fiber->getReturn(); unset($this->fibers[$key]); $this->startNextItem(); } } // If we're in a fiber loop ourselves then suspend up the chain once per loop. if (Fiber::getCurrent()) { Fiber::suspend(); } } } public function getResults(): array { return $this->results; } private function startNextItem(): void { if ($this->generator->valid()) { $callable = $this->generator->current(); $this->fibers[$this->generator->key()] = new Fiber($callable); $this->generator->next(); } } } $consistentSuspend = function () { static $seed = [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0]; return array_pop($seed); }; $example = new FiberPool((function () use ($consistentSuspend) { $output = array_combine(range('A', 'Z'), range(1,26)); foreach ($output as $key => $item) { echo 'generating ', $item, "\n"; yield $key => function () use ($consistentSuspend, $item) { echo 'starting ', $item, "\n"; while ($consistentSuspend()) { echo 'suspending ', $item, "\n"; Fiber::suspend(); } echo 'returning ', $item, "\n"; return $item; }; } })()); $example->wait(); print_r($example->getResults());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3prn4
function name:  (null)
number of ops:  18
compiled vars:  !0 = $consistentSuspend, !1 = $example
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          1        ASSIGN                                                   !0, ~2
   69     2        NEW                                              $4      'FiberPool'
          3        DECLARE_LAMBDA_FUNCTION                          ~5      [1]
          4        BIND_LEXICAL                                             ~5, !0
   84     5        INIT_DYNAMIC_CALL                                        ~5
          6        DO_FCALL                                      0  $6      
          7        SEND_VAR_NO_REF_EX                                       $6
   69     8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $4
   86    10        INIT_METHOD_CALL                                         !1, 'wait'
         11        DO_FCALL                                      0          
   88    12        INIT_FCALL                                               'print_r'
         13        INIT_METHOD_CALL                                         !1, 'getResults'
         14        DO_FCALL                                      0  $10     
         15        SEND_VAR                                                 $10
         16        DO_ICALL                                                 
         17      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3prn4
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $seed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   BIND_STATIC                                              !0
   66     1        INIT_FCALL                                               'array_pop'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   67     5*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 26
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 26
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 26
filename:       /in/3prn4
function name:  {closure}
number of ops:  28
compiled vars:  !0 = $consistentSuspend, !1 = $output, !2 = $item, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   GENERATOR_CREATE                                         
          1        BIND_STATIC                                              !0
   70     2        INIT_FCALL                                               'array_combine'
          3        INIT_FCALL                                               'range'
          4        SEND_VAL                                                 'A'
          5        SEND_VAL                                                 'Z'
          6        DO_ICALL                                         $4      
          7        SEND_VAR                                                 $4
          8        INIT_FCALL                                               'range'
          9        SEND_VAL                                                 1
         10        SEND_VAL                                                 26
         11        DO_ICALL                                         $5      
         12        SEND_VAR                                                 $5
         13        DO_ICALL                                         $6      
         14        ASSIGN                                                   !1, $6
   71    15      > FE_RESET_R                                       $8      !1, ->26
         16    > > FE_FETCH_R                                       ~9      $8, !2, ->26
         17    >   ASSIGN                                                   !3, ~9
   72    18        ECHO                                                     'generating+'
         19        ECHO                                                     !2
         20        ECHO                                                     '%0A'
   73    21        DECLARE_LAMBDA_FUNCTION                          ~11     [0]
         22        BIND_LEXICAL                                             ~11, !0
         23        BIND_LEXICAL                                             ~11, !2
   82    24        YIELD                                                    ~11, !3
   71    25      > JMP                                                      ->16
         26    >   FE_FREE                                                  $8
   84    27      > GENERATOR_RETURN                                         


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
Branch analysis from position: 6
filename:       /in/3prn4
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $consistentSuspend, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   74     2        ECHO                                                     'starting+'
          3        ECHO                                                     !1
          4        ECHO                                                     '%0A'
   75     5      > JMP                                                      ->11
   76     6    >   ECHO                                                     'suspending+'
          7        ECHO                                                     !1
          8        ECHO                                                     '%0A'
   77     9        INIT_STATIC_METHOD_CALL                                  'Fiber', 'suspend'
         10        DO_FCALL                                      0          
   75    11    >   INIT_DYNAMIC_CALL                                        !0
         12        DO_FCALL                                      0  $3      
         13      > JMPNZ                                                    $3, ->6
   80    14    >   ECHO                                                     'returning+'
         15        ECHO                                                     !1
         16        ECHO                                                     '%0A'
   81    17      > RETURN                                                   !1
   82    18*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 1

Class FiberPool:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3prn4
function name:  __construct
number of ops:  4
compiled vars:  !0 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'generator'
          2        OP_DATA                                                  !0
   13     3      > RETURN                                                   null

End of function __construct

Function wait:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 47) Position 1 = 51, Position 2 = 55
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 1
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 1
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 2
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 43
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 43
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 42
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 42
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 42
Branch analysis from position: 33
Branch analysis from position: 42
Branch analysis from position: 30
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 49
Branch analysis from position: 47
2 jumps found. (Code = 47) Position 1 = 51, Position 2 = 55
Branch analysis from position: 51
Branch analysis from position: 55
Branch analysis from position: 49
Branch analysis from position: 43
Branch analysis from position: 2
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
Branch analysis from position: 13
Branch analysis from position: 13
Branch analysis from position: 55
filename:       /in/3prn4
function name:  wait
number of ops:  57
compiled vars:  !0 = $fiber, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E > > JMP                                                      ->49
   19     1    > > JMP                                                      ->4
   20     2    >   INIT_METHOD_CALL                                         'startNextItem'
          3        DO_FCALL                                      0          
   19     4    >   FETCH_OBJ_R                                      ~3      'fibers'
          5        COUNT                                            ~4      ~3
          6        FETCH_OBJ_R                                      ~5      'max'
          7        IS_SMALLER                                       ~6      ~4, ~5
          8      > JMPZ_EX                                          ~6      ~6, ->13
          9    >   FETCH_OBJ_R                                      ~7      'generator'
         10        INIT_METHOD_CALL                                         ~7, 'valid'
         11        DO_FCALL                                      0  $8      
         12        BOOL                                             ~6      $8
         13    > > JMPNZ                                                    ~6, ->2
   23    14    >   FETCH_OBJ_R                                      ~9      'fibers'
         15      > FE_RESET_R                                       $10     ~9, ->43
         16    > > FE_FETCH_R                                       ~11     $10, !0, ->43
         17    >   ASSIGN                                                   !1, ~11
   25    18        INIT_METHOD_CALL                                         !0, 'isSuspended'
         19        DO_FCALL                                      0  $13     
         20      > JMPZ                                                     $13, ->24
   26    21    >   INIT_METHOD_CALL                                         !0, 'resume'
         22        DO_FCALL                                      0          
   25    23      > JMP                                                      ->30
   27    24    >   INIT_METHOD_CALL                                         !0, 'isStarted'
         25        DO_FCALL                                      0  $15     
         26        BOOL_NOT                                         ~16     $15
         27      > JMPZ                                                     ~16, ->30
   28    28    >   INIT_METHOD_CALL                                         !0, 'start'
         29        DO_FCALL                                      0          
   31    30    >   INIT_METHOD_CALL                                         !0, 'isTerminated'
         31        DO_FCALL                                      0  $18     
         32      > JMPZ                                                     $18, ->42
   32    33    >   INIT_METHOD_CALL                                         !0, 'getReturn'
         34        DO_FCALL                                      0  $21     
         35        FETCH_OBJ_W                                      $19     'results'
         36        ASSIGN_DIM                                               $19, !1
         37        OP_DATA                                                  $21
   34    38        FETCH_OBJ_UNSET                                  $22     'fibers'
         39        UNSET_DIM                                                $22, !1
   36    40        INIT_METHOD_CALL                                         'startNextItem'
         41        DO_FCALL                                      0          
   23    42    > > JMP                                                      ->16
         43    >   FE_FREE                                                  $10
   41    44        INIT_STATIC_METHOD_CALL                                  'Fiber', 'getCurrent'
         45        DO_FCALL                                      0  $24     
         46      > JMPZ                                                     $24, ->49
   42    47    >   INIT_STATIC_METHOD_CALL                                  'Fiber', 'suspend'
         48        DO_FCALL                                      0          
   17    49    >   FETCH_OBJ_R                                      ~26     'fibers'
         50      > JMPNZ_EX                                         ~26     ~26, ->55
         51    >   FETCH_OBJ_R                                      ~27     'generator'
         52        INIT_METHOD_CALL                                         ~27, 'valid'
         53        DO_FCALL                                      0  $28     
         54        BOOL                                             ~26     $28
         55    > > JMPNZ                                                    ~26, ->1
   45    56    > > RETURN                                                   null

End of function wait

Function getresults:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3prn4
function name:  getResults
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   FETCH_OBJ_R                                      ~0      'results'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   50     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getresults

Function startnextitem:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/3prn4
function name:  startNextItem
number of ops:  21
compiled vars:  !0 = $callable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   FETCH_OBJ_R                                      ~1      'generator'
          1        INIT_METHOD_CALL                                         ~1, 'valid'
          2        DO_FCALL                                      0  $2      
          3      > JMPZ                                                     $2, ->20
   55     4    >   FETCH_OBJ_R                                      ~3      'generator'
          5        INIT_METHOD_CALL                                         ~3, 'current'
          6        DO_FCALL                                      0  $4      
          7        ASSIGN                                                   !0, $4
   56     8        FETCH_OBJ_R                                      ~7      'generator'
          9        INIT_METHOD_CALL                                         ~7, 'key'
         10        DO_FCALL                                      0  $8      
         11        NEW                                              $10     'Fiber'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
         14        FETCH_OBJ_W                                      $6      'fibers'
         15        ASSIGN_DIM                                               $6, $8
         16        OP_DATA                                                  $10
   58    17        FETCH_OBJ_R                                      ~12     'generator'
         18        INIT_METHOD_CALL                                         ~12, 'next'
         19        DO_FCALL                                      0          
   60    20    > > RETURN                                                   null

End of function startnextitem

End of class FiberPool.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.97 ms | 1018 KiB | 17 Q