Skip to content

Commit 8a4ff34

Browse files
committed
[Application] Skip format-preserving print for unchanged files
1 parent fdbb6f1 commit 8a4ff34

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for the "skip print on unchanged file" change in FileProcessor::processFile().
7+
*
8+
* Previously every file - including the majority that no rule touches - paid one full
9+
* printFormatPreserving() pass just to detect "nothing changed". The change breaks out of
10+
* the convergence loop before that print when the file is still pristine and the pass
11+
* recorded no RectorWithLineChange.
12+
*
13+
* With a bare container (no rules registered) every file is unchanged, so this isolates
14+
* exactly one skipped print per file as the only difference between the two branches.
15+
*
16+
* Run on both branches to compare:
17+
* php bench/skip_unchanged_file_print_bench.php
18+
*
19+
* Reports CPU time (user+sys); CPU is the reliable metric, wall is noisy under concurrent load.
20+
*/
21+
22+
use Rector\Application\FileProcessor;
23+
use Rector\Application\Provider\CurrentFileProvider;
24+
use Rector\Configuration\ConfigurationFactory;
25+
use Rector\DependencyInjection\LazyContainerFactory;
26+
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
27+
use Rector\ValueObject\Application\File;
28+
29+
require __DIR__ . '/../vendor/autoload.php';
30+
31+
const STATEMENTS = 300; // methods in the source file, sizes the print cost
32+
const REPEAT = 200; // files processed
33+
34+
$rectorConfig = (new LazyContainerFactory())->create();
35+
$rectorConfig->boot();
36+
37+
$fileProcessor = $rectorConfig->make(FileProcessor::class);
38+
39+
/** @var CurrentFileProvider $currentFileProvider */
40+
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
41+
42+
/** @var DynamicSourceLocatorProvider $dynamicSourceLocatorProvider */
43+
$dynamicSourceLocatorProvider = $rectorConfig->make(DynamicSourceLocatorProvider::class);
44+
45+
/** @var ConfigurationFactory $configurationFactory */
46+
$configurationFactory = $rectorConfig->make(ConfigurationFactory::class);
47+
48+
// build a sizable, already-clean file; no rule is registered so it stays unchanged
49+
$source = "<?php\n\ndeclare(strict_types=1);\n\nnamespace Bench;\n\nclass SomeClass\n{\n";
50+
for ($i = 0; $i < STATEMENTS; ++$i) {
51+
$source .= " public function method{$i}(int \$value{$i}): int\n {\n return \$value{$i} + {$i};\n }\n\n";
52+
}
53+
54+
$source .= "}\n";
55+
56+
$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php';
57+
file_put_contents($sourceFilePath, $source);
58+
59+
$dynamicSourceLocatorProvider->setFilePath($sourceFilePath);
60+
$configuration = $configurationFactory->createForTests([$sourceFilePath]);
61+
62+
$cpuSeconds = static function (): float {
63+
$usage = getrusage();
64+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
65+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
66+
};
67+
68+
$run = static function () use ($fileProcessor, $currentFileProvider, $sourceFilePath, $configuration): void {
69+
// File is single-use (hydrateStmtsAndTokens rejects a second hydrate), so make a fresh one
70+
$file = new File($sourceFilePath, file_get_contents($sourceFilePath));
71+
$currentFileProvider->setFile($file);
72+
$fileProcessor->processFile($file, $configuration);
73+
};
74+
75+
// warm up autoloading/reflection/scope caches once, excluded from timing
76+
$run();
77+
78+
$wallStart = microtime(true);
79+
$cpuStart = $cpuSeconds();
80+
81+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
82+
$run();
83+
}
84+
85+
$cpu = $cpuSeconds() - $cpuStart;
86+
$wall = microtime(true) - $wallStart;
87+
88+
@unlink($sourceFilePath);
89+
90+
printf('statements: %d, files processed: %d%s', STATEMENTS, REPEAT, PHP_EOL);
91+
printf('CPU: %.4f s (%.3f ms/file)%s', $cpu, $cpu / REPEAT * 1_000, PHP_EOL);
92+
printf('wall: %.4f s%s', $wall, PHP_EOL);

‎src/Application/FileProcessor.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function processFile(File $file, Configuration $configuration): FileProce
6262
do {
6363
$file->changeHasChanged(false);
6464

65+
// every rule and post rector records a line change when it mutates the AST
66+
$changeCountBeforePass = count($file->getRectorWithLineChanges());
67+
6568
// 1. change nodes with Rector Rules
6669
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts(), $file);
6770

@@ -71,6 +74,14 @@ public function processFile(File $file, Configuration $configuration): FileProce
7174
// 3. this is needed for new tokens added in "afterTraverse()"
7275
$file->changeNewStmts($postNewStmts);
7376

77+
// file never changed and this pass changed nothing: the print would just reproduce the
78+
// original content, so skip the expensive format-preserving print entirely.
79+
// once the file has changed we keep printing every pass, as a reprint can still
80+
// stabilize the output (e.g. drop a leftover trailing comma)
81+
if (! $fileHasChanged && count($file->getRectorWithLineChanges()) === $changeCountBeforePass) {
82+
break;
83+
}
84+
7485
// 4. print to file or string
7586
// important to detect if file has changed
7687
$this->printFile($file, $configuration, $filePath);

0 commit comments

Comments
 (0)