1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #8193 from kkmuffme/improve-psalm-performance

Various minor improvements to speed up by ~10%
This commit is contained in:
orklah 2022-06-29 00:29:21 +02:00 committed by GitHub
commit 4bc803dd8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 10 deletions

View File

@ -136,6 +136,7 @@ class Algebra
if (!$clause_a->reconcilable || $clause_a->wedge) { if (!$clause_a->reconcilable || $clause_a->wedge) {
continue; continue;
} }
$clause_a_keys = array_keys($clause_a->possibilities);
if (count($clause_a->possibilities) !== 1 || count(array_values($clause_a->possibilities)[0]) !== 1) { if (count($clause_a->possibilities) !== 1 || count(array_values($clause_a->possibilities)[0]) !== 1) {
foreach ($cloned_clauses as $clause_b) { foreach ($cloned_clauses as $clause_b) {
@ -143,7 +144,7 @@ class Algebra
continue; continue;
} }
if (array_keys($clause_a->possibilities) === array_keys($clause_b->possibilities)) { if ($clause_a_keys === array_keys($clause_b->possibilities)) {
$opposing_keys = []; $opposing_keys = [];
foreach ($clause_a->possibilities as $key => $a_possibilities) { foreach ($clause_a->possibilities as $key => $a_possibilities) {

View File

@ -10,15 +10,17 @@ use function array_map;
use function array_unique; use function array_unique;
use function array_values; use function array_values;
use function count; use function count;
use function hash;
use function implode; use function implode;
use function ksort; use function ksort;
use function md5;
use function reset; use function reset;
use function serialize; use function serialize;
use function sort; use function sort;
use function strpos; use function strpos;
use function substr; use function substr;
use const PHP_VERSION_ID;
/** /**
* @internal * @internal
* *
@ -106,11 +108,15 @@ class Clause
} else { } else {
ksort($possibilities); ksort($possibilities);
foreach ($possibilities as $i => $_) { foreach ($possibilities as $i => $v) {
if (count($v) < 2) {
continue;
}
sort($possibilities[$i]); sort($possibilities[$i]);
} }
$this->hash = md5(serialize($possibilities)); $data = serialize($possibilities);
$this->hash = PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
} }
} }
@ -120,8 +126,14 @@ class Clause
return false; return false;
} }
foreach ($other_clause->possibilities as $var => $_) {
if (!isset($this->possibilities[$var])) {
return false;
}
}
foreach ($other_clause->possibilities as $var => $possible_types) { foreach ($other_clause->possibilities as $var => $possible_types) {
if (!isset($this->possibilities[$var]) || count(array_diff($possible_types, $this->possibilities[$var]))) { if (count(array_diff($possible_types, $this->possibilities[$var]))) {
return false; return false;
} }
} }

View File

@ -17,6 +17,7 @@ use function hash;
use function igbinary_serialize; use function igbinary_serialize;
use function igbinary_unserialize; use function igbinary_unserialize;
use function is_dir; use function is_dir;
use function is_null;
use function mkdir; use function mkdir;
use function serialize; use function serialize;
use function strtolower; use function strtolower;
@ -75,9 +76,15 @@ class ClassLikeStorageCacheProvider
{ {
$fq_classlike_name_lc = strtolower($storage->name); $fq_classlike_name_lc = strtolower($storage->name);
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
$storage->hash = $this->getCacheHash($file_path, $file_contents); $storage->hash = $this->getCacheHash($file_path, $file_contents);
// check if we have it in cache already
$cached_value = $this->loadFromCache($fq_classlike_name_lc, $file_path);
if (!is_null($cached_value) && $cached_value->hash === $storage->hash) {
return;
}
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
if ($this->config->use_igbinary) { if ($this->config->use_igbinary) {
file_put_contents($cache_location, igbinary_serialize($storage)); file_put_contents($cache_location, igbinary_serialize($storage));
} else { } else {
@ -110,9 +117,9 @@ class ClassLikeStorageCacheProvider
return $cached_value; 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); return PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
} }

View File

@ -117,9 +117,12 @@ class FileStorageCacheProvider
} }
} }
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); return PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
} }