From 9082eab91572f124606a2a26e04d4d6b2f178ee6 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 | 4 ++-- src/Psalm/Internal/Provider/FileStorageCacheProvider.php | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index 0c79452a647..9581702f0ed 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -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); } diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 5d4a5b29b0d..d52b4165acf 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -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); }