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 57239a7 commit 9082eab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php
Expand Up @@ -110,9 +110,9 @@ public function getLatestFromCache(
return $cached_value;
}

private function getCacheHash(?string $file_path, ?string $file_contents): string
private function getCacheHash(?string $_unused_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
7 changes: 5 additions & 2 deletions src/Psalm/Internal/Provider/FileStorageCacheProvider.php
Expand Up @@ -117,9 +117,12 @@ public function removeCacheForFile(string $file_path): void
}
}

private function getCacheHash(string $file_path, string $file_contents): string
private function getCacheHash(string $_unused_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 9082eab

Please sign in to comment.