Skip to content

Commit

Permalink
Cut memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 26, 2020
1 parent 4182e35 commit ba40208
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 74 deletions.
1 change: 0 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ services:
class: PHPStan\Parser\CachedParser
arguments:
originalParser: @directParser
cachedNodesByFileCountMax: %cache.nodesByFileCountMax%
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%

-
Expand Down
47 changes: 3 additions & 44 deletions src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

namespace PHPStan\Parser;

use PHPStan\File\FileReader;

class CachedParser implements Parser
{

private \PHPStan\Parser\Parser $originalParser;

/** @var array<string, \PhpParser\Node\Stmt[]> */
private array $cachedNodesByFile = [];

private int $cachedNodesByFileCount = 0;

private int $cachedNodesByFileCountMax;

/** @var array<string, \PhpParser\Node\Stmt[]>*/
private array $cachedNodesByString = [];

Expand All @@ -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;
}

Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -102,14 +69,6 @@ public function getCachedNodesByStringCountMax(): int
return $this->cachedNodesByStringCountMax;
}

/**
* @return array<string, \PhpParser\Node[]>
*/
public function getCachedNodesByFile(): array
{
return $this->cachedNodesByFile;
}

/**
* @return array<string, \PhpParser\Node[]>
*/
Expand Down
29 changes: 0 additions & 29 deletions tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
];
}
Expand Down

0 comments on commit ba40208

Please sign in to comment.