Skip to content

Commit 324cd4e

Browse files
staabmondrejmirtes
andauthored
Memoize ExprHandler dispatch in MutatingScope::resolveType()
Co-authored-by: Ondrej Mirtes <ondrej@mirtes.cz>
1 parent 57063b4 commit 324cd4e

4 files changed

Lines changed: 63 additions & 105 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node\Expr;
6+
use PHPStan\DependencyInjection\AutowiredService;
7+
use PHPStan\DependencyInjection\Container;
8+
use function get_class;
9+
use function spl_object_id;
10+
11+
/**
12+
* Resolves which ExprHandler handles a given Expr, memoizing the result by
13+
* Expr class so dispatch does not re-scan every tagged handler (a linear
14+
* supports() sweep) on each call.
15+
*/
16+
#[AutowiredService]
17+
final class ExprHandlerRegistry
18+
{
19+
20+
/** @var array<int, array<non-empty-string, ExprHandler<Expr>|false>> */
21+
private static array $exprHandlersByClass = [];
22+
23+
/**
24+
* @return ExprHandler<Expr>|null
25+
*/
26+
public static function resolve(Expr $expr, Container $container): ?ExprHandler
27+
{
28+
$cacheKey = get_class($expr);
29+
if ($expr instanceof Expr\CallLike) {
30+
$cacheKey .= '|' . $expr->isFirstClassCallable();
31+
if ($expr instanceof Expr\New_) {
32+
$cacheKey .= '|' . get_class($expr->class);
33+
}
34+
}
35+
36+
$containerId = spl_object_id($container);
37+
self::$exprHandlersByClass[$containerId] ??= [];
38+
$cached = self::$exprHandlersByClass[$containerId][$cacheKey] ?? null;
39+
if ($cached !== null) {
40+
return $cached === false ? null : $cached;
41+
}
42+
43+
$matchedHandler = null;
44+
/** @var ExprHandler<Expr> $exprHandler */
45+
foreach ($container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
46+
if (!$exprHandler->supports($expr)) {
47+
continue;
48+
}
49+
50+
$matchedHandler = $exprHandler;
51+
break;
52+
}
53+
54+
self::$exprHandlersByClass[$containerId][$cacheKey] = $matchedHandler ?? false;
55+
56+
return $matchedHandler;
57+
}
58+
59+
}

‎src/Analyser/MutatingScope.php‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,12 +1059,8 @@ private function resolveType(string $exprString, Expr $node): Type
10591059
return $this->expressionTypes[$exprString]->getType();
10601060
}
10611061

1062-
/** @var ExprHandler<Expr> $exprHandler */
1063-
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
1064-
if (!$exprHandler->supports($node)) {
1065-
continue;
1066-
}
1067-
1062+
$exprHandler = ExprHandlerRegistry::resolve($node, $this->container);
1063+
if ($exprHandler !== null) {
10681064
return $exprHandler->resolveType($this, $node);
10691065
}
10701066

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
use function array_slice;
176176
use function array_values;
177177
use function count;
178-
use function get_class;
179178
use function in_array;
180179
use function is_array;
181180
use function is_int;
@@ -198,19 +197,6 @@ class NodeScopeResolver
198197
/** @var array<string, true> filePath(string) => bool(true) */
199198
private array $analysedFiles = [];
200199

201-
/**
202-
* Memoizes which ExprHandler handles each Expr class so processExprNode() does not
203-
* re-scan every tagged handler on each call. Keyed by Expr class-string; false means
204-
* no handler matched (default handling).
205-
*
206-
* Call-likes (Expr\CallLike) are never memoized: their handlers select on
207-
* isFirstClassCallable(), which the Expr class alone does not determine. Every other
208-
* handler's supports() is a pure instanceof check, so the class fully determines it.
209-
*
210-
* @var array<class-string<Expr>, ExprHandler<Expr>|false>
211-
*/
212-
private array $exprHandlersByClass = [];
213-
214200
/** @var array<string, true> */
215201
private array $earlyTerminatingMethodNames;
216202

@@ -2805,7 +2791,7 @@ public function processExprNode(
28052791

28062792
$this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $storage, $context);
28072793

2808-
$exprHandler = $this->resolveExprHandler($expr);
2794+
$exprHandler = ExprHandlerRegistry::resolve($expr, $this->container);
28092795
if ($exprHandler !== null) {
28102796
return $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context);
28112797
}
@@ -2826,40 +2812,6 @@ public function processExprNode(
28262812
);
28272813
}
28282814

2829-
/**
2830-
* Resolves the ExprHandler for the given expression, memoizing the result by Expr class.
2831-
*
2832-
* @return ExprHandler<Expr>|null
2833-
*/
2834-
private function resolveExprHandler(Expr $expr): ?ExprHandler
2835-
{
2836-
// Call-likes are excluded from the cache: their handlers select on
2837-
// isFirstClassCallable(), so the Expr class does not uniquely determine the handler.
2838-
if (!$expr instanceof Expr\CallLike) {
2839-
$cached = $this->exprHandlersByClass[get_class($expr)] ?? null;
2840-
if ($cached !== null) {
2841-
return $cached === false ? null : $cached;
2842-
}
2843-
}
2844-
2845-
$matchedHandler = null;
2846-
/** @var ExprHandler<Expr> $exprHandler */
2847-
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
2848-
if (!$exprHandler->supports($expr)) {
2849-
continue;
2850-
}
2851-
2852-
$matchedHandler = $exprHandler;
2853-
break;
2854-
}
2855-
2856-
if (!$expr instanceof Expr\CallLike) {
2857-
$this->exprHandlersByClass[get_class($expr)] = $matchedHandler ?? false;
2858-
}
2859-
2860-
return $matchedHandler;
2861-
}
2862-
28632815
/**
28642816
* @param 'get'|'set' $hookName
28652817
* @return InternalThrowPoint[]

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
use function array_map;
4545
use function array_merge;
4646
use function count;
47-
use function get_class;
4847
use function in_array;
4948
use function strtolower;
5049
use function substr;
@@ -60,20 +59,6 @@ final class TypeSpecifier
6059
/** @var StaticMethodTypeSpecifyingExtension[][]|null */
6160
private ?array $staticMethodTypeSpecifyingExtensionsByClass = null;
6261

63-
/**
64-
* Memoizes which ExprHandler handles each Expr class so specifyTypesInCondition()
65-
* does not re-scan every tagged handler (a linear supports() sweep) on each call.
66-
* This matters for deep boolean chains, where specifyTypesInCondition() runs once per arm.
67-
* Keyed by Expr class-string; false means no handler matched (default narrowing).
68-
*
69-
* Call-likes (Expr\CallLike) are never memoized: their handlers select on
70-
* isFirstClassCallable(), which the Expr class alone does not determine. Every other
71-
* handler's supports() is a pure instanceof check, so the class fully determines it.
72-
*
73-
* @var array<class-string<Expr>, ExprHandler<Expr>|false>
74-
*/
75-
private array $exprHandlersByClass = [];
76-
7762
/**
7863
* @param FunctionTypeSpecifyingExtension[] $functionTypeSpecifyingExtensions
7964
* @param MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions
@@ -104,48 +89,14 @@ public function specifyTypesInCondition(
10489
return (new SpecifiedTypes([], []))->setRootExpr($expr);
10590
}
10691

107-
$exprHandler = $this->resolveExprHandler($expr);
92+
$exprHandler = ExprHandlerRegistry::resolve($expr, $this->container);
10893
if ($exprHandler !== null) {
10994
return $exprHandler->specifyTypes($this, $scope, $expr, $context);
11095
}
11196

11297
return $this->specifyDefaultTypes($scope, $expr, $context);
11398
}
11499

115-
/**
116-
* Resolves the ExprHandler for the given expression, memoizing the result by Expr class.
117-
*
118-
* @return ExprHandler<Expr>|null
119-
*/
120-
private function resolveExprHandler(Expr $expr): ?ExprHandler
121-
{
122-
// Call-likes are excluded from the cache: their handlers select on
123-
// isFirstClassCallable(), so the Expr class does not uniquely determine the handler.
124-
if (!$expr instanceof Expr\CallLike) {
125-
$cached = $this->exprHandlersByClass[get_class($expr)] ?? null;
126-
if ($cached !== null) {
127-
return $cached === false ? null : $cached;
128-
}
129-
}
130-
131-
$matchedHandler = null;
132-
/** @var ExprHandler<Expr> $exprHandler */
133-
foreach ($this->container->getServicesByTag(ExprHandler::EXTENSION_TAG) as $exprHandler) {
134-
if (!$exprHandler->supports($expr)) {
135-
continue;
136-
}
137-
138-
$matchedHandler = $exprHandler;
139-
break;
140-
}
141-
142-
if (!$expr instanceof Expr\CallLike) {
143-
$this->exprHandlersByClass[get_class($expr)] = $matchedHandler ?? false;
144-
}
145-
146-
return $matchedHandler;
147-
}
148-
149100
/** @internal */
150101
public function isNormalCountCall(FuncCall $countFuncCall, Type $typeToCount, Scope $scope): TrinaryLogic
151102
{

0 commit comments

Comments
 (0)