Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save staabm/20c082674ed880c8fc097b4775f2d7a7 to your computer and use it in GitHub Desktop.

Select an option

Save staabm/20c082674ed880c8fc097b4775f2d7a7 to your computer and use it in GitHub Desktop.
closure_from_callable_runtime_cache_bench.php
<?php
// Measures repeated fake-closure creation over a method that has not run yet.
// The affected path is Closure::fromCallable()/first-class callable generation.
final class CacheHeavyCallable
{
public $p1 = 1;
public $p2 = 2;
public $p3 = 3;
public $p4 = 4;
public $p5 = 5;
public $p6 = 6;
public $p7 = 7;
public $p8 = 8;
public $p9 = 9;
public $p10 = 10;
public $p11 = 11;
public $p12 = 12;
public $p13 = 13;
public $p14 = 14;
public $p15 = 15;
public $p16 = 16;
public $p17 = 17;
public $p18 = 18;
public $p19 = 19;
public $p20 = 20;
public $p21 = 21;
public $p22 = 22;
public $p23 = 23;
public $p24 = 24;
public function target()
{
return $this->p1 + $this->p2 + $this->p3 + $this->p4 + $this->p5 + $this->p6
+ $this->p7 + $this->p8 + $this->p9 + $this->p10 + $this->p11 + $this->p12
+ $this->p13 + $this->p14 + $this->p15 + $this->p16 + $this->p17 + $this->p18
+ $this->p19 + $this->p20 + $this->p21 + $this->p22 + $this->p23 + $this->p24;
}
}
function bench(string $label, Closure $factory, int $iterations): void
{
gc_collect_cycles();
$start = hrtime(true);
for ($i = 0; $i < $iterations; ++$i) {
$closure = $factory();
}
$elapsed_ms = (hrtime(true) - $start) / 1e6;
printf("%s: %.3f ms\n", $label, $elapsed_ms);
}
$iterations = (int) ($argv[1] ?? 2000000);
$target = new CacheHeavyCallable();
gc_disable();
// Leave the target method uncalled; the regression is in the closure creation path.
bench(
'Closure::fromCallable([$obj, "target"])',
static function () use ($target): Closure {
return Closure::fromCallable([$target, 'target']);
},
$iterations
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment