From 1b8f3377480bf5798f51683be94dca4371ef700b Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 09:49:45 +0000 Subject: [PATCH 1/6] Allow psalm to load threads from the config --- src/Psalm/Config.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 8e0f8a75c17..c80ca64de65 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -563,6 +563,9 @@ class Config */ public $internal_stubs = []; + /** @var ?int */ + public $threads; + protected function __construct() { self::$instance = $this; @@ -1222,6 +1225,10 @@ private static function fromXmlAndPaths( } } + if (isset($config_xml->threads)) { + $config->threads = (int)$config_xml->threads; + } + return $config; } From 2fdae4ae16544882f7192be16a3061d1460766c4 Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 09:50:01 +0000 Subject: [PATCH 2/6] Use config threads if lower than detected threads --- src/Psalm/Internal/Cli/Psalm.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index b512574332b..7585d7a9c14 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -249,7 +249,7 @@ public static function run(array $argv): void $options['long-progress'] = true; } - $threads = self::detectThreads($options, $in_ci); + $threads = self::useThreads($options, $in_ci, $config); self::emitMacPcreWarning($options, $threads); @@ -1337,4 +1337,14 @@ private static function getHelpText(): string HELP; } + + private static function useThreads(array $options, bool $in_ci, Config $config): int{ + $threads = self::detectThreads($options, $in_ci); + + if ($config->threads && $config->threads<$threads) { + $threads = $config->threads; + } + + return $threads; + } } From df48eabbd2d8e51f2eb197c72731c8b01792611e Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 09:50:12 +0000 Subject: [PATCH 3/6] Add new config item to docs --- docs/running_psalm/configuration.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/running_psalm/configuration.md b/docs/running_psalm/configuration.md index 077068cb6e3..3182769dae3 100644 --- a/docs/running_psalm/configuration.md +++ b/docs/running_psalm/configuration.md @@ -396,6 +396,14 @@ Whether or not to allow `require`/`include` calls in your PHP. Defaults to `true ``` Allows you to hard-code a serializer for Psalm to use when caching data. By default, Psalm uses `ext-igbinary` *if* the version is greater than or equal to 2.0.5, otherwise it defaults to PHP's built-in serializer. +#### threads +```xml + +``` +Allows you to hard-code a serializer for Psalm to use when caching data. By default, Psalm uses `ext-igbinary` *if* the version is greater than or equal to 2.0.5, otherwise it defaults to PHP's built-in serializer. + ## Project settings From 0dc4249d431caacae8a6815508aee0a10539e3d3 Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 10:09:15 +0000 Subject: [PATCH 4/6] Fix phpcs --- src/Psalm/Internal/Cli/Psalm.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 7585d7a9c14..d3c5a51c46a 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -1338,7 +1338,8 @@ private static function getHelpText(): string HELP; } - private static function useThreads(array $options, bool $in_ci, Config $config): int{ + private static function useThreads(array $options, bool $in_ci, Config $config): int + { $threads = self::detectThreads($options, $in_ci); if ($config->threads && $config->threads<$threads) { From 06aafa78ad24b644a1923da7f679f87ba53e807d Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 10:22:22 +0000 Subject: [PATCH 5/6] Wording for threads config doc --- docs/running_psalm/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/running_psalm/configuration.md b/docs/running_psalm/configuration.md index 3182769dae3..cda1078ca5c 100644 --- a/docs/running_psalm/configuration.md +++ b/docs/running_psalm/configuration.md @@ -402,7 +402,7 @@ Allows you to hard-code a serializer for Psalm to use when caching data. By defa threads="[int]" > ``` -Allows you to hard-code a serializer for Psalm to use when caching data. By default, Psalm uses `ext-igbinary` *if* the version is greater than or equal to 2.0.5, otherwise it defaults to PHP's built-in serializer. +Allows you to hard-code the number of threads Psalm will use (similar to `--threads` on the command line). This value will only be applied if it is lower than the number of threads Psalm would natively use (i.e. you cannot override other settings that force threads=1 or utilise more threads than detected on the machine) ## Project settings From 6107148fceb859f9231b63c5c345239d814e8bf6 Mon Sep 17 00:00:00 2001 From: m1ke Date: Thu, 20 Jan 2022 15:24:21 +0000 Subject: [PATCH 6/6] Alter order of precedence and upated docs to match --- docs/running_psalm/configuration.md | 3 +-- src/Psalm/Internal/Cli/Psalm.php | 17 ++++------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/docs/running_psalm/configuration.md b/docs/running_psalm/configuration.md index cda1078ca5c..198ab3b974f 100644 --- a/docs/running_psalm/configuration.md +++ b/docs/running_psalm/configuration.md @@ -402,8 +402,7 @@ Allows you to hard-code a serializer for Psalm to use when caching data. By defa threads="[int]" > ``` -Allows you to hard-code the number of threads Psalm will use (similar to `--threads` on the command line). This value will only be applied if it is lower than the number of threads Psalm would natively use (i.e. you cannot override other settings that force threads=1 or utilise more threads than detected on the machine) - +Allows you to hard-code the number of threads Psalm will use (similar to `--threads` on the command line). This value will be used in place of detecting threads from the host machine, but will be overridden by using `--threads` or `--debug` (which sets threads to 1) on the command line ## Project settings diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index d3c5a51c46a..70893dddc4a 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -249,7 +249,7 @@ public static function run(array $argv): void $options['long-progress'] = true; } - $threads = self::useThreads($options, $in_ci, $config); + $threads = self::detectThreads($options, $config, $in_ci); self::emitMacPcreWarning($options, $threads); @@ -913,12 +913,14 @@ private static function restart(array $options, Config $config, int $threads): v } } - private static function detectThreads(array $options, bool $in_ci): int + private static function detectThreads(array $options, Config $config, bool $in_ci): int { if (isset($options['threads'])) { $threads = (int)$options['threads']; } elseif (isset($options['debug']) || $in_ci) { $threads = 1; + } elseif ($config->threads) { + $threads = $config->threads; } else { $threads = max(1, ProjectAnalyzer::getCpuCount() - 1); } @@ -1337,15 +1339,4 @@ private static function getHelpText(): string HELP; } - - private static function useThreads(array $options, bool $in_ci, Config $config): int - { - $threads = self::detectThreads($options, $in_ci); - - if ($config->threads && $config->threads<$threads) { - $threads = $config->threads; - } - - return $threads; - } }