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

allow using more than 8G of memory in psalter

This commit is contained in:
orklah 2023-05-22 23:06:02 +02:00
parent 22fd6fb9db
commit d23616cafa
4 changed files with 24 additions and 119 deletions

View File

@ -189,7 +189,7 @@ final class Psalm
self::validateCliArguments($args);
self::setMemoryLimit($options);
CliUtils::setMemoryLimit($options);
self::syncShortOptions($options);
@ -462,29 +462,6 @@ final class Psalm
);
}
/**
* @param array<string,string|false|list<mixed>> $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<int, string> $args
*/

View File

@ -4,6 +4,7 @@ namespace Psalm\Internal\Cli;
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 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;
@ -89,6 +91,7 @@ final class Psalter
'add-newline-between-docblock-annotations:',
'no-cache',
'no-progress',
'memory-limit:',
];
/** @param array<int,string> $argv */
@ -100,8 +103,6 @@ final class Psalter
ErrorHandler::install($argv);
self::setMemoryLimit();
$args = array_slice($argv, 1);
// get options from command line
@ -109,6 +110,8 @@ final class Psalter
self::validateCliArguments($args);
CliUtils::setMemoryLimit($options);
self::syncShortOptions($options);
if (isset($options['c']) && is_array($options['c'])) {
@ -442,15 +445,6 @@ final class Psalter
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<int,string> $args */
private static function validateCliArguments(array $args): void
{

View File

@ -14,8 +14,8 @@ use Psalm\Report;
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 file_put_contents;
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_get_meta_data;
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 @@ final class CliUtils
}
/**
* @psalm-pure
* @param array<string,string|false|list<mixed>> $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 (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 (array_key_exists('memory-limit', $options)) {
$memoryLimit = $options['memory-limit'];
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

View File

@ -1,54 +0,0 @@
<?php
namespace Psalm\Tests\CommandFunctions;
use Psalm\Internal\CliUtils;
use Psalm\Tests\TestCase;
class GetMemoryLimitInBytesTest extends TestCase
{
/**
* @return array<int,array<string|int>>
*/
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',
);
}
}