Skip to content

Commit ba40208

Browse files
committed
Cut memory consumption
1 parent 4182e35 commit ba40208

File tree

3 files changed

+3
-74
lines changed

3 files changed

+3
-74
lines changed

conf/config.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ services:
493493
class: PHPStan\Parser\CachedParser
494494
arguments:
495495
originalParser: @directParser
496-
cachedNodesByFileCountMax: %cache.nodesByFileCountMax%
497496
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
498497

499498
-

src/Parser/CachedParser.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
namespace PHPStan\Parser;
44

5+
use PHPStan\File\FileReader;
6+
57
class CachedParser implements Parser
68
{
79

810
private \PHPStan\Parser\Parser $originalParser;
911

10-
/** @var array<string, \PhpParser\Node\Stmt[]> */
11-
private array $cachedNodesByFile = [];
12-
13-
private int $cachedNodesByFileCount = 0;
14-
15-
private int $cachedNodesByFileCountMax;
16-
1712
/** @var array<string, \PhpParser\Node\Stmt[]>*/
1813
private array $cachedNodesByString = [];
1914

@@ -23,12 +18,10 @@ class CachedParser implements Parser
2318

2419
public function __construct(
2520
Parser $originalParser,
26-
int $cachedNodesByFileCountMax,
2721
int $cachedNodesByStringCountMax
2822
)
2923
{
3024
$this->originalParser = $originalParser;
31-
$this->cachedNodesByFileCountMax = $cachedNodesByFileCountMax;
3225
$this->cachedNodesByStringCountMax = $cachedNodesByStringCountMax;
3326
}
3427

@@ -38,23 +31,7 @@ public function __construct(
3831
*/
3932
public function parseFile(string $file): array
4033
{
41-
if ($this->cachedNodesByFileCountMax !== 0 && $this->cachedNodesByFileCount >= $this->cachedNodesByFileCountMax) {
42-
$this->cachedNodesByFile = array_slice(
43-
$this->cachedNodesByFile,
44-
1,
45-
null,
46-
true
47-
);
48-
49-
--$this->cachedNodesByFileCount;
50-
}
51-
52-
if (!isset($this->cachedNodesByFile[$file])) {
53-
$this->cachedNodesByFile[$file] = $this->originalParser->parseFile($file);
54-
$this->cachedNodesByFileCount++;
55-
}
56-
57-
return $this->cachedNodesByFile[$file];
34+
return $this->parseString(FileReader::read($file));
5835
}
5936

6037
/**
@@ -82,16 +59,6 @@ public function parseString(string $sourceCode): array
8259
return $this->cachedNodesByString[$sourceCode];
8360
}
8461

85-
public function getCachedNodesByFileCount(): int
86-
{
87-
return $this->cachedNodesByFileCount;
88-
}
89-
90-
public function getCachedNodesByFileCountMax(): int
91-
{
92-
return $this->cachedNodesByFileCountMax;
93-
}
94-
9562
public function getCachedNodesByStringCount(): int
9663
{
9764
return $this->cachedNodesByStringCount;
@@ -102,14 +69,6 @@ public function getCachedNodesByStringCountMax(): int
10269
return $this->cachedNodesByStringCountMax;
10370
}
10471

105-
/**
106-
* @return array<string, \PhpParser\Node[]>
107-
*/
108-
public function getCachedNodesByFile(): array
109-
{
110-
return $this->cachedNodesByFile;
111-
}
112-
11372
/**
11473
* @return array<string, \PhpParser\Node[]>
11574
*/

tests/PHPStan/Parser/CachedParserTest.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,24 @@ class CachedParserTest extends \PHPUnit\Framework\TestCase
77

88
/**
99
* @dataProvider dataParseFileClearCache
10-
* @param int $cachedNodesByFileCountMax
1110
* @param int $cachedNodesByStringCountMax
12-
* @param int $cachedNodesByFileCountExpected
1311
* @param int $cachedNodesByStringCountExpected
1412
*/
1513
public function testParseFileClearCache(
16-
int $cachedNodesByFileCountMax,
1714
int $cachedNodesByStringCountMax,
18-
int $cachedNodesByFileCountExpected,
1915
int $cachedNodesByStringCountExpected
2016
): void
2117
{
2218
$parser = new CachedParser(
2319
$this->getParserMock(),
24-
$cachedNodesByFileCountMax,
2520
$cachedNodesByStringCountMax
2621
);
2722

28-
$this->assertEquals(
29-
$cachedNodesByFileCountMax,
30-
$parser->getCachedNodesByFileCountMax()
31-
);
32-
3323
$this->assertEquals(
3424
$cachedNodesByStringCountMax,
3525
$parser->getCachedNodesByStringCountMax()
3626
);
3727

38-
// Add files to cache
39-
for ($i = 0; $i <= $cachedNodesByFileCountMax; $i++) {
40-
$parser->parseFile('file' . $i);
41-
}
42-
43-
$this->assertEquals(
44-
$cachedNodesByFileCountExpected,
45-
$parser->getCachedNodesByFileCount()
46-
);
47-
48-
$this->assertCount(
49-
$cachedNodesByFileCountExpected,
50-
$parser->getCachedNodesByFile()
51-
);
52-
5328
// Add strings to cache
5429
for ($i = 0; $i <= $cachedNodesByStringCountMax; $i++) {
5530
$parser->parseString('string' . $i);
@@ -69,16 +44,12 @@ public function testParseFileClearCache(
6944
public function dataParseFileClearCache(): \Generator
7045
{
7146
yield 'even' => [
72-
'cachedNodesByFileCountMax' => 100,
7347
'cachedNodesByStringCountMax' => 50,
74-
'cachedNodesByFileCountExpected' => 100,
7548
'cachedNodesByStringCountExpected' => 50,
7649
];
7750

7851
yield 'odd' => [
79-
'cachedNodesByFileCountMax' => 101,
8052
'cachedNodesByStringCountMax' => 51,
81-
'cachedNodesByFileCountExpected' => 101,
8253
'cachedNodesByStringCountExpected' => 51,
8354
];
8455
}

0 commit comments

Comments
 (0)