3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IteratorWithFilter { public $generator; public function __construct(array $data) { $this->generator = new \ArrayIterator($data); } public function filter(Closure $closure) { $closure = clone $closure; $closure = $closure->bindTo($this); $this->generator = new \CallbackFilterIterator($this->generator, static function () {}); $this->generator->cl = $closure; // this assignment prevents GC as well return $this; } } function createIterator(Closure $closure) { $iter = new IteratorWithFilter([['a'], ['b']]); return $iter->filter($closure); } function createClosure() { return function ($row) { return true; }; } $closure = createClosure(); for ($i = 0; $i < 50 * 1000; $i++) { createIterator($closure); if (($i % 1000) === 0) { // gc_collect_cycles(); // explicit GC does not help... 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 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 5
Branch analysis from position: 23
Branch analysis from position: 5
Branch analysis from position: 20
filename:       /in/AL2Hr
function name:  (null)
number of ops:  24
compiled vars:  !0 = $closure, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   INIT_FCALL                                               'createclosure'
          1        DO_FCALL                                      0  $2      
          2        ASSIGN                                                   !0, $2
   37     3        ASSIGN                                                   !1, 0
          4      > JMP                                                      ->21
   38     5    >   INIT_FCALL                                               'createiterator'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0          
   40     8        MOD                                              ~6      !1, 1000
          9        IS_IDENTICAL                                             ~6, 0
         10      > JMPZ                                                     ~7, ->20
   42    11    >   INIT_FCALL                                               'round'
         12        INIT_FCALL                                               'memory_get_usage'
         13        DO_ICALL                                         $8      
         14        DIV                                              ~9      $8, 1048576
         15        SEND_VAL                                                 ~9
         16        SEND_VAL                                                 3
         17        DO_ICALL                                         $10     
         18        CONCAT                                           ~11     $10, '+MiB%0A'
         19        ECHO                                                     ~11
   37    20    >   PRE_INC                                                  !1
         21    >   IS_SMALLER                                               !1, 50000
         22      > JMPNZ                                                    ~13, ->5
   44    23    > > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FAL2Hr%3A17%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AL2Hr
function name:  {closure}
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FAL2Hr%3A17%240

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

End of function createiterator

Function createclosure:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AL2Hr
function name:  createClosure
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FAL2Hr%3A30%241'
   32     1      > RETURN                                                   ~0
   33     2*     > RETURN                                                   null

End of function createclosure

Function %00%7Bclosure%7D%2Fin%2FAL2Hr%3A30%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AL2Hr
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1      > RETURN                                                   <true>
   32     2*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FAL2Hr%3A30%241

Class IteratorWithFilter:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AL2Hr
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/AL2Hr
function name:  filter
number of ops:  23
compiled vars:  !0 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        CLONE                                            ~1      !0
          2        ASSIGN                                                   !0, ~1
   15     3        INIT_METHOD_CALL                                         !0, 'bindTo'
          4        FETCH_THIS                                       $3      
          5        SEND_VAR_EX                                              $3
          6        DO_FCALL                                      0  $4      
          7        ASSIGN                                                   !0, $4
   17     8        NEW                                              $7      'CallbackFilterIterator'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $8      'generator'
         11        SEND_FUNC_ARG                                            $8
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FAL2Hr%3A17%240'
         13        SEND_VAL_EX                                              ~9
         14        DO_FCALL                                      0          
         15        ASSIGN_OBJ                                               'generator'
         16        OP_DATA                                                  $7
   18    17        FETCH_OBJ_W                                      $11     'generator'
         18        ASSIGN_OBJ                                               $11, 'cl'
         19        OP_DATA                                                  !0
   20    20        FETCH_THIS                                       ~13     
         21      > RETURN                                                   ~13
   21    22*     > RETURN                                                   null

End of function filter

End of class IteratorWithFilter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.46 ms | 1407 KiB | 19 Q