Skip to content

Commit

Permalink
improve cache hash performance
Browse files Browse the repository at this point in the history
* do not concatenate with timestamp as this is slow, since $file_contents may be big
* use file contents not file path for cache hash only to ensure it works if file_path not set but file_content is
* improves performance by ~5%
  • Loading branch information
kkmuffme committed Jun 28, 2022
1 parent 2ceb16d commit 3fd96be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Expand Up @@ -112,7 +112,7 @@ public function getLatestFromCache(

private function getCacheHash(?string $file_path, ?string $file_contents): string
{
$data = ($file_path ? $file_contents : '') . $this->modified_timestamps;
$data = $file_contents ? $file_contents : $this->modified_timestamps;
return PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Psalm/Internal/Provider/FileStorageCacheProvider.php
Expand Up @@ -119,7 +119,10 @@ public function removeCacheForFile(string $file_path): void

private function getCacheHash(string $file_path, string $file_contents): string
{
$data = ($file_path ? $file_contents : '') . $this->modified_timestamps;
// do not concatenate, as $file_contents can be big and performance will be bad
// the timestamp is only needed if we don't have file contents
// as same contents should give same results, independent of when file was modified
$data = $file_contents ? $file_contents : $this->modified_timestamps;
return PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
}

Expand Down

0 comments on commit 3fd96be

Please sign in to comment.