Skip to content

Commit 21ec2dd

Browse files
committed
[perf] Collect short names in a single AST walk in ShortNameResolver
resolveFromFile() built its short-name map with two separate full-AST traversals of the same stmts - one for class-like and Name nodes, one for docblock short names. The map is version-cached, so during a convergence loop it is rebuilt once per stmts-version bump, paying for two traversals each time. Merge both into a single traversal: the docblock short names are collected in the same callback, then fqnized and merged afterwards. Result is identical, the stmts tree is walked once instead of twice. Adds bench/short_name_resolver_traversal_bench.php, which forces a re-resolve on every call to isolate the rebuild cost.
1 parent 66a13fd commit 21ec2dd

2 files changed

Lines changed: 131 additions & 34 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for ShortNameResolver::resolveFromFile() traversal cost.
7+
*
8+
* resolveFromFile() builds the short-name map by walking the file stmts. The map is
9+
* version-cached, so during a convergence loop it is rebuilt once per stmts version bump.
10+
* This bench isolates that rebuild cost: the stmts version is bumped before every call,
11+
* so every call does the full walk (no cache hits).
12+
*
13+
* Use it to compare the single-walk vs double-walk implementations of resolveForStmts():
14+
* php bench/short_name_resolver_traversal_bench.php # on each branch
15+
*
16+
* Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy
17+
* under concurrent load.
18+
*/
19+
20+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
21+
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
22+
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
23+
use Rector\DependencyInjection\LazyContainerFactory;
24+
use Rector\NodeNameResolver\NodeNameResolver;
25+
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
26+
use Rector\PhpParser\Node\BetterNodeFinder;
27+
use Rector\Testing\TestingParser\TestingParser;
28+
use Rector\ValueObject\Application\File;
29+
30+
require __DIR__ . '/../vendor/autoload.php';
31+
32+
const REBUILDS = 200; // forced re-resolves (each one does a full walk)
33+
const REPEAT = 40; // outer repeats, each with a fresh-cache resolver
34+
35+
$rectorConfig = (new LazyContainerFactory())->create();
36+
$rectorConfig->boot();
37+
38+
/** @var TestingParser $testingParser */
39+
$testingParser = $rectorConfig->make(TestingParser::class);
40+
41+
$sourceFilePath = __DIR__ . '/Source/ManyNamesSource.php';
42+
43+
$simpleCallableNodeTraverser = $rectorConfig->make(SimpleCallableNodeTraverser::class);
44+
$nodeNameResolver = $rectorConfig->make(NodeNameResolver::class);
45+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
46+
$useImportNameMatcher = $rectorConfig->make(UseImportNameMatcher::class);
47+
$phpDocInfoFactory = $rectorConfig->make(PhpDocInfoFactory::class);
48+
49+
$makeResolver = static fn (): ShortNameResolver => new ShortNameResolver(
50+
$simpleCallableNodeTraverser,
51+
$nodeNameResolver,
52+
$betterNodeFinder,
53+
$useImportNameMatcher,
54+
$phpDocInfoFactory
55+
);
56+
57+
$cpuSeconds = static function (): float {
58+
$usage = getrusage();
59+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
60+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
61+
};
62+
63+
$run = static function () use ($makeResolver, $testingParser, $sourceFilePath): void {
64+
$resolver = $makeResolver();
65+
$file = $testingParser->parseFilePathToFile($sourceFilePath);
66+
67+
for ($rebuild = 0; $rebuild < REBUILDS; ++$rebuild) {
68+
// bump version so the next call misses the cache and does the full walk
69+
$file->changeNewStmts($file->getNewStmts());
70+
$resolver->resolveFromFile($file);
71+
}
72+
};
73+
74+
$measure = static function () use ($run, $cpuSeconds): array {
75+
// warm up autoloading / parser once, excluded from timing
76+
$run();
77+
78+
$cpuStart = $cpuSeconds();
79+
$wallStart = hrtime(true);
80+
81+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
82+
$run();
83+
}
84+
85+
return [
86+
'cpu' => $cpuSeconds() - $cpuStart,
87+
'wall' => (hrtime(true) - $wallStart) / 1_000_000_000,
88+
];
89+
};
90+
91+
assert(class_exists(File::class));
92+
93+
$result = $measure();
94+
95+
printf('ShortNameResolver::resolveFromFile traversal benchmark%s', PHP_EOL);
96+
printf(' rebuilds=%d repeat=%d%s', REBUILDS, REPEAT, PHP_EOL);
97+
printf('%s', PHP_EOL);
98+
printf(' forced re-resolve cpu=%.4fs wall=%.4fs%s', $result['cpu'], $result['wall'], PHP_EOL);

‎rules/CodingStyle/ClassNameImport/ShortNameResolver.php‎

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,17 @@ private function findShortClassLikeNames(File $file): array
118118
private function resolveForStmts(array $stmts): array
119119
{
120120
$shortNamesToFullyQualifiedNames = [];
121+
$docBlockShortNames = [];
121122

123+
// single traversal collects both PHP names and docblock short names, so the file stmts are walked once
122124
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
123-
&$shortNamesToFullyQualifiedNames
125+
&$shortNamesToFullyQualifiedNames,
126+
&$docBlockShortNames
124127
): null {
128+
foreach ($this->resolveDocBlockShortNames($node) as $docBlockShortName) {
129+
$docBlockShortNames[] = $docBlockShortName;
130+
}
131+
125132
// class name is used!
126133
if ($node instanceof ClassLike && $node->name instanceof Identifier) {
127134
$fullyQualifiedName = $this->nodeNameResolver->getName($node);
@@ -152,54 +159,46 @@ private function resolveForStmts(array $stmts): array
152159
return null;
153160
});
154161

155-
$docBlockShortNamesToFullyQualifiedNames = $this->resolveFromStmtsDocBlocks($stmts);
162+
$docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $stmts);
156163
/** @var array<string, string> $result */
157164
$result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames];
158165
return $result;
159166
}
160167

161168
/**
162-
* @param Stmt[] $stmts
163-
* @return array<string, string>
169+
* @return string[]
164170
*/
165-
private function resolveFromStmtsDocBlocks(array $stmts): array
171+
private function resolveDocBlockShortNames(Node $node): array
166172
{
167-
$shortNames = [];
168-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
169-
&$shortNames
170-
): null {
171-
// speed up for nodes that are
172-
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
173-
if (! $phpDocInfo instanceof PhpDocInfo) {
174-
return null;
175-
}
176-
177-
$phpDocNodeTraverser = new PhpDocNodeTraverser();
178-
$phpDocNodeTraverser->traverseWithCallable(
179-
$phpDocInfo->getPhpDocNode(),
180-
'',
181-
static function ($node) use (&$shortNames): null {
182-
if ($node instanceof PhpDocTagNode) {
183-
$shortName = trim($node->name, '@');
184-
if (ucfirst($shortName) === $shortName) {
185-
$shortNames[] = $shortName;
186-
}
187-
188-
return null;
189-
}
173+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
174+
if (! $phpDocInfo instanceof PhpDocInfo) {
175+
return [];
176+
}
190177

191-
if ($node instanceof IdentifierTypeNode) {
192-
$shortNames[] = $node->name;
178+
$shortNames = [];
179+
$phpDocNodeTraverser = new PhpDocNodeTraverser();
180+
$phpDocNodeTraverser->traverseWithCallable(
181+
$phpDocInfo->getPhpDocNode(),
182+
'',
183+
static function ($node) use (&$shortNames): null {
184+
if ($node instanceof PhpDocTagNode) {
185+
$shortName = trim($node->name, '@');
186+
if (ucfirst($shortName) === $shortName) {
187+
$shortNames[] = $shortName;
193188
}
194189

195190
return null;
196191
}
197-
);
198192

199-
return null;
200-
});
193+
if ($node instanceof IdentifierTypeNode) {
194+
$shortNames[] = $node->name;
195+
}
196+
197+
return null;
198+
}
199+
);
201200

202-
return $this->fqnizeShortNames($shortNames, $stmts);
201+
return $shortNames;
203202
}
204203

205204
/**

0 commit comments

Comments
 (0)