Skip to content

Commit a54a556

Browse files
committed
Allow disabling array cache to reduce memory usage
1 parent c84788e commit a54a556

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/Psalm/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ final class Config
221221
*/
222222
public ?string $cache_directory = null;
223223

224+
public bool $array_cache = true;
225+
224226
private bool $cache_directory_initialized = false;
225227

226228
/**
@@ -942,6 +944,7 @@ private static function fromXmlAndPaths(
942944
$config_xml = simplexml_import_dom($dom_document);
943945

944946
$booleanAttributes = [
947+
'arrayCache' => 'array_cache',
945948
'useDocblockTypes' => 'use_docblock_types',
946949
'useDocblockPropertyTypes' => 'use_docblock_property_types',
947950
'docblockPropertyTypesSealProperties' => 'docblock_property_types_seal_properties',

src/Psalm/Internal/Cache.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ final class Cache
5858
/** @var resource */
5959
private mixed $lock;
6060

61+
private readonly bool $arrayCache;
62+
6163
public function __construct(
6264
Config $config,
6365
string $subdir,
6466
array $dependencies = [],
6567
private readonly bool $persistent = true,
6668
) {
6769
$this->serializer = $config->getCacheSerializer();
70+
$this->arrayCache = $config->array_cache;
6871
if (!$persistent) {
6972
return;
7073
}
@@ -104,7 +107,7 @@ public function __construct(
104107
flock($lock, LOCK_SH);
105108
$this->lock = $lock;
106109

107-
if (file_exists($this->dir.'consolidated')) {
110+
if (file_exists($this->dir.'consolidated') && $this->arrayCache) {
108111
/** @var array<string, list{string, T}> */
109112
$this->cache = $this->serializer->unserialize(Providers::safeFileGetContents($this->dir.'consolidated'));
110113
}
@@ -226,7 +229,9 @@ public function getItem(string $key, ?string $hash = ''): array|object|string|nu
226229

227230
/** @var T */
228231
$content = $this->serializer->unserialize($content);
229-
$this->cache[$key] = [$hash, $content];
232+
if ($this->arrayCache) {
233+
$this->cache[$key] = [$hash, $content];
234+
}
230235
return $content;
231236
}
232237

src/Psalm/Internal/Codebase/DataFlowGraph.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use function abs;
1111
use function array_keys;
12-
use function array_reverse;
1312
use function array_sum;
1413
use function count;
1514
use function str_starts_with;

src/Psalm/Internal/Fork/ForkContext.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ private function checkExit(bool $wait = false): ?int
235235
if ($signal === 11) {
236236
$signal = "11: THIS IS A PHP BUG, please report this to https://github.com/vimeo/psalm/issues".
237237
" AND to https://github.com/php/php-src/issues";
238+
} elseif ($signal === 9) {
239+
$signal = "9: the process was likely killed by the OOM killer, try increasing the swap space ".
240+
"or use the arrayCache=\"false\" config to reduce memory usage";
238241
}
239242
throw new ContextException("Worker exited due to signal $signal!");
240243
}

0 commit comments

Comments
 (0)