Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow using more than 8G of memory in psalter #9805

Merged
merged 2 commits into from May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 1 addition & 26 deletions src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -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;
Expand Down Expand Up @@ -189,7 +187,7 @@ public static function run(array $argv): void

self::validateCliArguments($args);

self::setMemoryLimit($options);
CliUtils::setMemoryLimit($options);

self::syncShortOptions($options);

Expand Down Expand Up @@ -462,29 +460,6 @@ static function (string $arg): void {
);
}

/**
* @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
*/
Expand Down
15 changes: 3 additions & 12 deletions src/Psalm/Internal/Cli/Psalter.php
Expand Up @@ -43,7 +43,6 @@
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;
Expand Down Expand Up @@ -89,6 +88,7 @@ final class Psalter
'add-newline-between-docblock-annotations:',
'no-cache',
'no-progress',
'memory-limit:',
];

/** @param array<int,string> $argv */
Expand All @@ -100,15 +100,15 @@ public static function run(array $argv): void

ErrorHandler::install($argv);

self::setMemoryLimit();

$args = array_slice($argv, 1);

// get options from command line
$options = getopt(implode('', self::SHORT_OPTIONS), self::LONG_OPTIONS);

self::validateCliArguments($args);

CliUtils::setMemoryLimit($options);

self::syncShortOptions($options);

if (isset($options['c']) && is_array($options['c'])) {
Expand Down Expand Up @@ -442,15 +442,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<int,string> $args */
private static function validateCliArguments(array $args): void
{
Expand Down
48 changes: 18 additions & 30 deletions src/Psalm/Internal/CliUtils.php
Expand Up @@ -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;
Expand All @@ -27,21 +27,20 @@
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;
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;
Expand Down Expand Up @@ -446,38 +445,27 @@ public static function getPathToConfig(array $options): ?string
}

/**
* @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 (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
Expand Down
54 changes: 0 additions & 54 deletions tests/CommandFunctions/GetMemoryLimitInBytesTest.php

This file was deleted.