Skip to content

Commit

Permalink
Cache on successful file processing (#3604)
Browse files Browse the repository at this point in the history
  • Loading branch information
yguedidi committed Apr 11, 2023
1 parent 5ea7727 commit 4c56874
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 39 deletions.
6 changes: 4 additions & 2 deletions packages-tests/Caching/Detector/ChangedFilesDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testHasFileChanged(): void
$filePath = __DIR__ . '/Source/file.php';

$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->addFileWithDependencies($filePath, []);
$this->changedFilesDetector->addFileWithDependencies($filePath);

$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
$this->changedFilesDetector->invalidateFile($filePath);
Expand All @@ -46,7 +46,9 @@ public function testHasFileChanged(): void
#[DataProvider('provideData')]
public function testGetDependentFileInfos(string $filePath, array $dependantFiles): void
{
$this->changedFilesDetector->addFileWithDependencies($filePath, $dependantFiles);
$this->changedFilesDetector->addFileDependentFiles($filePath, $dependantFiles);
$this->changedFilesDetector->addFileWithDependencies($filePath);

$dependantFilePaths = $this->changedFilesDetector->getDependentFilePaths($filePath);

$dependantFilesCount = count($dependantFiles);
Expand Down
19 changes: 17 additions & 2 deletions packages/Caching/Detector/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
final class ChangedFilesDetector
{
/**
* @var array<string, string[]>
*/
private array $dependentFiles = [];

public function __construct(
private readonly FileHashComputer $fileHashComputer,
private readonly Cache $cache
Expand All @@ -24,13 +29,23 @@ public function __construct(
/**
* @param string[] $dependentFiles
*/
public function addFileWithDependencies(string $filePath, array $dependentFiles): void
public function addFileDependentFiles(string $filePath, array $dependentFiles): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$this->dependentFiles[$filePathCacheKey] = $dependentFiles;
}

public function addFileWithDependencies(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$hash = $this->hashFile($filePath);

$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash);
$this->cache->save($filePathCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY, $dependentFiles);
$this->cache->save(
$filePathCacheKey . '_files',
CacheKey::DEPENDENT_FILES_KEY,
$this->dependentFiles[$filePathCacheKey],
);
}

public function hasFileChanged(string $filePath): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,6 @@ private function resolveAndSaveDependentFiles(
}
}

$this->changedFilesDetector->addFileWithDependencies($filePath, $dependentFiles);
$this->changedFilesDetector->addFileDependentFiles($filePath, $dependentFiles);
}
}
23 changes: 22 additions & 1 deletion packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
Expand Down Expand Up @@ -41,7 +42,8 @@ public function __construct(
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor,
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly array $fileProcessors = []
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly array $fileProcessors = [],
) {
}

Expand Down Expand Up @@ -85,14 +87,24 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
$this->applicationFileProcessor->configurePHPStanNodeScopeResolver($filePaths, $configuration);

foreach ($filePaths as $filePath) {
$file = null;

try {
$file = new File($filePath, FileSystem::read($filePath));
$this->currentFileProvider->setFile($file);

$errorAndFileDiffs = $this->processFiles($file, $configuration, $errorAndFileDiffs);

if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
$this->changedFilesDetector->invalidateFile($file->getFilePath());
} else {
$this->changedFilesDetector->addFileWithDependencies($file->getFilePath());
}
} catch (Throwable $throwable) {
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);

$this->invalidateFile($file);
}
}

Expand Down Expand Up @@ -158,4 +170,13 @@ private function collectSystemErrors(array $systemErrors, Throwable $throwable,

return $systemErrors;
}

private function invalidateFile(?File $file): void
{
if ($file === null) {
return;
}

$this->changedFilesDetector->invalidateFile($file->getFilePath());
}
}
10 changes: 9 additions & 1 deletion src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\Application;

use PHPStan\Analyser\NodeScopeResolver;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Application\FileDecorator\FileDiffFileDecorator;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Configuration\Option;
Expand Down Expand Up @@ -52,7 +53,8 @@ public function __construct(
private readonly ParameterProvider $parameterProvider,
private readonly ScheduleFactory $scheduleFactory,
private readonly CpuCoreCountProvider $cpuCoreCountProvider,
private readonly array $fileProcessors = []
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly array $fileProcessors = [],
) {
}

Expand Down Expand Up @@ -128,6 +130,12 @@ public function processFiles(array $files, Configuration $configuration): array
$systemErrorsAndFileDiffs = $this->arrayParametersMerger->merge($systemErrorsAndFileDiffs, $result);
}

if ($systemErrorsAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
$this->changedFilesDetector->invalidateFile($file->getFilePath());
} else {
$this->changedFilesDetector->addFileWithDependencies($file->getFilePath());
}

// progress bar +1
if ($shouldShowProgressBar) {
$this->rectorOutputStyle->progressAdvance();
Expand Down
19 changes: 0 additions & 19 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$processResult = $this->processResultFactory->create($systemErrorsAndFileDiffs);
$outputFormatter->report($processResult, $configuration);

// invalidate affected files
$this->invalidateCacheForChangedAndErroredFiles($processResult);

return $this->resolveReturnCode($processResult, $configuration);
}

Expand All @@ -114,22 +111,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
}
}

private function invalidateCacheForChangedAndErroredFiles(ProcessResult $processResult): void
{
foreach ($processResult->getChangedFilePaths() as $changedFilePath) {
$this->changedFilesDetector->invalidateFile($changedFilePath);
}

foreach ($processResult->getErrors() as $systemError) {
$errorFile = $systemError->getFile();
if (! is_string($errorFile)) {
continue;
}

$this->changedFilesDetector->invalidateFile($errorFile);
}
}

/**
* @return ExitCode::*
*/
Expand Down
13 changes: 0 additions & 13 deletions src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,4 @@ public function getRemovedNodeCount(): int
{
return $this->removedNodeCount;
}

/**
* @return string[]
*/
public function getChangedFilePaths(): array
{
$fileInfos = [];
foreach ($this->fileDiffs as $fileDiff) {
$fileInfos[] = $fileDiff->getRelativeFilePath();
}

return array_unique($fileInfos);
}
}

0 comments on commit 4c56874

Please sign in to comment.