Skip to content

Commit ca63857

Browse files
SanderMullerclaude
andcommitted
perf: replay cached dry-run diffs for unchanged files
Files with a pending diff are never marked clean in dry-run mode, the diff must keep being reported, so every warm dry-run reprocessed them from scratch. On a 4,400-file project with 37 pending diffs that was ~11s per run. Cache the FileDiff with the file's own hash plus one hash per captured dependency; when all still match, replay the cached diff instead of reprocessing, skipping scope resolution entirely. Dry-run only: write mode always computes fresh. --no-diffs results never cross into normal entries, and the original hasChanged flag is replayed, as a rule can report line changes while printing identical content. Warm dry-run on the same project: ~9x faster single process, ~3.5x parallel. Output stays byte-identical in every cache state. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1f5c42a commit ca63857

5 files changed

Lines changed: 286 additions & 4 deletions

File tree

‎src/Application/ApplicationFileProcessor.php‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Parser\ParserErrorsException;
99
use Rector\Application\Provider\CurrentFileProvider;
1010
use Rector\Caching\Detector\ChangedFilesDetector;
11+
use Rector\Caching\DryRunDiffCache;
1112
use Rector\Configuration\Option;
1213
use Rector\Configuration\Parameter\SimpleParameterProvider;
1314
use Rector\FileSystem\FilesFinder;
@@ -49,6 +50,7 @@ public function __construct(
4950
private readonly FileProcessor $fileProcessor,
5051
private readonly ArrayParametersMerger $arrayParametersMerger,
5152
private readonly MissConfigurationReporter $missConfigurationReporter,
53+
private readonly DryRunDiffCache $dryRunDiffCache,
5254
) {
5355
}
5456

@@ -168,16 +170,30 @@ private function processFile(File $file, Configuration $configuration): FileProc
168170
{
169171
$this->currentFileProvider->setFile($file);
170172

173+
// a selective run applies a subset of rules, so its results are not
174+
// interchangeable with full runs → bypass both caches entirely
175+
$isSelectiveRun = $configuration->getOnlyRule() !== null || $configuration->getOnlySuffix() !== null;
176+
$useDiffCache = $configuration->isDryRun() && ! $isSelectiveRun;
177+
178+
if ($useDiffCache) {
179+
$cachedFileProcessResult = $this->dryRunDiffCache->load($file, $configuration);
180+
if ($cachedFileProcessResult instanceof FileProcessResult) {
181+
return $cachedFileProcessResult;
182+
}
183+
}
184+
171185
$fileProcessResult = $this->fileProcessor->processFile($file, $configuration);
186+
$fileDiff = $fileProcessResult->getFileDiff();
172187

173188
if ($fileProcessResult->getSystemErrors() !== []) {
174189
$this->changedFilesDetector->invalidateFile($file->getFilePath());
175-
} elseif (! $configuration->isDryRun() || ! $fileProcessResult->getFileDiff() instanceof FileDiff) {
176-
// a file clean under a subset of rules is not necessarily clean under all rules,
177-
// caching it would hide its pending changes from the next full run
178-
if ($configuration->getOnlyRule() === null && $configuration->getOnlySuffix() === null) {
190+
} elseif (! $configuration->isDryRun() || ! $fileDiff instanceof FileDiff) {
191+
// a file clean under a subset of rules is not necessarily clean under all rules
192+
if (! $isSelectiveRun) {
179193
$this->changedFilesDetector->cacheFile($file->getFilePath());
180194
}
195+
} elseif ($useDiffCache) {
196+
$this->dryRunDiffCache->save($file, $configuration, $fileDiff, $fileProcessResult->hasChanged());
181197
}
182198

183199
return $fileProcessResult;

‎src/Caching/DryRunDiffCache.php‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Caching;
6+
7+
use Rector\Caching\Enum\CacheKey;
8+
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
9+
use Rector\Configuration\Parameter\SimpleParameterProvider;
10+
use Rector\Parallel\ValueObject\BridgeItem;
11+
use Rector\Util\FileHasher;
12+
use Rector\ValueObject\Application\File;
13+
use Rector\ValueObject\Configuration;
14+
use Rector\ValueObject\FileProcessResult;
15+
use Rector\ValueObject\Reporting\FileDiff;
16+
17+
/**
18+
* Caches dry-run FileDiffs. Files with a pending diff are never marked clean, as a
19+
* dry-run must keep reporting them, so they are fully reprocessed on every run. When
20+
* the file and all its captured dependencies are unchanged, the cached diff is
21+
* replayed instead, skipping the whole pipeline including PHPStan scope resolution.
22+
*
23+
* @see \Rector\Tests\Caching\DryRunDiffCacheTest
24+
*/
25+
final readonly class DryRunDiffCache
26+
{
27+
public function __construct(
28+
private Cache $cache,
29+
private FileHasher $fileHasher,
30+
private FileDependencyCollector $fileDependencyCollector,
31+
) {
32+
}
33+
34+
public function load(File $file, Configuration $configuration): ?FileProcessResult
35+
{
36+
$cached = $this->cache->load($this->key($file), CacheKey::FILE_DIFF_KEY);
37+
if (! is_array($cached)) {
38+
return null;
39+
}
40+
41+
// own content + config must match
42+
if (($cached['hash'] ?? null) !== $this->contentHash($file, $configuration)) {
43+
return null;
44+
}
45+
46+
// every dependency captured last run must still hash to the same value
47+
$cachedDependencyHashes = $cached['deps'] ?? null;
48+
if (! is_array($cachedDependencyHashes)) {
49+
return null;
50+
}
51+
52+
if ($this->fileDependencyCollector->hasAnyChangedDependency($cachedDependencyHashes)) {
53+
return null;
54+
}
55+
56+
$diffJson = $cached['diff'] ?? null;
57+
if (! is_array($diffJson)) {
58+
return null;
59+
}
60+
61+
// a rule can report line changes while printing identical content, so a diff
62+
// does not imply a changed file → replay the original flag or warm runs
63+
// would report phantom changed files
64+
$hasChanged = $cached['changed'] ?? null;
65+
if (! is_bool($hasChanged)) {
66+
return null;
67+
}
68+
69+
return new FileProcessResult([], FileDiff::decode($diffJson), $hasChanged);
70+
}
71+
72+
public function save(File $file, Configuration $configuration, FileDiff $fileDiff, bool $hasChanged): void
73+
{
74+
// a failed capture means a possibly incomplete set, skip caching so the file is reprocessed
75+
$dependencyHashes = $this->fileDependencyCollector->getDependencyFileHashes($file->getFilePath());
76+
if ($dependencyHashes === null) {
77+
return;
78+
}
79+
80+
$diffJson = $fileDiff->jsonSerialize();
81+
// decompose objects to plain arrays so the var_export-based file cache can round-trip them
82+
$diffJson[BridgeItem::RECTORS_WITH_LINE_CHANGES] = array_map(
83+
static fn (RectorWithLineChange $rectorWithLineChange): array => $rectorWithLineChange->jsonSerialize(),
84+
$diffJson[BridgeItem::RECTORS_WITH_LINE_CHANGES]
85+
);
86+
87+
$this->cache->save($this->key($file), CacheKey::FILE_DIFF_KEY, [
88+
'hash' => $this->contentHash($file, $configuration),
89+
'deps' => $dependencyHashes,
90+
'diff' => $diffJson,
91+
'changed' => $hasChanged,
92+
]);
93+
}
94+
95+
private function key(File $file): string
96+
{
97+
return 'diff_' . $this->fileHasher->hash($file->getFilePath());
98+
}
99+
100+
private function contentHash(File $file, Configuration $configuration): string
101+
{
102+
// --no-diffs changes the produced FileDiff content but is not part of the
103+
// parameter hash, include it so entries do not cross-replay
104+
return $this->fileHasher->hash($file->getOriginalFileContent())
105+
. '_' . SimpleParameterProvider::hash()
106+
. ($configuration->shouldShowDiffs() ? '' : '_no-diffs');
107+
}
108+
}

‎src/Caching/Enum/CacheKey.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ final class CacheKey
1212
public const string CONFIGURATION_HASH_KEY = 'configuration_hash';
1313

1414
public const string FILE_HASH_KEY = 'file_hash';
15+
16+
public const string FILE_DIFF_KEY = 'file_diff';
1517
}

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Rector\Caching\CacheFactory;
4040
use Rector\Caching\Config\FileHashComputer;
4141
use Rector\Caching\Contract\CacheMetaExtensionInterface;
42+
use Rector\Caching\DryRunDiffCache;
4243
use Rector\Caching\FileDependencyCollector;
4344
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
4445
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
@@ -456,6 +457,7 @@ public function create(): RectorConfig
456457
$rectorConfig->singleton(FileProcessor::class);
457458
$rectorConfig->singleton(PostFileProcessor::class);
458459
$rectorConfig->singleton(FileDependencyCollector::class);
460+
$rectorConfig->singleton(DryRunDiffCache::class);
459461

460462
$rectorConfig->when(RectorNodeTraverser::class)
461463
->needs('$rectors')
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Caching;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\Caching\Cache;
9+
use Rector\Caching\DryRunDiffCache;
10+
use Rector\Caching\FileDependencyCollector;
11+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
12+
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
13+
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
14+
use Rector\Util\FileHasher;
15+
use Rector\ValueObject\Application\File;
16+
use Rector\ValueObject\Configuration;
17+
use Rector\ValueObject\FileProcessResult;
18+
use Rector\ValueObject\Reporting\FileDiff;
19+
use Symfony\Component\Filesystem\Filesystem;
20+
21+
final class DryRunDiffCacheTest extends TestCase
22+
{
23+
private string $cacheDirectory;
24+
25+
private string $sourceFilePath;
26+
27+
private string $dependencyFilePath;
28+
29+
private FileDependencyCollector $fileDependencyCollector;
30+
31+
private DryRunDiffCache $dryRunDiffCache;
32+
33+
protected function setUp(): void
34+
{
35+
$this->cacheDirectory = sys_get_temp_dir() . '/rector_dry_run_diff_cache_test_' . getmypid();
36+
37+
$this->sourceFilePath = $this->cacheDirectory . '/Source.php';
38+
$this->dependencyFilePath = $this->cacheDirectory . '/Dependency.php';
39+
40+
$filesystem = new Filesystem();
41+
$filesystem->mkdir($this->cacheDirectory);
42+
$filesystem->dumpFile($this->sourceFilePath, "<?php\n\nclass Source\n{\n}\n");
43+
$filesystem->dumpFile($this->dependencyFilePath, "<?php\n\nclass Dependency\n{\n}\n");
44+
45+
$fileHasher = new FileHasher();
46+
$this->fileDependencyCollector = new FileDependencyCollector($fileHasher);
47+
48+
$this->dryRunDiffCache = new DryRunDiffCache(
49+
new Cache(new FileCacheStorage($this->cacheDirectory, $filesystem)),
50+
$fileHasher,
51+
$this->fileDependencyCollector
52+
);
53+
}
54+
55+
protected function tearDown(): void
56+
{
57+
(new Filesystem())->remove($this->cacheDirectory);
58+
}
59+
60+
public function testSaveLoadRoundTrip(): void
61+
{
62+
$file = $this->createFile();
63+
$configuration = new Configuration(isDryRun: true);
64+
65+
$this->assertNull($this->dryRunDiffCache->load($file, $configuration));
66+
67+
$this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath);
68+
$this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true);
69+
70+
$cachedFileProcessResult = $this->dryRunDiffCache->load($file, $configuration);
71+
$this->assertInstanceOf(FileProcessResult::class, $cachedFileProcessResult);
72+
$this->assertTrue($cachedFileProcessResult->hasChanged());
73+
74+
$loadedFileDiff = $cachedFileProcessResult->getFileDiff();
75+
$this->assertInstanceOf(FileDiff::class, $loadedFileDiff);
76+
$this->assertSame('some diff', $loadedFileDiff->getDiff());
77+
$this->assertSame([RemoveUnusedPrivateMethodRector::class], $loadedFileDiff->getRectorClasses());
78+
}
79+
80+
public function testReplayKeepsUnchangedFlag(): void
81+
{
82+
$file = $this->createFile();
83+
$configuration = new Configuration(isDryRun: true);
84+
85+
// a rule can report line changes while the printed content stays identical
86+
$this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath);
87+
$this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), false);
88+
89+
$cachedFileProcessResult = $this->dryRunDiffCache->load($file, $configuration);
90+
$this->assertInstanceOf(FileProcessResult::class, $cachedFileProcessResult);
91+
$this->assertFalse($cachedFileProcessResult->hasChanged());
92+
}
93+
94+
public function testChangedOwnContentInvalidates(): void
95+
{
96+
$file = $this->createFile();
97+
$configuration = new Configuration(isDryRun: true);
98+
$this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true);
99+
100+
$changedFile = new File($this->sourceFilePath, "<?php\n\nclass Source\n{\n public int \$added;\n}\n");
101+
$this->assertNull($this->dryRunDiffCache->load($changedFile, $configuration));
102+
}
103+
104+
public function testChangedDependencyInvalidates(): void
105+
{
106+
$file = $this->createFile();
107+
$configuration = new Configuration(isDryRun: true);
108+
109+
$this->fileDependencyCollector->record($this->sourceFilePath, $this->dependencyFilePath);
110+
$this->dryRunDiffCache->save($file, $configuration, $this->createFileDiff(), true);
111+
$this->assertInstanceOf(FileProcessResult::class, $this->dryRunDiffCache->load($file, $configuration));
112+
113+
(new Filesystem())->dumpFile(
114+
$this->dependencyFilePath,
115+
"<?php\n\nclass Dependency\n{\n public int \$added;\n}\n"
116+
);
117+
118+
// fresh collector = fresh process run, no memoized hashes
119+
$freshFileHasher = new FileHasher();
120+
$freshDryRunDiffCache = new DryRunDiffCache(
121+
new Cache(new FileCacheStorage($this->cacheDirectory, new Filesystem())),
122+
$freshFileHasher,
123+
new FileDependencyCollector($freshFileHasher)
124+
);
125+
126+
$this->assertNull($freshDryRunDiffCache->load($file, $configuration));
127+
}
128+
129+
public function testNoDiffsConfigurationDoesNotCrossReplay(): void
130+
{
131+
$file = $this->createFile();
132+
133+
$this->dryRunDiffCache->save(
134+
$file,
135+
new Configuration(isDryRun: true, showDiffs: false),
136+
$this->createFileDiff(),
137+
true
138+
);
139+
140+
$this->assertNull($this->dryRunDiffCache->load($file, new Configuration(isDryRun: true, showDiffs: true)));
141+
}
142+
143+
private function createFile(): File
144+
{
145+
return new File($this->sourceFilePath, (string) file_get_contents($this->sourceFilePath));
146+
}
147+
148+
private function createFileDiff(): FileDiff
149+
{
150+
return new FileDiff($this->sourceFilePath, 'some diff', 'some diff formatted', [
151+
new RectorWithLineChange(RemoveUnusedPrivateMethodRector::class, 7),
152+
]);
153+
}
154+
}

0 commit comments

Comments
 (0)