Skip to content

Commit b8492b8

Browse files
authored
Faster MutatingScope->getNodeKey() (#5014)
1 parent 6f3a2a1 commit b8492b8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Analyser/MutatingScope.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,12 @@ public function getScopeNativeType(Expr $expr): Type
917917

918918
private function getNodeKey(Expr $node): string
919919
{
920-
$key = $this->exprPrinter->printExpr($node);
920+
// perf optimize for the most common path
921+
if ($node instanceof Variable && !$node->name instanceof Expr) {
922+
return '$' . $node->name;
923+
}
921924

925+
$key = $this->exprPrinter->printExpr($node);
922926
$attributes = $node->getAttributes();
923927
if (
924928
$node instanceof Node\FunctionLike

src/Node/Printer/ExprPrinter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function __construct(private Printer $printer)
2020

2121
public function printExpr(Expr $expr): string
2222
{
23+
// perf optimize for the most common path
24+
if ($expr instanceof Expr\Variable && !$expr->name instanceof Expr) {
25+
return '$' . $expr->name;
26+
}
27+
2328
/** @var string|null $exprString */
2429
$exprString = $expr->getAttribute(self::ATTRIBUTE_CACHE_KEY);
2530
if ($exprString === null) {

0 commit comments

Comments
 (0)