From 2ceb16d296868dfd784da009257ddecdff5e9885 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Tue, 28 Jun 2022 19:04:09 +0200 Subject: [PATCH] only sort when necessary and faster hash * sort is much more expensive than count, so we only sort if we have something to sort * could implement for ksort too, but advantage there is minimal since we almost always have more than 1 possibility * use same hash algorithm as in other places (= faster) * reduces runtime by 2-3% --- src/Psalm/Internal/Clause.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Clause.php b/src/Psalm/Internal/Clause.php index 422cf38677e..10dd663fe23 100644 --- a/src/Psalm/Internal/Clause.php +++ b/src/Psalm/Internal/Clause.php @@ -10,15 +10,17 @@ use function array_unique; use function array_values; use function count; +use function hash; use function implode; use function ksort; -use function md5; use function reset; use function serialize; use function sort; use function strpos; use function substr; +use const PHP_VERSION_ID; + /** * @internal * @@ -106,11 +108,15 @@ public function __construct( } else { ksort($possibilities); - foreach ($possibilities as $i => $_) { + foreach ($possibilities as $i => $v) { + if (count($v) < 2) { + continue; + } sort($possibilities[$i]); } - $this->hash = md5(serialize($possibilities)); + $data = serialize($possibilities); + $this->hash = PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data); } }