From d23616cafa4f34ef61afa77066d3b41f2227a51d Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 22 May 2023 23:06:02 +0200 Subject: [PATCH 1/2] allow using more than 8G of memory in psalter --- src/Psalm/Internal/Cli/Psalm.php | 25 +-------- src/Psalm/Internal/Cli/Psalter.php | 16 ++---- src/Psalm/Internal/CliUtils.php | 48 +++++++---------- .../GetMemoryLimitInBytesTest.php | 54 ------------------- 4 files changed, 24 insertions(+), 119 deletions(-) delete mode 100644 tests/CommandFunctions/GetMemoryLimitInBytesTest.php diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 8c7df6c3a64..46d41d7deb9 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -189,7 +189,7 @@ public static function run(array $argv): void self::validateCliArguments($args); - self::setMemoryLimit($options); + CliUtils::setMemoryLimit($options); self::syncShortOptions($options); @@ -462,29 +462,6 @@ static function (string $arg): void { ); } - /** - * @param array> $options - */ - private static function setMemoryLimit(array $options): void - { - if (!array_key_exists('use-ini-defaults', $options)) { - ini_set('display_errors', 'stderr'); - ini_set('display_startup_errors', '1'); - - $memoryLimit = (8 * 1_024 * 1_024 * 1_024); - - if (array_key_exists('memory-limit', $options)) { - $memoryLimit = $options['memory-limit']; - - if (!is_scalar($memoryLimit)) { - throw new ConfigException('Invalid memory limit specified.'); - } - } - - ini_set('memory_limit', (string) $memoryLimit); - } - } - /** * @param array $args */ diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index 6d5dfa0cf50..f20a32b18fe 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -4,6 +4,7 @@ use AssertionError; use Psalm\Config; +use Psalm\Exception\ConfigException; use Psalm\Exception\UnsupportedIssueToFixException; use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\CliUtils; @@ -47,6 +48,7 @@ use function is_array; use function is_dir; use function is_numeric; +use function is_scalar; use function is_string; use function microtime; use function pathinfo; @@ -89,6 +91,7 @@ final class Psalter 'add-newline-between-docblock-annotations:', 'no-cache', 'no-progress', + 'memory-limit:', ]; /** @param array $argv */ @@ -100,8 +103,6 @@ public static function run(array $argv): void ErrorHandler::install($argv); - self::setMemoryLimit(); - $args = array_slice($argv, 1); // get options from command line @@ -109,6 +110,8 @@ public static function run(array $argv): void self::validateCliArguments($args); + CliUtils::setMemoryLimit($options); + self::syncShortOptions($options); if (isset($options['c']) && is_array($options['c'])) { @@ -442,15 +445,6 @@ public static function run(array $argv): void IssueBuffer::finish($project_analyzer, false, $start_time); } - private static function setMemoryLimit(): void - { - $memLimit = CliUtils::getMemoryLimitInBytes(); - // Magic number is 4096M in bytes - if ($memLimit > 0 && $memLimit < 8 * 1_024 * 1_024 * 1_024) { - ini_set('memory_limit', (string) (8 * 1_024 * 1_024 * 1_024)); - } - } - /** @param array $args */ private static function validateCliArguments(array $args): void { diff --git a/src/Psalm/Internal/CliUtils.php b/src/Psalm/Internal/CliUtils.php index fc1f979feb6..1e5a1abdd6e 100644 --- a/src/Psalm/Internal/CliUtils.php +++ b/src/Psalm/Internal/CliUtils.php @@ -14,8 +14,8 @@ use RuntimeException; use function array_filter; +use function array_key_exists; use function array_slice; -use function assert; use function count; use function define; use function dirname; @@ -27,13 +27,13 @@ use function fwrite; use function implode; use function in_array; -use function ini_get; +use function ini_set; use function is_array; use function is_dir; +use function is_scalar; use function is_string; use function json_decode; use function preg_last_error_msg; -use function preg_match; use function preg_replace; use function preg_split; use function realpath; @@ -41,7 +41,6 @@ use function stream_set_blocking; use function strlen; use function strpos; -use function strtoupper; use function substr; use function substr_replace; use function trim; @@ -446,38 +445,27 @@ public static function getPathToConfig(array $options): ?string } /** - * @psalm-pure + * @param array> $options + * @throws ConfigException */ - public static function getMemoryLimitInBytes(): int + public static function setMemoryLimit(array $options): void { - return self::convertMemoryLimitToBytes(ini_get('memory_limit')); - } + if (!array_key_exists('use-ini-defaults', $options)) { + ini_set('display_errors', 'stderr'); + ini_set('display_startup_errors', '1'); - /** @psalm-pure */ - public static function convertMemoryLimitToBytes(string $limit): int - { - // for unlimited = -1 - if ($limit < 0) { - return -1; - } + $memoryLimit = (8 * 1_024 * 1_024 * 1_024); + + if (array_key_exists('memory-limit', $options)) { + $memoryLimit = $options['memory-limit']; - if (preg_match('/^(\d+)(\D?)$/', $limit, $matches)) { - assert(isset($matches[1])); - $limit = (int)$matches[1]; - switch (strtoupper($matches[2] ?? '')) { - case 'G': - $limit *= 1_024 * 1_024 * 1_024; - break; - case 'M': - $limit *= 1_024 * 1_024; - break; - case 'K': - $limit *= 1_024; - break; + if (!is_scalar($memoryLimit)) { + throw new ConfigException('Invalid memory limit specified.'); + } } - } - return (int)$limit; + ini_set('memory_limit', (string) $memoryLimit); + } } public static function initPhpVersion(array $options, Config $config, ProjectAnalyzer $project_analyzer): void diff --git a/tests/CommandFunctions/GetMemoryLimitInBytesTest.php b/tests/CommandFunctions/GetMemoryLimitInBytesTest.php deleted file mode 100644 index fee4bae2d21..00000000000 --- a/tests/CommandFunctions/GetMemoryLimitInBytesTest.php +++ /dev/null @@ -1,54 +0,0 @@ -> - */ - public function memoryLimitSettingProvider(): array - { - return [ - // unlimited - [-1, -1], - // byte values - [1, 1], - [512, 512], - [2_048, 2_048], - // uppercase units - ['1K', 1_024], - ['24K', 24_576], - ['1M', 1_048_576], - ['24M', 25_165_824], - ['1G', 1_073_741_824], - ['24G', 25_769_803_776], - // lowercase units - ['1k', 1_024], - ['24k', 24_576], - ['1m', 1_048_576], - ['24m', 25_165_824], - ['1g', 1_073_741_824], - ['24g', 25_769_803_776], - ]; - } - - /** - * @dataProvider memoryLimitSettingProvider - * @param int|string $setting - * @param int|string $expectedBytes - */ - public function testGetMemoryLimitInBytes( - $setting, - $expectedBytes - ): void { - $this->assertSame( - $expectedBytes, - CliUtils::convertMemoryLimitToBytes((string)$setting), - 'Memory limit in bytes does not fit setting', - ); - } -} From f65d53f32e4fa03e2fd3816a501857ab65e6ac37 Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 22 May 2023 23:16:17 +0200 Subject: [PATCH 2/2] fix CS --- src/Psalm/Internal/Cli/Psalm.php | 2 -- src/Psalm/Internal/Cli/Psalter.php | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 46d41d7deb9..7401cd1e621 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -57,10 +57,8 @@ use function implode; use function in_array; use function ini_get; -use function ini_set; use function is_array; use function is_numeric; -use function is_scalar; use function is_string; use function json_encode; use function max; diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index f20a32b18fe..9dd8eaf47d0 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -4,7 +4,6 @@ use AssertionError; use Psalm\Config; -use Psalm\Exception\ConfigException; use Psalm\Exception\UnsupportedIssueToFixException; use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\CliUtils; @@ -44,11 +43,9 @@ use function getopt; use function implode; use function in_array; -use function ini_set; use function is_array; use function is_dir; use function is_numeric; -use function is_scalar; use function is_string; use function microtime; use function pathinfo;