forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedParser.php
More file actions
161 lines (135 loc) · 4.65 KB
/
Copy pathCachedParser.php
File metadata and controls
161 lines (135 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use PhpParser\Node;
use PHPStan\File\FileReader;
use function array_key_first;
use function strlen;
final class CachedParser implements Parser
{
/**
* Size-based eviction never shrinks the cache below this many entries.
* Without this floor, a single source larger than $cachedSourceBytesMax
* would flush the whole cache on every insertion, degenerating the cache
* to a single entry whenever such a source is hot.
*/
private const SIZE_EVICTION_FLOOR_LIMIT = 32;
/**
* Default for $cachedSourceBytesMax, must match the default of
* cache.nodesByStringSourceBytesMax in config.neon. It doubles as the
* constructor default because third-party extensions (e.g. Larastan's
* migrationsParser service) instantiate this class without the parameter.
*/
private const CACHED_SOURCE_BYTES_DEFAULT_LIMIT = 4_194_304;
/** @var array<string, Node\Stmt[]>*/
private array $cachedNodesByString = [];
private int $cachedNodesByStringCount = 0;
private int $cachedSourceBytes = 0;
/** @var array<string, true> */
private array $parsedByString = [];
/**
* The AST of a parsed file takes up roughly 50-60x more memory than the
* source code itself, so alongside the entry count limit, the total source
* size of the cached ASTs is capped by $cachedSourceBytesMax (0 = unlimited)
* so that large files cannot pin hundreds of megabytes in each worker
* process. The cap has to be generous enough to hold a big project's hot
* working set (WordPress needs ~4 MB) because evicting a hot file costs
* a re-parse proportional to the very bytes the eviction saved.
*/
public function __construct(
private Parser $originalParser,
private int $cachedNodesByStringCountMax,
private int $cachedSourceBytesMax = self::CACHED_SOURCE_BYTES_DEFAULT_LIMIT,
)
{
}
/**
* @param string $file path to a file to parse
* @return Node\Stmt[]
*/
public function parseFile(string $file): array
{
$sourceCode = FileReader::read($file);
$isCached = isset($this->cachedNodesByString[$sourceCode]);
if ($isCached && !isset($this->parsedByString[$sourceCode])) {
return $this->markRecentlyUsed($sourceCode);
}
$nodes = $this->originalParser->parseFile($file);
if ($isCached) {
// upgrade an entry previously produced by parseString() in place -
// no net change to the entry count, just refresh its LRU position
unset($this->cachedNodesByString[$sourceCode], $this->parsedByString[$sourceCode]);
} else {
$this->evictLeastRecentlyUsed(strlen($sourceCode));
$this->cachedNodesByStringCount++;
$this->cachedSourceBytes += strlen($sourceCode);
}
$this->cachedNodesByString[$sourceCode] = $nodes;
return $nodes;
}
/**
* @return Node\Stmt[]
*/
public function parseString(string $sourceCode): array
{
if (isset($this->cachedNodesByString[$sourceCode])) {
return $this->markRecentlyUsed($sourceCode);
}
$nodes = $this->originalParser->parseString($sourceCode);
$this->evictLeastRecentlyUsed(strlen($sourceCode));
$this->cachedNodesByString[$sourceCode] = $nodes;
$this->cachedNodesByStringCount++;
$this->cachedSourceBytes += strlen($sourceCode);
$this->parsedByString[$sourceCode] = true;
return $nodes;
}
/**
* LRU bookkeeping: re-insert the entry at the end so genuinely cold sources
* are evicted first, not the ones inserted earliest.
*
* @return Node\Stmt[]
*/
private function markRecentlyUsed(string $sourceCode): array
{
$nodes = $this->cachedNodesByString[$sourceCode];
unset($this->cachedNodesByString[$sourceCode]);
$this->cachedNodesByString[$sourceCode] = $nodes;
return $nodes;
}
private function evictLeastRecentlyUsed(int $incomingSourceBytes): void
{
if ($this->cachedNodesByStringCountMax === 0) {
return;
}
while (
$this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax
|| (
$this->cachedSourceBytesMax > 0
&& $this->cachedSourceBytes + $incomingSourceBytes > $this->cachedSourceBytesMax
&& $this->cachedNodesByStringCount > self::SIZE_EVICTION_FLOOR_LIMIT
)
) {
$oldestKey = array_key_first($this->cachedNodesByString);
if ($oldestKey === null) {
break;
}
unset($this->cachedNodesByString[$oldestKey], $this->parsedByString[$oldestKey]);
$this->cachedNodesByStringCount--;
$this->cachedSourceBytes -= strlen($oldestKey);
}
}
public function getCachedNodesByStringCount(): int
{
return $this->cachedNodesByStringCount;
}
public function getCachedNodesByStringCountMax(): int
{
return $this->cachedNodesByStringCountMax;
}
/**
* @return array<string, Node[]>
*/
public function getCachedNodesByString(): array
{
return $this->cachedNodesByString;
}
}