High memory use when analysing large literal arrays #15452
Description
Bug report
Analysing a PHP array literal with 20,000 string keys and values costs about 120 MB and 1.2 s for one file on PHPStan 2.3.1.
Reproducer: https://github.com/alfredbez/phpstan-literal-array-memory-reproducer
It generates two files that return the same data. One contains the array literal, the other loads it from a JSON file with json_decode(file_get_contents(...)). Each file is analysed in a separate process with an empty cache, at level 6 with --debug -vvv.
Results on macOS arm64, PHP 8.5.10, Xdebug disabled, 20,000 entries (medians of three runs):
| PHPStan | Array literal | JSON control |
|---|---|---|
| 2.3.0 | 121.5 MB, 1.19 s | 9.98 MB, 0.07 s |
| 2.3.1 | 121.5 MB, 1.19 s | 7.98 MB, 0.07 s |
These are the per-file "consumed" values from --debug -vvv: the increase in memory_get_peak_usage(true) during analysis, not retained memory or RSS. All analyses exit 0.
The JSON variant does not require PHPStan to infer every entry, so it does less static-analysis work. I do not expect the same cost, but I would like to see whether the literal-array case can use less memory without losing useful type information. This comparison does not show a regression between 2.3.0 and 2.3.1 or a memory leak.
I already opened phpstan/phpstan-src#6727 with a proposed fix. Opening this issue with the reproducer as requested there.
Code snippet that reproduces the problem
git clone https://github.com/alfredbez/phpstan-literal-array-memory-reproducer.git
cd phpstan-literal-array-memory-reproducer
composer install
XDEBUG_MODE=off php run.phpRequires PHP 8.2 or newer and Composer. The repository pins PHPStan 2.3.1. To use another PHPStan version or a larger array:
XDEBUG_MODE=off php run.php /path/to/phpstan.phar 40000Expected output
Lower peak memory use when analysing large literal arrays, without losing useful type information. I am happy to test a fix against the reproducer.
Activity
mergeable commentedon Oct 10, 2026
This bug report is missing a link to reproduction at phpstan.org/try.
It will most likely be closed after manual review.