From ba40208daa839870bc0d5faaf2c3bc52dbd4a941 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 26 Aug 2020 19:46:51 +0200 Subject: [PATCH] Cut memory consumption --- conf/config.neon | 1 - src/Parser/CachedParser.php | 47 ++--------------------- tests/PHPStan/Parser/CachedParserTest.php | 29 -------------- 3 files changed, 3 insertions(+), 74 deletions(-) diff --git a/conf/config.neon b/conf/config.neon index 9cfe233f20..b0c90007b2 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -493,7 +493,6 @@ services: class: PHPStan\Parser\CachedParser arguments: originalParser: @directParser - cachedNodesByFileCountMax: %cache.nodesByFileCountMax% cachedNodesByStringCountMax: %cache.nodesByStringCountMax% - diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 1185387610..4b27a9c4bd 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -2,18 +2,13 @@ namespace PHPStan\Parser; +use PHPStan\File\FileReader; + class CachedParser implements Parser { private \PHPStan\Parser\Parser $originalParser; - /** @var array */ - private array $cachedNodesByFile = []; - - private int $cachedNodesByFileCount = 0; - - private int $cachedNodesByFileCountMax; - /** @var array*/ private array $cachedNodesByString = []; @@ -23,12 +18,10 @@ class CachedParser implements Parser public function __construct( Parser $originalParser, - int $cachedNodesByFileCountMax, int $cachedNodesByStringCountMax ) { $this->originalParser = $originalParser; - $this->cachedNodesByFileCountMax = $cachedNodesByFileCountMax; $this->cachedNodesByStringCountMax = $cachedNodesByStringCountMax; } @@ -38,23 +31,7 @@ public function __construct( */ public function parseFile(string $file): array { - if ($this->cachedNodesByFileCountMax !== 0 && $this->cachedNodesByFileCount >= $this->cachedNodesByFileCountMax) { - $this->cachedNodesByFile = array_slice( - $this->cachedNodesByFile, - 1, - null, - true - ); - - --$this->cachedNodesByFileCount; - } - - if (!isset($this->cachedNodesByFile[$file])) { - $this->cachedNodesByFile[$file] = $this->originalParser->parseFile($file); - $this->cachedNodesByFileCount++; - } - - return $this->cachedNodesByFile[$file]; + return $this->parseString(FileReader::read($file)); } /** @@ -82,16 +59,6 @@ public function parseString(string $sourceCode): array return $this->cachedNodesByString[$sourceCode]; } - public function getCachedNodesByFileCount(): int - { - return $this->cachedNodesByFileCount; - } - - public function getCachedNodesByFileCountMax(): int - { - return $this->cachedNodesByFileCountMax; - } - public function getCachedNodesByStringCount(): int { return $this->cachedNodesByStringCount; @@ -102,14 +69,6 @@ public function getCachedNodesByStringCountMax(): int return $this->cachedNodesByStringCountMax; } - /** - * @return array - */ - public function getCachedNodesByFile(): array - { - return $this->cachedNodesByFile; - } - /** * @return array */ diff --git a/tests/PHPStan/Parser/CachedParserTest.php b/tests/PHPStan/Parser/CachedParserTest.php index ca71124e28..b014507974 100644 --- a/tests/PHPStan/Parser/CachedParserTest.php +++ b/tests/PHPStan/Parser/CachedParserTest.php @@ -7,49 +7,24 @@ class CachedParserTest extends \PHPUnit\Framework\TestCase /** * @dataProvider dataParseFileClearCache - * @param int $cachedNodesByFileCountMax * @param int $cachedNodesByStringCountMax - * @param int $cachedNodesByFileCountExpected * @param int $cachedNodesByStringCountExpected */ public function testParseFileClearCache( - int $cachedNodesByFileCountMax, int $cachedNodesByStringCountMax, - int $cachedNodesByFileCountExpected, int $cachedNodesByStringCountExpected ): void { $parser = new CachedParser( $this->getParserMock(), - $cachedNodesByFileCountMax, $cachedNodesByStringCountMax ); - $this->assertEquals( - $cachedNodesByFileCountMax, - $parser->getCachedNodesByFileCountMax() - ); - $this->assertEquals( $cachedNodesByStringCountMax, $parser->getCachedNodesByStringCountMax() ); - // Add files to cache - for ($i = 0; $i <= $cachedNodesByFileCountMax; $i++) { - $parser->parseFile('file' . $i); - } - - $this->assertEquals( - $cachedNodesByFileCountExpected, - $parser->getCachedNodesByFileCount() - ); - - $this->assertCount( - $cachedNodesByFileCountExpected, - $parser->getCachedNodesByFile() - ); - // Add strings to cache for ($i = 0; $i <= $cachedNodesByStringCountMax; $i++) { $parser->parseString('string' . $i); @@ -69,16 +44,12 @@ public function testParseFileClearCache( public function dataParseFileClearCache(): \Generator { yield 'even' => [ - 'cachedNodesByFileCountMax' => 100, 'cachedNodesByStringCountMax' => 50, - 'cachedNodesByFileCountExpected' => 100, 'cachedNodesByStringCountExpected' => 50, ]; yield 'odd' => [ - 'cachedNodesByFileCountMax' => 101, 'cachedNodesByStringCountMax' => 51, - 'cachedNodesByFileCountExpected' => 101, 'cachedNodesByStringCountExpected' => 51, ]; }