diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 7829231448..296e3f1e7e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -917,8 +917,12 @@ public function getScopeNativeType(Expr $expr): Type private function getNodeKey(Expr $node): string { - $key = $this->exprPrinter->printExpr($node); + // perf optimize for the most common path + if ($node instanceof Variable && !$node->name instanceof Expr) { + return '$' . $node->name; + } + $key = $this->exprPrinter->printExpr($node); $attributes = $node->getAttributes(); if ( $node instanceof Node\FunctionLike diff --git a/src/Node/Printer/ExprPrinter.php b/src/Node/Printer/ExprPrinter.php index 8d31c9344d..33de36f211 100644 --- a/src/Node/Printer/ExprPrinter.php +++ b/src/Node/Printer/ExprPrinter.php @@ -20,6 +20,11 @@ public function __construct(private Printer $printer) public function printExpr(Expr $expr): string { + // perf optimize for the most common path + if ($expr instanceof Expr\Variable && !$expr->name instanceof Expr) { + return '$' . $expr->name; + } + /** @var string|null $exprString */ $exprString = $expr->getAttribute(self::ATTRIBUTE_CACHE_KEY); if ($exprString === null) {