|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Benchmark for NodeComparator::areNodesEqual() short-circuits. |
| 7 | + * |
| 8 | + * areNodesEqual() is called heavily on the traversal hot path, most often on pairs that are not |
| 9 | + * equal. Previously every single-node pair paid a double printWithoutComments() round-trip |
| 10 | + * (comment removal + printer) even when the answer was trivially false. The change adds two cheap |
| 11 | + * bail-outs before the print: a null operand, and two single nodes of a different class. |
| 12 | + * |
| 13 | + * This bench runs a realistic blend: null pairs and different-class pairs (the cases the |
| 14 | + * short-circuit now skips) mixed with same-class equal/unequal pairs (unchanged - still printed). |
| 15 | + * |
| 16 | + * Run on both branches to compare: |
| 17 | + * php bench/node_comparator_short_circuit_bench.php |
| 18 | + * |
| 19 | + * Reports CPU time (user+sys); CPU is the reliable metric, wall is noisy under concurrent load. |
| 20 | + */ |
| 21 | + |
| 22 | +use PhpParser\Node\Expr\Variable; |
| 23 | +use PhpParser\Node\Identifier; |
| 24 | +use PhpParser\Node\Scalar\LNumber; |
| 25 | +use PhpParser\Node\Scalar\String_; |
| 26 | +use Rector\DependencyInjection\LazyContainerFactory; |
| 27 | +use Rector\PhpParser\Comparing\NodeComparator; |
| 28 | + |
| 29 | +require __DIR__ . '/../vendor/autoload.php'; |
| 30 | + |
| 31 | +const REPEAT = 200000; |
| 32 | + |
| 33 | +$rectorConfig = (new LazyContainerFactory())->create(); |
| 34 | + |
| 35 | +/** @var NodeComparator $nodeComparator */ |
| 36 | +$nodeComparator = $rectorConfig->make(NodeComparator::class); |
| 37 | + |
| 38 | +// realistic blend of pairs; most are unequal, the common hot-path case |
| 39 | +$pairs = [ |
| 40 | + // null operand - newly short-circuited |
| 41 | + [new Variable('value'), null], |
| 42 | + [null, new String_('name')], |
| 43 | + // different class single nodes - newly short-circuited |
| 44 | + [new Variable('value'), new String_('value')], |
| 45 | + [new Identifier('foo'), new LNumber(1)], |
| 46 | + [new String_('a'), new Variable('a')], |
| 47 | + // same class, unequal - unchanged (still printed) |
| 48 | + [new Variable('first'), new Variable('second')], |
| 49 | + [new String_('alpha'), new String_('beta')], |
| 50 | + // same class, equal - unchanged (still printed) |
| 51 | + [new Variable('same'), new Variable('same')], |
| 52 | + [new Identifier('id'), new Identifier('id')], |
| 53 | +]; |
| 54 | + |
| 55 | +$cpuSeconds = static function (): float { |
| 56 | + $usage = getrusage(); |
| 57 | + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 |
| 58 | + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
| 59 | +}; |
| 60 | + |
| 61 | +$run = static function () use ($nodeComparator, $pairs): void { |
| 62 | + foreach ($pairs as [$first, $second]) { |
| 63 | + $nodeComparator->areNodesEqual($first, $second); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +// warm up |
| 68 | +$run(); |
| 69 | + |
| 70 | +$wallStart = microtime(true); |
| 71 | +$cpuStart = $cpuSeconds(); |
| 72 | + |
| 73 | +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { |
| 74 | + $run(); |
| 75 | +} |
| 76 | + |
| 77 | +$cpu = $cpuSeconds() - $cpuStart; |
| 78 | +$wall = microtime(true) - $wallStart; |
| 79 | + |
| 80 | +$totalCalls = REPEAT * count($pairs); |
| 81 | + |
| 82 | +printf('pairs: %d, repeats: %d, calls: %d%s', count($pairs), REPEAT, $totalCalls, PHP_EOL); |
| 83 | +printf('CPU: %.4f s (%.4f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
| 84 | +printf('wall: %.4f s%s', $wall, PHP_EOL); |
0 commit comments