Skip to content

Commit b75424d

Browse files
committed
Use separate cache for CachedParser->parseFile()
1 parent ff39220 commit b75424d

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/Parser/CachedParser.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ final class CachedParser implements Parser
1414

1515
private int $cachedNodesByStringCount = 0;
1616

17-
/** @var array<string, true> */
18-
private array $parsedByString = [];
17+
/** @var array<string, Node\Stmt[]>*/
18+
private array $cachedNodesByFile = [];
19+
20+
private int $cachedNodesByFileCount = 0;
1921

2022
public function __construct(
2123
private Parser $originalParser,
@@ -30,24 +32,22 @@ public function __construct(
3032
*/
3133
public function parseFile(string $file): array
3234
{
33-
if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) {
34-
$this->cachedNodesByString = array_slice(
35-
$this->cachedNodesByString,
35+
if ($this->cachedNodesByFileCount !== 0 && $this->cachedNodesByFileCount >= $this->cachedNodesByStringCountMax) {
36+
$this->cachedNodesByFile = array_slice(
37+
$this->cachedNodesByFile,
3638
1,
3739
preserve_keys: true,
3840
);
3941

40-
--$this->cachedNodesByStringCount;
42+
--$this->cachedNodesByFileCount;
4143
}
4244

43-
$sourceCode = FileReader::read($file);
44-
if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) {
45-
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file);
46-
$this->cachedNodesByStringCount++;
47-
unset($this->parsedByString[$sourceCode]);
45+
if (!isset($this->cachedNodesByFile[$file])) {
46+
$this->cachedNodesByFile[$file] = $this->originalParser->parseFile($file);
47+
$this->cachedNodesByFileCount++;
4848
}
4949

50-
return $this->cachedNodesByString[$sourceCode];
50+
return $this->cachedNodesByFile[$file];
5151
}
5252

5353
/**
@@ -68,7 +68,6 @@ public function parseString(string $sourceCode): array
6868
if (!isset($this->cachedNodesByString[$sourceCode])) {
6969
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode);
7070
$this->cachedNodesByStringCount++;
71-
$this->parsedByString[$sourceCode] = true;
7271
}
7372

7473
return $this->cachedNodesByString[$sourceCode];

0 commit comments

Comments
 (0)