From 3fd96bedf96d1ac9cdf2a7c522347669cb19fb16 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Tue, 28 Jun 2022 15:27:58 +0200 Subject: [PATCH] improve cache hash performance * 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% --- .../Internal/Provider/ClassLikeStorageCacheProvider.php | 2 +- src/Psalm/Internal/Provider/FileStorageCacheProvider.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index 0c79452a647..f0eaf317682 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -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); } diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 5d4a5b29b0d..418d0efbd0a 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -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); }