3v4l.org

run code in 500+ PHP versions simultaneously
<?php class IteratorWithFilter { public $generator; public function __construct(array $data) { $this->generator = new \ArrayIterator($data); } public function filter() { $weakRef = WeakReference::create($this); $this->generator = new CallbackFilterIterator($this->generator, static function(array $row) use ($weakRef) { $filterObj = $weakRef->get(); return $filterObj->matchRow($row); }); $this->generator->filterObj = $this; // this assigment will link the objects exactly the same as if // bound function is passed directly, thus the WeakReference // will always be valid/nonGCed return $this; } public function matchRow(array $row) { return $row[0] === 'b'; } } function createIterator() { $iter = new IteratorWithFilter([['a'], ['b']]); return $iter->filter(); } for ($i = 0; $i < 10 * 1000; $i++) { $generator = createIterator()->generator; iterator_to_array($generator); // must success and always return ['b'] if (($i % 10) === 0) { gc_collect_cycles(); } if (($i % 1000) === 0) { echo round(memory_get_usage() / (1024 * 1024), 3) . " MiB\n"; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 2
Branch analysis from position: 29
Branch analysis from position: 2
Branch analysis from position: 26
Branch analysis from position: 14
filename:       /in/U3h2Q
function name:  (null)
number of ops:  30
compiled vars:  !0 = $i, !1 = $generator
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   36     0  E >   ASSIGN                                                       !0, 0
          1      > JMP                                                          ->27
   37     2    >   INIT_FCALL                                                   'createiterator'
          3        DO_FCALL                                          0  $3      
          4        FETCH_OBJ_R                                          ~4      $3, 'generator'
          5        ASSIGN                                                       !1, ~4
   39     6        INIT_FCALL                                                   'iterator_to_array'
          7        SEND_VAR                                                     !1
          8        DO_ICALL                                                     
   41     9        MOD                                                  ~7      !0, 10
         10        IS_IDENTICAL                                                 ~7, 0
         11      > JMPZ                                                         ~8, ->14
   42    12    >   INIT_FCALL                                                   'gc_collect_cycles'
         13        DO_ICALL                                                     
   44    14    >   MOD                                                  ~10     !0, 1000
         15        IS_IDENTICAL                                                 ~10, 0
         16      > JMPZ                                                         ~11, ->26
   45    17    >   INIT_FCALL                                                   'round'
         18        INIT_FCALL                                                   'memory_get_usage'
         19        DO_ICALL                                             $12     
         20        DIV                                                  ~13     $12, 1048576
         21        SEND_VAL                                                     ~13
         22        SEND_VAL                                                     3
         23        DO_ICALL                                             $14     
         24        CONCAT                                               ~15     $14, '+MiB%0A'
         25        ECHO                                                         ~15
   36    26    >   PRE_INC                                                      !0
         27    >   IS_SMALLER                                                   !0, 10000
         28      > JMPNZ                                                        ~17, ->2
   47    29    > > RETURN                                                       1

Function createiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U3h2Q
function name:  createIterator
number of ops:  8
compiled vars:  !0 = $iter
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   NEW                                                  $1      'IteratorWithFilter'
          1        SEND_VAL_EX                                                  <array>
          2        DO_FCALL                                          0          
          3        ASSIGN                                                       !0, $1
   33     4        INIT_METHOD_CALL                                             !0, 'filter'
          5        DO_FCALL                                          0  $4      
          6      > RETURN                                                       $4
   34     7*     > RETURN                                                       null

End of function createiterator

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

End of function __construct

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U3h2Q
function name:  filter
number of ops:  22
compiled vars:  !0 = $weakRef
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   INIT_STATIC_METHOD_CALL                                      'WeakReference', 'create'
          1        FETCH_THIS                                           ~1      
          2        SEND_VAL                                                     ~1
          3        DO_FCALL                                          0  $2      
          4        ASSIGN                                                       !0, $2
   15     5        NEW                                                  $5      'CallbackFilterIterator'
          6        CHECK_FUNC_ARG                                               
          7        FETCH_OBJ_FUNC_ARG                                   $6      'generator'
          8        SEND_FUNC_ARG                                                $6
          9        DECLARE_LAMBDA_FUNCTION                              ~7      [0]
         10        BIND_LEXICAL                                                 ~7, !0
   18    11        SEND_VAL_EX                                                  ~7
   15    12        DO_FCALL                                          0          
         13        ASSIGN_OBJ                                                   'generator'
   18    14        OP_DATA                                                      $5
   19    15        FETCH_THIS                                           ~11     
         16        FETCH_OBJ_W                                          $9      'generator'
         17        ASSIGN_OBJ                                                   $9, 'filterObj'
         18        OP_DATA                                                      ~11
   23    19        FETCH_THIS                                           ~12     
         20      > RETURN                                                       ~12
   24    21*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U3h2Q
function name:  {closure:IteratorWithFilter::filter():15}
number of ops:  10
compiled vars:  !0 = $row, !1 = $weakRef, !2 = $filterObj
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   16     2        INIT_METHOD_CALL                                             !1, 'get'
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !2, $3
   17     5        INIT_METHOD_CALL                                             !2, 'matchRow'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0  $5      
          8      > RETURN                                                       $5
   18     9*     > RETURN                                                       null

End of Dynamic Function 0

End of function filter

Function matchrow:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/U3h2Q
function name:  matchRow
number of ops:  5
compiled vars:  !0 = $row
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   RECV                                                 !0      
   27     1        FETCH_DIM_R                                          ~1      !0, 0
          2        IS_IDENTICAL                                         ~2      ~1, 'b'
          3      > RETURN                                                       ~2
   28     4*     > RETURN                                                       null

End of function matchrow

End of class IteratorWithFilter.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.41 ms | 1399 KiB | 18 Q