|
17 | 17 | * attaches scope (as the real run does), then drives refresh() over every class node - the exact |
18 | 18 | * path RectorRunner takes after a rule returns a changed Class_. |
19 | 19 | * |
| 20 | + * By default the classes are refreshed with the same content on every pass, as when a rule keeps |
| 21 | + * visiting a class it does not change further. With "changed", every refresh toggles an extra |
| 22 | + * constant first, so each refresh sees a class content different from the former one. |
| 23 | + * |
20 | 24 | * Run on both branches to compare: |
21 | 25 | * php bench/class_reflection_refresh_bench.php |
| 26 | + * php bench/class_reflection_refresh_bench.php changed |
22 | 27 | * |
23 | 28 | * Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy under |
24 | 29 | * concurrent load. |
25 | 30 | */ |
26 | 31 |
|
| 32 | +use PhpParser\Node\Const_; |
| 33 | +use PhpParser\Node\Scalar\Int_; |
27 | 34 | use PhpParser\Node\Stmt\Class_; |
| 35 | +use PhpParser\Node\Stmt\ClassConst; |
28 | 36 | use PHPStan\Analyser\MutatingScope; |
29 | 37 | use Rector\Application\ChangedNodeScopeRefresher; |
30 | 38 | use Rector\DependencyInjection\LazyContainerFactory; |
|
38 | 46 | const METHODS = 10; // methods per class |
39 | 47 | const REPEAT = 20; // outer repeats over the whole set |
40 | 48 |
|
| 49 | +$isChanged = ($argv[1] ?? '') === 'changed'; |
| 50 | + |
41 | 51 | $rectorConfig = (new LazyContainerFactory())->create(); |
42 | 52 | $rectorConfig->boot(); |
43 | 53 |
|
|
100 | 110 | + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; |
101 | 111 | }; |
102 | 112 |
|
103 | | -$run = static function () use ($changedNodeScopeRefresher, $classes, $scopes, $sourceFilePath): void { |
| 113 | +$run = static function () use ($changedNodeScopeRefresher, $classes, $scopes, $sourceFilePath, $isChanged): void { |
104 | 114 | foreach ($classes as $key => $class) { |
| 115 | + if ($isChanged) { |
| 116 | + if ($class->stmts[0] instanceof ClassConst && $class->stmts[0]->consts[0]->name->toString() === 'EXTRA') { |
| 117 | + array_shift($class->stmts); |
| 118 | + } else { |
| 119 | + // BetterReflection asserts the constant lines, so mirror the class lines as rules do for new nodes |
| 120 | + $lines = [ |
| 121 | + 'startLine' => $class->getStartLine(), |
| 122 | + 'endLine' => $class->getStartLine(), |
| 123 | + ]; |
| 124 | + array_unshift($class->stmts, new ClassConst([new Const_('EXTRA', new Int_(1), $lines)], 0, $lines)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
105 | 128 | $changedNodeScopeRefresher->refresh($class, $sourceFilePath, $scopes[$key]); |
106 | 129 | } |
107 | 130 | }; |
|
125 | 148 |
|
126 | 149 | $totalCalls = REPEAT * count($classes); |
127 | 150 |
|
| 151 | +printf('mode: %s%s', $isChanged ? 'changed' : 'same', PHP_EOL); |
128 | 152 | printf('classes: %d, methods/class: %d, repeats: %d, refresh() calls: %d%s', count($classes), METHODS + 1, REPEAT, $totalCalls, PHP_EOL); |
129 | 153 | printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); |
130 | 154 | printf('wall: %.4f s%s', $wall, PHP_EOL); |
|
0 commit comments