Skip to content

Commit

Permalink
only sort when necessary and faster hash
Browse files Browse the repository at this point in the history
* 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%
  • Loading branch information
kkmuffme committed Jun 28, 2022
1 parent 2422418 commit 2ceb16d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Psalm/Internal/Clause.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 2ceb16d

Please sign in to comment.