Skip to content

Commit

Permalink
Result cache - use file hashes instead of modified times
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 14, 2020
1 parent 2f649fa commit be3e4fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ jobs:
uses: actions/cache@v1
with:
path: ./tmp
key: "result-cache-v1"
key: "result-cache-v2"

- name: "PHPStan with result cache"
run: |
Expand Down
36 changes: 17 additions & 19 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use PHPStan\File\FileReader;
use function array_fill_keys;
use function array_key_exists;
use function filemtime;

class ResultCacheManager
{

private const CACHE_VERSION = 'v1';
private const CACHE_VERSION = 'v2-hashes';

/** @var string */
private $cacheFilePath;
Expand All @@ -32,6 +31,9 @@ class ResultCacheManager
/** @var string */
private $usedLevel;

/** @var array<string, string> */
private $fileHashes = [];

/**
* @param string $cacheFilePath
* @param string[] $allCustomConfigFiles
Expand Down Expand Up @@ -113,15 +115,12 @@ public function restore(array $allAnalysedFiles, bool $debug): ResultCache
unset($deletedFiles[$analysedFile]);

$analysedFileData = $invertedDependencies[$analysedFile];
$cachedModifiedTime = $analysedFileData['modifiedTime'];
$cachedFileHash = $analysedFileData['fileHash'];
$dependentFiles = $analysedFileData['dependentFiles'];
$invertedDependenciesToReturn[$analysedFile] = $dependentFiles;
$currentModifiedTime = filemtime($analysedFile);
if ($currentModifiedTime === false) {
$currentModifiedTime = time();
}
$currentFileHash = $this->getFileHash($analysedFile);

if ($cachedModifiedTime === $currentModifiedTime) {
if ($cachedFileHash === $currentFileHash) {
continue;
}

Expand Down Expand Up @@ -293,12 +292,8 @@ private function save(
foreach ($dependencies as $file => $fileDependencies) {
foreach ($fileDependencies as $fileDep) {
if (!array_key_exists($fileDep, $invertedDependencies)) {
$modifiedTime = filemtime($fileDep);
if ($modifiedTime === false) {
$modifiedTime = time();
}
$invertedDependencies[$fileDep] = [
'modifiedTime' => $modifiedTime,
'fileHash' => $this->getFileHash($fileDep),
'dependentFiles' => [],
];
unset($filesNoOneIsDependingOn[$fileDep]);
Expand All @@ -316,12 +311,8 @@ private function save(
continue;
}

$modifiedTime = filemtime($file);
if ($modifiedTime === false) {
$modifiedTime = time();
}
$invertedDependencies[$file] = [
'modifiedTime' => $modifiedTime,
'fileHash' => $this->getFileHash($file),
'dependentFiles' => [],
];
}
Expand Down Expand Up @@ -385,10 +376,17 @@ private function getConfigFiles(): array

private function getFileHash(string $path): string
{
if (array_key_exists($path, $this->fileHashes)) {
return $this->fileHashes[$path];
}

$contents = FileReader::read($path);
$contents = str_replace("\r\n", "\n", $contents);

return sha1($contents);
$hash = sha1($contents);
$this->fileHashes[$path] = $hash;

return $hash;
}

private function getPhpStanVersion(): string
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/ResultCacheEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function testResultCache(): void
$lexerCode = str_replace('@param string $code', '', $lexerCode);
$lexerCode = str_replace('public function startLexing($code', 'public function startLexing(\\PhpParser\\Node\\Expr\\MethodCall $code', $lexerCode);
file_put_contents($lexerPath, $lexerCode);
touch(__DIR__ . '/PHP-Parser/lib/PhpParser/ErrorHandler.php');

$errorHandlerPath = __DIR__ . '/PHP-Parser/lib/PhpParser/ErrorHandler.php';
$errorHandlerContents = FileReader::read($errorHandlerPath);
$errorHandlerContents .= "\n\n";
file_put_contents($errorHandlerPath, $errorHandlerContents);

$bootstrapPath = __DIR__ . '/PHP-Parser/lib/bootstrap.php';
$originalBootstrapContents = FileReader::read($bootstrapPath);
Expand Down

0 comments on commit be3e4fc

Please sign in to comment.