Skip to content

Commit bf4d513

Browse files
ondrejmirtesclaude
andcommitted
Cap CachedParser by total cached source size
The AST of a parsed file takes up roughly 50-60x more memory than the source code itself, and the cache limit was entry-count-based only, so a handful of very large files could pin tens of megabytes of AST in each worker process for the whole run. Evict by total source bytes of the cached entries in addition to the entry count. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017DjgMHgfvwhD4ogdTaRtLp
1 parent 896595b commit bf4d513

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

‎src/Parser/CachedParser.php‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@
55
use PhpParser\Node;
66
use PHPStan\File\FileReader;
77
use function array_key_first;
8+
use function strlen;
89

910
final class CachedParser implements Parser
1011
{
1112

13+
/**
14+
* The AST of a parsed file takes up roughly 50-60x more memory
15+
* than the source code itself. Together with the entry count limit,
16+
* this caps the total source size of the cached ASTs so that a few
17+
* large files cannot pin hundreds of megabytes in each worker process.
18+
*/
19+
private const CACHED_SOURCE_BYTES_LIMIT = 524_288;
20+
1221
/** @var array<string, Node\Stmt[]>*/
1322
private array $cachedNodesByString = [];
1423

1524
private int $cachedNodesByStringCount = 0;
1625

26+
private int $cachedSourceBytes = 0;
27+
1728
/** @var array<string, true> */
1829
private array $parsedByString = [];
1930

@@ -42,8 +53,9 @@ public function parseFile(string $file): array
4253
// no net change to the entry count, just refresh its LRU position
4354
unset($this->cachedNodesByString[$sourceCode], $this->parsedByString[$sourceCode]);
4455
} else {
45-
$this->evictLeastRecentlyUsed();
56+
$this->evictLeastRecentlyUsed(strlen($sourceCode));
4657
$this->cachedNodesByStringCount++;
58+
$this->cachedSourceBytes += strlen($sourceCode);
4759
}
4860

4961
$this->cachedNodesByString[$sourceCode] = $nodes;
@@ -61,9 +73,10 @@ public function parseString(string $sourceCode): array
6173
}
6274

6375
$nodes = $this->originalParser->parseString($sourceCode);
64-
$this->evictLeastRecentlyUsed();
76+
$this->evictLeastRecentlyUsed(strlen($sourceCode));
6577
$this->cachedNodesByString[$sourceCode] = $nodes;
6678
$this->cachedNodesByStringCount++;
79+
$this->cachedSourceBytes += strlen($sourceCode);
6780
$this->parsedByString[$sourceCode] = true;
6881

6982
return $nodes;
@@ -84,20 +97,24 @@ private function markRecentlyUsed(string $sourceCode): array
8497
return $nodes;
8598
}
8699

87-
private function evictLeastRecentlyUsed(): void
100+
private function evictLeastRecentlyUsed(int $incomingSourceBytes): void
88101
{
89102
if ($this->cachedNodesByStringCountMax === 0) {
90103
return;
91104
}
92105

93-
while ($this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) {
106+
while (
107+
$this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax
108+
|| $this->cachedSourceBytes + $incomingSourceBytes > self::CACHED_SOURCE_BYTES_LIMIT
109+
) {
94110
$oldestKey = array_key_first($this->cachedNodesByString);
95111
if ($oldestKey === null) {
96112
break;
97113
}
98114

99115
unset($this->cachedNodesByString[$oldestKey], $this->parsedByString[$oldestKey]);
100116
$this->cachedNodesByStringCount--;
117+
$this->cachedSourceBytes -= strlen($oldestKey);
101118
}
102119
}
103120

0 commit comments

Comments
 (0)