Skip to content

Instantly share code, notes, and snippets.

@dave1010
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave1010/36a772124092769689b7 to your computer and use it in GitHub Desktop.
Save dave1010/36a772124092769689b7 to your computer and use it in GitHub Desktop.
retry

Improved retry function for igorw/retry, as proposed by @acleon.

Benefits:

  • Appears to use fewer opcodes (This is the 1st time I've used VLD, I could be wrong)
  • Doesn't use goto
<?php
namespace igorw;
require_once __DIR__ . '/src/retry.php';
retry(1, function () {});

Original sad:

function name:  igorw\retry
number of ops:  21
compiled vars:  !0 = $retries, !1 = $fn, !2 = $e
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
  21     0  >   RECV                                             !0      
         1      RECV                                             !1      
  26     2  >   INIT_FCALL_BY_NAME                                       !1
         3      DO_FCALL_BY_NAME                              0  $0      
         4    > RETURN                                                   $0
  27     5*     JMP                                                      ->20
         6  >   CATCH                                        14          'Exception', !2
  28     7      BOOL_NOT                                         ~1      !0
         8    > JMPZ                                                     ~1, ->17
  29     9  >   FETCH_CLASS                                   4  :2      'igorw%5CFailingTooHardException'
        10      NEW                                              $3      :2
        11      SEND_VAL                                                 ''
        12      SEND_VAL                                                 0
        13      SEND_VAR                                                 !2
        14      DO_FCALL_BY_NAME                              3          
        15    > THROW                                         0          $3
  30    16*     JMP                                                      ->17
  31    17  >   POST_DEC                                         ~5      !0
        18      FREE                                                     ~5
  32    19    > JMP                                                      ->2
  34    20*   > RETURN                                                   null

New sad:

function name:  igorw\retry
number of ops:  17
compiled vars:  !0 = $retries, !1 = $fn, !2 = $e
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   7     0  >   RECV                                             !0      
         1      RECV                                             !1      
  11     2  >   INIT_FCALL_BY_NAME                                       !1
         3      DO_FCALL_BY_NAME                              0  $0      
         4    > RETURN                                                   $0
  13     5*     JMP                                                      ->7
         6  >   CATCH                                         7          'Exception', !2
  15     7      POST_DEC                                         ~1      !0
         8    > JMPNZ                                                    ~1, ->2
  17     9  >   FETCH_CLASS                                   4  :2      'igorw%5CFailingTooHardException'
        10      NEW                                              $3      :2
        11      SEND_VAL                                                 ''
        12      SEND_VAL                                                 0
        13      SEND_VAR                                                 !2
        14      DO_FCALL_BY_NAME                              3          
        15    > THROW                                         0          $3
  18    16*   > RETURN                                                   null

Original happy:

function name:  igorw\retry
number of ops:  21
compiled vars:  !0 = $retries, !1 = $fn, !2 = $e
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
  21     0  >   RECV                                             !0      
         1      RECV                                             !1      
  26     2  >   INIT_FCALL_BY_NAME                                       !1
         3      DO_FCALL_BY_NAME                              0  $0      
         4    > RETURN                                                   $0
  27     5*     JMP                                                      ->20
         6  >   CATCH                                        14          'Exception', !2
  28     7      BOOL_NOT                                         ~1      !0
         8    > JMPZ                                                     ~1, ->17
  29     9  >   FETCH_CLASS                                   4  :2      'igorw%5CFailingTooHardException'
        10      NEW                                              $3      :2
        11      SEND_VAL                                                 ''
        12      SEND_VAL                                                 0
        13      SEND_VAR                                                 !2
        14      DO_FCALL_BY_NAME                              3          
        15    > THROW                                         0          $3
  30    16*     JMP                                                      ->17
  31    17  >   POST_DEC                                         ~5      !0
        18      FREE                                                     ~5
  32    19    > JMP                                                      ->2
  34    20*   > RETURN                                                   null

New happy:

function name:  igorw\retry
number of ops:  17
compiled vars:  !0 = $retries, !1 = $fn, !2 = $e
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   7     0  >   RECV                                             !0      
         1      RECV                                             !1      
  11     2  >   INIT_FCALL_BY_NAME                                       !1
         3      DO_FCALL_BY_NAME                              0  $0      
         4    > RETURN                                                   $0
  13     5*     JMP                                                      ->7
         6  >   CATCH                                         7          'Exception', !2
  15     7      POST_DEC                                         ~1      !0
         8    > JMPNZ                                                    ~1, ->2
  17     9  >   FETCH_CLASS                                   4  :2      'igorw%5CFailingTooHardException'
        10      NEW                                              $3      :2
        11      SEND_VAL                                                 ''
        12      SEND_VAL                                                 0
        13      SEND_VAR                                                 !2
        14      DO_FCALL_BY_NAME                              3          
        15    > THROW                                         0          $3
  18    16*   > RETURN                                                   null
<?php
namespace igorw;
class FailingTooHardException extends \Exception {}
// updated code, from @acleon
function retry($retries, callable $fn)
{
do {
try {
return $fn();
}
catch (\Exception $e) { }
}
while($retries--);
throw new FailingTooHardException('', 0, $e);
}
<?php
namespace igorw;
require_once __DIR__ . '/src/retry.php';
$i = 0;
$failed = false;
$value = retry(1, function () use (&$i, &$failed) {
$i++;
if (!$failed) {
$failed = true;
throw new \RuntimeException('roflcopter');
}
return 5;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment