Skip to content

Commit 2999ab8

Browse files
authored
[perf] Skip ancestor reflection when no parent:: call to trim (#8707)
1 parent 1e66aba commit 2999ab8

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for RemoveArgumentFromDefaultParentCallRector.
7+
*
8+
* The rule subscribes to Class_. For every class that extends a parent it resolved the class
9+
* reflection and walked getAncestors() - native reflection over the whole ancestor chain -
10+
* before even checking whether the class body contains a parent:: call to trim. A subclass
11+
* that never calls parent:: (the common case) paid for that ancestor lookup only to find
12+
* nothing to do.
13+
*
14+
* The optimization adds a cheap AST pre-scan (hasParentStaticCall): if no method body holds a
15+
* parent:: static call, the rule bails before resolveClassReflection()/getAncestors().
16+
*
17+
* This bench parses a file of many subclasses that extend a parent but never call parent::,
18+
* attaches scope (as the real run does), then drives refactor() over every Class_ node. Those
19+
* nodes never change, so the calls are repeatable.
20+
*
21+
* Run on both branches to compare:
22+
* php bench/remove_argument_from_default_parent_call_bench.php
23+
*
24+
* Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy
25+
* under concurrent load.
26+
*/
27+
28+
use PhpParser\Node\Stmt\Class_;
29+
use Rector\DeadCode\Rector\ClassMethod\RemoveArgumentFromDefaultParentCallRector;
30+
use Rector\DependencyInjection\LazyContainerFactory;
31+
use Rector\PhpParser\Node\BetterNodeFinder;
32+
use Rector\Testing\TestingParser\TestingParser;
33+
34+
require __DIR__ . '/../vendor/autoload.php';
35+
36+
const CHILDREN = 150; // subclasses that extend a parent but never call parent::
37+
const REPEAT = 60; // outer repeats over the whole class set
38+
39+
$rectorConfig = (new LazyContainerFactory())->create();
40+
$rectorConfig->boot();
41+
42+
/** @var TestingParser $testingParser */
43+
$testingParser = $rectorConfig->make(TestingParser::class);
44+
45+
/** @var BetterNodeFinder $betterNodeFinder */
46+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
47+
48+
$rector = $rectorConfig->make(RemoveArgumentFromDefaultParentCallRector::class);
49+
50+
// generate a file: one parent with a defaulted constructor param, many children that extend it
51+
// but only call an ordinary function - no parent:: call, so the rule has nothing to trim
52+
$source = "<?php\n\nnamespace Bench;\n\n";
53+
$source .= "class SomeParent\n{\n public function __construct(array \$param = [])\n {\n }\n}\n\n";
54+
for ($i = 0; $i < CHILDREN; ++$i) {
55+
$source .= "class Child{$i} extends \\Bench\\SomeParent\n{\n";
56+
$source .= " public function __construct(string \$differentParam)\n {\n";
57+
$source .= " strlen(\$differentParam);\n";
58+
$source .= " }\n}\n\n";
59+
}
60+
61+
$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php';
62+
file_put_contents($sourceFilePath, $source);
63+
64+
$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath);
65+
66+
/** @var Class_[] $classes */
67+
$classes = $betterNodeFinder->findInstanceOf($stmts, Class_::class);
68+
69+
$cpuSeconds = static function (): float {
70+
$usage = getrusage();
71+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
72+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
73+
};
74+
75+
$run = static function () use ($rector, $classes): void {
76+
foreach ($classes as $class) {
77+
$rector->refactor($class);
78+
}
79+
};
80+
81+
// warm up autoloading/reflection caches once, excluded from timing
82+
$run();
83+
84+
$wallStart = microtime(true);
85+
$cpuStart = $cpuSeconds();
86+
87+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
88+
$run();
89+
}
90+
91+
$cpu = $cpuSeconds() - $cpuStart;
92+
$wall = microtime(true) - $wallStart;
93+
94+
@unlink($sourceFilePath);
95+
96+
$totalCalls = REPEAT * count($classes);
97+
98+
printf('classes: %d, repeats: %d, refactor() calls: %d%s', count($classes), REPEAT, $totalCalls, PHP_EOL);
99+
printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL);
100+
printf('wall: %.4f s%s', $wall, PHP_EOL);

‎rules/DeadCode/Rector/ClassMethod/RemoveArgumentFromDefaultParentCallRector.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function refactor(Node $node): ?Class_
9696
return null;
9797
}
9898

99+
// no parent:: call to trim? skip the ancestor reflection lookup
100+
if (! $this->hasParentStaticCall($node)) {
101+
return null;
102+
}
103+
99104
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
100105

101106
if (! $classReflection instanceof ClassReflection) {
@@ -204,4 +209,33 @@ public function refactor(Node $node): ?Class_
204209

205210
return $node;
206211
}
212+
213+
private function hasParentStaticCall(Class_ $class): bool
214+
{
215+
foreach ($class->getMethods() as $classMethod) {
216+
if ($classMethod->isPrivate()) {
217+
continue;
218+
}
219+
220+
if ($classMethod->isAbstract()) {
221+
continue;
222+
}
223+
224+
foreach ((array) $classMethod->stmts as $stmt) {
225+
if (! $stmt instanceof Expression) {
226+
continue;
227+
}
228+
229+
if (! $stmt->expr instanceof StaticCall) {
230+
continue;
231+
}
232+
233+
if ($this->isName($stmt->expr->class, 'parent')) {
234+
return true;
235+
}
236+
}
237+
}
238+
239+
return false;
240+
}
207241
}

0 commit comments

Comments
 (0)