Skip to content

Commit

Permalink
Merge pull request #9805 from orklah/memory-psalter
Browse files Browse the repository at this point in the history
allow using more than 8G of memory in psalter
  • Loading branch information
orklah authored May 22, 2023
2 parents cb34901 + f65d53f commit f90118c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 122 deletions.
27 changes: 1 addition & 26 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.

0 comments on commit f90118c

Please sign in to comment.