Skip to content

Commit 550c878

Browse files
ondrejmirtesclaude
andcommitted
Configure LRU limits of BetterReflection memoizing caches
Wires the new optional cache limits in BetterReflection through configuration parameters: - cache.phpStormStubsNodesCountMax (default 64): binary search on self-analysis showed the memory floor is reached at 32-64 with no elapsed time cost even on cold caches (2.7 GB -> 2.5 GB). Stub nodes are read mostly once because reflections are memoized upstream, so a small limit is safe; 64 keeps headroom for stdlib-heavy projects. - cache.memoizingSourceLocatorEntriesCountMax (default 0 = unlimited): measurements showed capping this cache is counterproductive - its entries are pointers to reflections pinned by upstream memoization, so eviction frees nothing and forces re-locations that build duplicate reflection graphs (limit 128 increased total worker memory from 2.7 GB to 3.15 GB). The parameter exists as an escape hatch only. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017DjgMHgfvwhD4ogdTaRtLp
1 parent 856b9a9 commit 550c878

8 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎conf/config.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ parameters:
126126
nodesByStringCountMax: 256
127127
resolvedPhpDocBlockCacheCountMax: 2048
128128
nameScopeMapMemoryCacheCountMax: 512
129+
memoizingSourceLocatorEntriesCountMax: 0
130+
phpStormStubsNodesCountMax: 64
129131
reportUnmatchedIgnoredErrors: true
130132
reportIgnoresWithoutComments: false
131133
typeAliases: []

‎conf/config.stubValidator.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ services:
1919
arguments:
2020
allStubFiles: %allStubFiles%
2121
php8Parser: @php8PhpParser
22+
memoizingSourceLocatorEntriesCountMax: %cache.memoizingSourceLocatorEntriesCountMax%
2223

2324
# overrides service from parsers.neon
2425
defaultAnalysisParser!:

‎conf/parametersSchema.neon‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ parametersSchema:
149149
cache: structure([
150150
nodesByStringCountMax: int(),
151151
resolvedPhpDocBlockCacheCountMax: int(),
152-
nameScopeMapMemoryCacheCountMax: int()
152+
nameScopeMapMemoryCacheCountMax: int(),
153+
memoizingSourceLocatorEntriesCountMax: int(),
154+
phpStormStubsNodesCountMax: int()
153155
])
154156
reportUnmatchedIgnoredErrors: bool()
155157
reportIgnoresWithoutComments: bool()

‎src/PhpDoc/StubSourceLocatorFactory.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
private OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository,
2727
private OptimizedPsrAutoloaderLocatorFactory $optimizedPsrAutoloaderLocatorFactory,
2828
private array $allStubFiles,
29+
private int $memoizingSourceLocatorEntriesCountMax,
2930
)
3031
{
3132
}
@@ -51,7 +52,10 @@ public function create(): SourceLocator
5152

5253
$locators[] = new PhpInternalSourceLocator($astPhp8Locator, $this->phpStormStubsSourceStubber);
5354

54-
return new MemoizingSourceLocator(new AggregateSourceLocator($locators));
55+
return new MemoizingSourceLocator(
56+
new AggregateSourceLocator($locators),
57+
$this->memoizingSourceLocatorEntriesCountMax === 0 ? null : $this->memoizingSourceLocatorEntriesCountMax,
58+
);
5559
}
5660

5761
}

‎src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function __construct(
7575
private bool $playgroundMode, // makes all PHPStan classes in the PHAR discoverable with PSR-4
7676
#[AutowiredParameter]
7777
private ?string $singleReflectionFile,
78+
#[AutowiredParameter(ref: '%cache.memoizingSourceLocatorEntriesCountMax%')]
79+
private int $memoizingSourceLocatorEntriesCountMax,
7880
)
7981
{
8082
}
@@ -173,7 +175,10 @@ public function create(): SourceLocator
173175
return new AggregateSourceLocator($locators);
174176
};
175177

176-
return new MemoizingSourceLocator(new LazySourceLocator($initializer));
178+
return new MemoizingSourceLocator(
179+
new LazySourceLocator($initializer),
180+
$this->memoizingSourceLocatorEntriesCountMax === 0 ? null : $this->memoizingSourceLocatorEntriesCountMax,
181+
);
177182
}
178183

179184
}

‎src/Reflection/BetterReflection/SourceStubber/PhpStormStubsSourceStubberFactory.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ public function __construct(
1818
private Parser $phpParser,
1919
private Printer $printer,
2020
private PhpVersion $phpVersion,
21+
#[AutowiredParameter(ref: '%cache.phpStormStubsNodesCountMax%')]
22+
private int $phpStormStubsNodesCountMax,
2123
)
2224
{
2325
}
2426

2527
public function create(): PhpStormStubsSourceStubber
2628
{
27-
return new PhpStormStubsSourceStubber($this->phpParser, $this->printer, $this->phpVersion->getVersionId());
29+
return new PhpStormStubsSourceStubber(
30+
$this->phpParser,
31+
$this->printer,
32+
$this->phpVersion->getVersionId(),
33+
$this->phpStormStubsNodesCountMax === 0 ? null : $this->phpStormStubsNodesCountMax,
34+
);
2835
}
2936

3037
}

‎src/Testing/TestCase.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
php8Parser: @php8PhpParser
1010
fileExtensions: %fileExtensions%
1111
excludePaths: %excludePaths%
12+
memoizingSourceLocatorEntriesCountMax: %cache.memoizingSourceLocatorEntriesCountMax%
1213

1314
# overrides service from parsers.neon
1415
currentPhpVersionSimpleParser!:

‎src/Testing/TestCaseSourceLocatorFactory.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct(
4747
private PhpVersion $phpVersion,
4848
private array $fileExtensions,
4949
private ?array $excludePaths,
50+
private int $memoizingSourceLocatorEntriesCountMax,
5051
)
5152
{
5253
}
@@ -97,7 +98,10 @@ public function create(): SourceLocator
9798
$locators[] = new PhpVersionBlacklistSourceLocator(new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber);
9899
$locators[] = new PhpVersionBlacklistSourceLocator(new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber);
99100

100-
return new MemoizingSourceLocator(new AggregateSourceLocator($locators));
101+
return new MemoizingSourceLocator(
102+
new AggregateSourceLocator($locators),
103+
$this->memoizingSourceLocatorEntriesCountMax === 0 ? null : $this->memoizingSourceLocatorEntriesCountMax,
104+
);
101105
}
102106

103107
}

0 commit comments

Comments
 (0)