Skip to content

Commit 896595b

Browse files
ondrejmirtesclaude
andcommitted
Restore value sharing in name scope maps hydrated from the file cache
The name scope maps are persisted with var_export() which cannot represent shared references. A hydrated map of a file with many members carried its own copy of the same uses/constUses arrays and a duplicate of the whole parent scope chain in every single scope, taking up many times more memory than a freshly created map where these values are shared. Interning the hydrated scopes restores the sharing. In self-analysis this shrinks the retained size of FileTypeMapper::$memoryCache in an average worker process from ~70 MB to ~7 MB. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017DjgMHgfvwhD4ogdTaRtLp
1 parent 4be03a5 commit 896595b

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎src/Analyser/IntermediaryNameScope.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Analyser;
44

55
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
6+
use function serialize;
67

78
final class IntermediaryNameScope
89
{
@@ -115,6 +116,54 @@ public function unsetTemplatePhpDocNodes(array $namesToUnset): self
115116
);
116117
}
117118

119+
/**
120+
* Restores sharing of identical property values and parent scopes after hydration from the file cache.
121+
*
122+
* var_export() used by the cache cannot represent shared references, so every hydrated
123+
* scope carries its own copy of the same uses maps and of the whole parent chain.
124+
* Without interning, the name scope map of a file with many members takes up
125+
* many times more memory when loaded from the cache than when freshly created.
126+
*
127+
* @param array<string, self|array<mixed>> $pool
128+
*/
129+
public function intern(array &$pool): self
130+
{
131+
$key = serialize($this);
132+
if (isset($pool[$key])) {
133+
/** @var self */
134+
return $pool[$key];
135+
}
136+
137+
$this->uses = self::internArray($pool, $this->uses);
138+
$this->templatePhpDocNodes = self::internArray($pool, $this->templatePhpDocNodes);
139+
$this->typeAliasesMap = self::internArray($pool, $this->typeAliasesMap);
140+
$this->constUses = self::internArray($pool, $this->constUses);
141+
if ($this->parent !== null) {
142+
$this->parent = $this->parent->intern($pool);
143+
}
144+
145+
return $pool[$key] = $this;
146+
}
147+
148+
/**
149+
* @template T of array<mixed>
150+
* @param array<string, self|array<mixed>> $pool
151+
* @param T $value
152+
* @return T
153+
*/
154+
private static function internArray(array &$pool, array $value): array
155+
{
156+
$key = 'a:' . serialize($value);
157+
if (isset($pool[$key])) {
158+
/** @var T */
159+
return $pool[$key];
160+
}
161+
162+
$pool[$key] = $value;
163+
164+
return $value;
165+
}
166+
118167
/**
119168
* @return array{string, string, string, string|null, string|null}|null
120169
*/

‎src/Type/FileTypeMapper.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ private function loadCachedPhpDocNodeMap(string $cacheKey, string $variableCache
401401
}
402402

403403
if ($useCache) {
404+
$pool = [];
405+
foreach ($nameScopeMap as $nameScopeKey => $intermediaryNameScope) {
406+
$nameScopeMap[$nameScopeKey] = $intermediaryNameScope->intern($pool);
407+
}
408+
404409
return [$nameScopeMap, array_keys($filesWithHashes)];
405410
}
406411
}

0 commit comments

Comments
 (0)