Skip to content

Commit 2ad0b31

Browse files
authored
[perf] Bucket method renames by old name in RenameMethodRector (#8734)
1 parent 6809b38 commit 2ad0b31

4 files changed

Lines changed: 134 additions & 24 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for RenameMethodRector.
7+
*
8+
* The rule is configured with a list of method renames. For every method/static call node it used
9+
* to scan the whole list, running isStringName() on each entry before the expensive type/reflection
10+
* checks. With a large configured list (symfony/doctrine sets ship hundreds) this is a per-node
11+
* linear scan whose cost is pure string comparison self time.
12+
*
13+
* The change buckets the renames by lowercased old-method name at configure() time, so a node only
14+
* looks at the few renames sharing its name - one hash lookup instead of N isStringName() calls.
15+
*
16+
* This bench configures the rule with many renames, then drives refactor() over many calls whose
17+
* names are NOT in the map (the common case), isolating the scan cost from type resolution.
18+
*
19+
* Run on both branches to compare:
20+
* php bench/rename_method_bucket_bench.php
21+
*
22+
* Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy under
23+
* concurrent load.
24+
*/
25+
26+
use PhpParser\Node\Expr\MethodCall;
27+
use Rector\DependencyInjection\LazyContainerFactory;
28+
use Rector\PhpParser\Node\BetterNodeFinder;
29+
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
30+
use Rector\Renaming\ValueObject\MethodCallRename;
31+
use Rector\Testing\TestingParser\TestingParser;
32+
33+
require __DIR__ . '/../vendor/autoload.php';
34+
35+
const RENAMES = 400; // configured method renames
36+
const CALLS = 200; // method calls per pass, names not in the map
37+
const REPEAT = 200; // outer repeats over the whole 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(RenameMethodRector::class);
49+
50+
$methodCallRenames = [];
51+
for ($i = 0; $i < RENAMES; ++$i) {
52+
$methodCallRenames[] = new MethodCallRename('Bench\\SomeClass' . $i, 'oldMethod' . $i, 'newMethod' . $i);
53+
}
54+
55+
$rector->configure($methodCallRenames);
56+
57+
$source = "<?php\n\nnamespace Bench;\n\n\$value = new Other();\n";
58+
for ($i = 0; $i < CALLS; ++$i) {
59+
// names deliberately absent from the configured renames
60+
$source .= "\$value->freshCall{$i}();\n";
61+
}
62+
63+
$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php';
64+
file_put_contents($sourceFilePath, $source);
65+
66+
$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath);
67+
68+
/** @var MethodCall[] $methodCalls */
69+
$methodCalls = $betterNodeFinder->findInstanceOf($stmts, MethodCall::class);
70+
71+
$cpuSeconds = static function (): float {
72+
$usage = getrusage();
73+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
74+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
75+
};
76+
77+
$run = static function () use ($rector, $methodCalls): void {
78+
foreach ($methodCalls as $methodCall) {
79+
$rector->refactor($methodCall);
80+
}
81+
};
82+
83+
// warm up autoloading/reflection caches once, excluded from timing
84+
$run();
85+
86+
$wallStart = microtime(true);
87+
$cpuStart = $cpuSeconds();
88+
89+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
90+
$run();
91+
}
92+
93+
$cpu = $cpuSeconds() - $cpuStart;
94+
$wall = microtime(true) - $wallStart;
95+
96+
@unlink($sourceFilePath);
97+
98+
$totalCalls = REPEAT * count($methodCalls);
99+
100+
printf('renames: %d, calls: %d, repeats: %d, refactor() calls: %d%s', RENAMES, count($methodCalls), REPEAT, $totalCalls, PHP_EOL);
101+
printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL);
102+
printf('wall: %.4f s%s', $wall, PHP_EOL);

‎rules/Renaming/Rector/MethodCall/RenameMethodRector.php‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
final class RenameMethodRector extends AbstractRector implements ConfigurableRectorInterface
3737
{
3838
/**
39-
* @var MethodCallRenameInterface[]
39+
* @var array<string, MethodCallRenameInterface[]>
4040
*/
41-
private array $methodCallRenames = [];
41+
private array $methodCallRenamesByOldMethod = [];
4242

4343
public function __construct(
4444
private readonly ClassManipulator $classManipulator,
@@ -101,7 +101,12 @@ public function configure(array $configuration): void
101101
{
102102
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);
103103

104-
$this->methodCallRenames = $configuration;
104+
// bucket by old method name, so a node matches only the few renames sharing its name,
105+
// instead of scanning the whole configured list per node
106+
$this->methodCallRenamesByOldMethod = [];
107+
foreach ($configuration as $methodCallRename) {
108+
$this->methodCallRenamesByOldMethod[strtolower($methodCallRename->getOldMethod())][] = $methodCallRename;
109+
}
105110
}
106111

107112
private function shouldSkipClassMethod(
@@ -176,7 +181,7 @@ private function refactorClass(
176181
continue;
177182
}
178183

179-
foreach ($this->methodCallRenames as $methodCallRename) {
184+
foreach ($this->methodCallRenamesByOldMethod[strtolower($methodName)] ?? [] as $methodCallRename) {
180185
if ($this->shouldSkipRename(
181186
$methodName,
182187
$classMethod,
@@ -238,7 +243,7 @@ private function refactorMethodCallAndStaticCall(
238243
return null;
239244
}
240245

241-
foreach ($this->methodCallRenames as $methodCallRename) {
246+
foreach ($this->methodCallRenamesByOldMethod[strtolower($callName)] ?? [] as $methodCallRename) {
242247
if (! $this->nodeNameResolver->isStringName($callName, $methodCallRename->getOldMethod())) {
243248
continue;
244249
}

‎src/BetterPhpDocParser/PhpDocManipulator/PhpDocClassRenamer.php‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
use Rector\Enum\ClassName;
1717
use Rector\Renaming\Collector\RenamedNameCollector;
1818

19-
final readonly class PhpDocClassRenamer
19+
final class PhpDocClassRenamer
2020
{
21+
/**
22+
* @var array<string, string>
23+
*/
24+
private array $wordBoundaryPatternByOldClass = [];
25+
2126
public function __construct(
22-
private ClassAnnotationMatcher $classAnnotationMatcher,
23-
private RenamedNameCollector $renamedNameCollector
27+
private readonly ClassAnnotationMatcher $classAnnotationMatcher,
28+
private readonly RenamedNameCollector $renamedNameCollector
2429
) {
2530
}
2631

@@ -130,6 +135,12 @@ private function processSerializerTypeTagValueNode(
130135

131136
$classNameArrayItemNode = $doctrineAnnotationTagValueNode->getSilentValue();
132137

138+
// loop-invariant, resolve once instead of per configured class
139+
$currentTypeArrayItemNode = $doctrineAnnotationTagValueNode->getValue('type');
140+
$currentTypeStringNode = $currentTypeArrayItemNode instanceof ArrayItemNode
141+
? $currentTypeArrayItemNode->value
142+
: null;
143+
133144
foreach ($oldToNewClasses as $oldClass => $newClass) {
134145
if ($classNameArrayItemNode instanceof ArrayItemNode && $classNameArrayItemNode->value instanceof StringNode) {
135146
$classNameStringNode = $classNameArrayItemNode->value;
@@ -142,7 +153,7 @@ private function processSerializerTypeTagValueNode(
142153
$this->renamedNameCollector->add($oldClass);
143154
$classNameStringNode->value = Strings::replace(
144155
$classNameStringNode->value,
145-
'#\b' . preg_quote($oldClass, '#') . '\b#',
156+
$this->wordBoundaryPatternByOldClass[$oldClass] ??= '#\b' . preg_quote($oldClass, '#') . '\b#',
146157
$newClass
147158
);
148159

@@ -151,17 +162,7 @@ private function processSerializerTypeTagValueNode(
151162
$hasChanged = true;
152163
}
153164

154-
$currentTypeArrayItemNode = $doctrineAnnotationTagValueNode->getValue('type');
155-
if (! $currentTypeArrayItemNode instanceof ArrayItemNode) {
156-
continue;
157-
}
158-
159-
$currentTypeStringNode = $currentTypeArrayItemNode->value;
160-
if (! $currentTypeStringNode instanceof StringNode) {
161-
continue;
162-
}
163-
164-
if ($currentTypeStringNode->value === $oldClass) {
165+
if ($currentTypeStringNode instanceof StringNode && $currentTypeStringNode->value === $oldClass) {
165166
$currentTypeStringNode->value = $newClass;
166167

167168
$hasChanged = true;

‎src/NodeTypeResolver/PhpDoc/NodeAnalyzer/DocBlockClassRenamer.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212

1313
final readonly class DocBlockClassRenamer
1414
{
15+
private PhpDocNodeTraverser $phpDocNodeTraverser;
16+
1517
public function __construct(
1618
private ClassRenamePhpDocNodeVisitor $classRenamePhpDocNodeVisitor,
1719
) {
20+
// the traverser + visitor are reusable across calls, so build them once
21+
$this->phpDocNodeTraverser = new PhpDocNodeTraverser();
22+
$this->phpDocNodeTraverser->addPhpDocNodeVisitor($this->classRenamePhpDocNodeVisitor);
1823
}
1924

2025
/**
@@ -26,14 +31,11 @@ public function renamePhpDocType(PhpDocInfo $phpDocInfo, array $oldToNewTypes, N
2631
return false;
2732
}
2833

29-
$phpDocNodeTraverser = new PhpDocNodeTraverser();
30-
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->classRenamePhpDocNodeVisitor);
31-
3234
$this->classRenamePhpDocNodeVisitor->setCurrentPhpNode($currentPhpNode);
3335

3436
$this->classRenamePhpDocNodeVisitor->setOldToNewTypes($oldToNewTypes);
3537

36-
$phpDocNodeTraverser->traverse($phpDocInfo->getPhpDocNode());
38+
$this->phpDocNodeTraverser->traverse($phpDocInfo->getPhpDocNode());
3739

3840
return $this->classRenamePhpDocNodeVisitor->hasChanged();
3941
}

0 commit comments

Comments
 (0)