From 5d0861b306c20e62fe78e5026f929440a9502b51 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 21 Dec 2025 11:31:02 +0100 Subject: [PATCH 1/2] Use separate cache for CachedParser->parseFile() --- src/Parser/CachedParser.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 400c21bf5a..98a8dbb854 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -14,8 +14,10 @@ final class CachedParser implements Parser private int $cachedNodesByStringCount = 0; - /** @var array */ - private array $parsedByString = []; + /** @var array*/ + private array $cachedNodesByFile = []; + + private int $cachedNodesByFileCount = 0; public function __construct( private Parser $originalParser, @@ -30,24 +32,22 @@ public function __construct( */ public function parseFile(string $file): array { - if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) { - $this->cachedNodesByString = array_slice( - $this->cachedNodesByString, + if ($this->cachedNodesByFileCount !== 0 && $this->cachedNodesByFileCount >= $this->cachedNodesByStringCountMax) { + $this->cachedNodesByFile = array_slice( + $this->cachedNodesByFile, 1, preserve_keys: true, ); - --$this->cachedNodesByStringCount; + --$this->cachedNodesByFileCount; } - $sourceCode = FileReader::read($file); - if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) { - $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file); - $this->cachedNodesByStringCount++; - unset($this->parsedByString[$sourceCode]); + if (!isset($this->cachedNodesByFile[$file])) { + $this->cachedNodesByFile[$file] = $this->originalParser->parseFile($file); + $this->cachedNodesByFileCount++; } - return $this->cachedNodesByString[$sourceCode]; + return $this->cachedNodesByFile[$file]; } /** @@ -68,7 +68,6 @@ public function parseString(string $sourceCode): array if (!isset($this->cachedNodesByString[$sourceCode])) { $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode); $this->cachedNodesByStringCount++; - $this->parsedByString[$sourceCode] = true; } return $this->cachedNodesByString[$sourceCode]; From fbfe45470920f29af2f6420a805c0e1e68195678 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 23 Dec 2025 10:22:27 +0100 Subject: [PATCH 2/2] simplify --- src/Parser/CachedParser.php | 36 +---------------------- tests/PHPStan/Parser/CachedParserTest.php | 15 ---------- 2 files changed, 1 insertion(+), 50 deletions(-) diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 98a8dbb854..34957ae430 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -3,17 +3,11 @@ namespace PHPStan\Parser; use PhpParser\Node; -use PHPStan\File\FileReader; use function array_slice; final class CachedParser implements Parser { - /** @var array*/ - private array $cachedNodesByString = []; - - private int $cachedNodesByStringCount = 0; - /** @var array*/ private array $cachedNodesByFile = []; @@ -55,27 +49,7 @@ public function parseFile(string $file): array */ public function parseString(string $sourceCode): array { - if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) { - $this->cachedNodesByString = array_slice( - $this->cachedNodesByString, - 1, - preserve_keys: true, - ); - - --$this->cachedNodesByStringCount; - } - - if (!isset($this->cachedNodesByString[$sourceCode])) { - $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode); - $this->cachedNodesByStringCount++; - } - - return $this->cachedNodesByString[$sourceCode]; - } - - public function getCachedNodesByStringCount(): int - { - return $this->cachedNodesByStringCount; + return $this->originalParser->parseString($sourceCode); } public function getCachedNodesByStringCountMax(): int @@ -83,12 +57,4 @@ public function getCachedNodesByStringCountMax(): int return $this->cachedNodesByStringCountMax; } - /** - * @return array - */ - public function getCachedNodesByString(): array - { - return $this->cachedNodesByString; - } - } diff --git a/tests/PHPStan/Parser/CachedParserTest.php b/tests/PHPStan/Parser/CachedParserTest.php index 29ca21ee3a..9bcbc98c0c 100644 --- a/tests/PHPStan/Parser/CachedParserTest.php +++ b/tests/PHPStan/Parser/CachedParserTest.php @@ -29,21 +29,6 @@ public function testParseFileClearCache( $cachedNodesByStringCountMax, $parser->getCachedNodesByStringCountMax(), ); - - // Add strings to cache - for ($i = 0; $i <= $cachedNodesByStringCountMax; $i++) { - $parser->parseString('string' . $i); - } - - $this->assertSame( - $cachedNodesByStringCountExpected, - $parser->getCachedNodesByStringCount(), - ); - - $this->assertCount( - $cachedNodesByStringCountExpected, - $parser->getCachedNodesByString(), - ); } /**