Skip to content

Commit f2c7ecb

Browse files
committed
perf: defer static-to-self scope lookup
1 parent 792291c commit f2c7ecb

3 files changed

Lines changed: 113 additions & 20 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpParser\Node\Stmt\Class_;
6+
use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector;
7+
use Rector\DependencyInjection\LazyContainerFactory;
8+
use Rector\PhpParser\Node\BetterNodeFinder;
9+
use Rector\Testing\TestingParser\TestingParser;
10+
11+
require __DIR__ . '/../vendor/autoload.php';
12+
13+
const CLASSES = 200;
14+
const REPEAT = 300;
15+
16+
$rectorConfig = (new LazyContainerFactory())->create();
17+
$rectorConfig->boot();
18+
19+
/** @var TestingParser $testingParser */
20+
$testingParser = $rectorConfig->make(TestingParser::class);
21+
22+
/** @var BetterNodeFinder $betterNodeFinder */
23+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
24+
25+
$rector = $rectorConfig->make(ConvertStaticToSelfRector::class);
26+
27+
$source = "<?php\n\nnamespace Bench;\n\n";
28+
for ($i = 0; $i < CLASSES; ++$i) {
29+
$source .= "final class Service{$i}\n{\n";
30+
$source .= " public function run(string \$value): string\n {\n return strtoupper(\$value);\n }\n}\n\n";
31+
}
32+
33+
$tempBase = tempnam(sys_get_temp_dir(), 'bench_');
34+
$sourceFilePath = $tempBase . '.php';
35+
file_put_contents($sourceFilePath, $source);
36+
37+
$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath);
38+
39+
/** @var Class_[] $classes */
40+
$classes = $betterNodeFinder->findInstanceOf($stmts, Class_::class);
41+
42+
$cpuSeconds = static function (): float {
43+
$usage = getrusage();
44+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
45+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
46+
};
47+
48+
$run = static function () use ($rector, $classes): void {
49+
foreach ($classes as $class) {
50+
$rector->refactor($class);
51+
}
52+
};
53+
54+
$run();
55+
56+
$wallStart = microtime(true);
57+
$cpuStart = $cpuSeconds();
58+
59+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
60+
$run();
61+
}
62+
63+
$cpu = $cpuSeconds() - $cpuStart;
64+
$wall = microtime(true) - $wallStart;
65+
66+
if (is_file($sourceFilePath)) {
67+
unlink($sourceFilePath);
68+
}
69+
70+
if (is_file($tempBase)) {
71+
unlink($tempBase);
72+
}
73+
74+
$totalCalls = REPEAT * count($classes);
75+
76+
printf('classes: %d, repeats: %d, refactor() calls: %d%s', count($classes), REPEAT, $totalCalls, PHP_EOL);
77+
printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL);
78+
printf('wall: %.4f s%s', $wall, PHP_EOL);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\ConvertStaticToSelfRector\Fixture;
4+
5+
final class WithoutStaticAccess
6+
{
7+
public function run(string $value): string
8+
{
9+
return strtoupper($value);
10+
}
11+
}

‎rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,15 @@ public function refactor(Node $node): ?Class_
7777
{
7878
$hasChanged = false;
7979
$isFinal = $node->isFinal() || FeatureFlags::treatClassesAsFinal($node);
80-
$scope = ScopeFetcher::fetch($node);
81-
$classReflection = $scope->getClassReflection();
82-
83-
if (! $classReflection instanceof ClassReflection) {
84-
return null;
85-
}
80+
$scope = null;
81+
$classReflection = null;
8682

8783
$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (
8884
&$hasChanged,
89-
$classReflection,
85+
&$classReflection,
9086
$isFinal,
91-
$scope,
87+
&$scope,
88+
$node,
9289
): ?Node {
9390
if (
9491
! $subNode instanceof StaticPropertyFetch
@@ -98,6 +95,25 @@ public function refactor(Node $node): ?Class_
9895
return null;
9996
}
10097

98+
if (! $subNode->class instanceof Name) {
99+
return null;
100+
}
101+
102+
if (! $this->isName($subNode->class, ObjectReference::STATIC)) {
103+
return null;
104+
}
105+
106+
if (! $subNode->name instanceof Identifier) {
107+
return null;
108+
}
109+
110+
$scope ??= ScopeFetcher::fetch($node);
111+
$classReflection ??= $scope->getClassReflection();
112+
113+
if (! $classReflection instanceof ClassReflection) {
114+
return null;
115+
}
116+
101117
if ($this->shouldSkip($subNode, $classReflection, $isFinal, $scope)) {
102118
return null;
103119
}
@@ -116,18 +132,6 @@ private function shouldSkip(
116132
bool $isFinal,
117133
Scope $scope,
118134
): bool {
119-
if (! $node->class instanceof Name) {
120-
return true;
121-
}
122-
123-
if (! $this->isName($node->class, ObjectReference::STATIC)) {
124-
return true;
125-
}
126-
127-
if (! $node->name instanceof Identifier) {
128-
return true;
129-
}
130-
131135
$name = (string) $this->getName($node->name);
132136

133137
// For final classes, we can safely convert all static:: to self::, even

0 commit comments

Comments
 (0)