Skip to content

Commit 1349fbf

Browse files
ondrejmirtesclaude
andcommitted
Cache read-variable names on the AST node
askScopeVariableStateMatches() traversed the expression's whole subtree per result - O(depth) per chain link, recreated every loop-convergence pass. On tests/bench/data/nullsafe-chain-walk.php the traversal was 43% of the run (NodeTraverser::traverseNode 22x the self-cost of 2.2.x). The names are pure syntax, so they cache on the node as an attribute (sharing the node's lifetime) and each link composes its set from its child's cached set in O(1) amortized. Restores a27ece6, dropped with the consume/splice arc removal; the read set keeps the current semantics (variables only, $this excluded, closure use() names only). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R7pqAkCx4xP6WYxBo2nMJE
1 parent 0dfcae6 commit 1349fbf

1 file changed

Lines changed: 55 additions & 31 deletions

File tree

‎src/Analyser/ExpressionResult.php‎

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace PHPStan\Analyser;
44

5-
use Override;
65
use PhpParser\Node;
76
use PhpParser\Node\Expr;
8-
use PhpParser\NodeTraverser;
9-
use PhpParser\NodeVisitor;
10-
use PhpParser\NodeVisitorAbstract;
117
use PHPStan\Analyser\Traverser\VoidToNullTraverser;
128
use PHPStan\DependencyInjection\AutowiredExtensions;
139
use PHPStan\DependencyInjection\ExtensionsCollection;
@@ -19,6 +15,7 @@
1915
use PHPStan\Type\TypeUtils;
2016
use PHPStan\Type\UnionType;
2117
use function array_keys;
18+
use function is_array;
2219
use function is_string;
2320
use function spl_object_id;
2421

@@ -769,44 +766,71 @@ public function onNonNullabilityDevicedScopes(MutatingScope $beforeScope, Mutati
769766
*/
770767
private function getReadVariableNames(): array
771768
{
772-
if ($this->readVariableNames !== null) {
773-
return $this->readVariableNames;
774-
}
769+
return $this->readVariableNames ??= self::collectReadVariableNames($this->expr);
770+
}
775771

776-
$visitor = new class extends NodeVisitorAbstract {
772+
/**
773+
* The names are pure syntax, so they cache on the AST node itself as an
774+
* attribute (sharing the node's own lifetime) - a deep fetch/call chain
775+
* composes each link's set from its child's cached set in O(1) amortized
776+
* instead of re-traversing the whole subtree per link (and per
777+
* loop-convergence pass, which recreates the results).
778+
*/
779+
private const READ_VARIABLE_NAMES_ATTRIBUTE = 'readVariableNames';
777780

778-
/** @var array<string, true> */
779-
public array $names = [];
781+
/**
782+
* @return list<string>
783+
*/
784+
private static function collectReadVariableNames(Node $node): array
785+
{
786+
if ($node instanceof Expr) {
787+
/** @var list<string>|null $cached */
788+
$cached = $node->getAttribute(self::READ_VARIABLE_NAMES_ATTRIBUTE);
789+
if ($cached !== null) {
790+
return $cached;
791+
}
792+
}
780793

781-
#[Override]
782-
public function enterNode(Node $node): ?int
783-
{
784-
if ($node instanceof Expr\Variable && is_string($node->name) && $node->name !== 'this') {
785-
$this->names[$node->name] = true;
794+
$names = [];
795+
if ($node instanceof Expr\Variable && is_string($node->name) && $node->name !== 'this') {
796+
$names[$node->name] = true;
797+
}
798+
if ($node instanceof Expr\Closure) {
799+
// a closure body's variables live in its own scope - only the
800+
// use() clause reads the enclosing position. Arrow functions
801+
// capture implicitly and are traversed.
802+
foreach ($node->uses as $use) {
803+
if (!is_string($use->var->name)) {
804+
continue;
786805
}
787-
// a closure body's variables live in its own scope - only the
788-
// use() clause reads the enclosing position. Arrow functions
789-
// capture implicitly and are traversed.
790-
if ($node instanceof Expr\Closure) {
791-
foreach ($node->uses as $use) {
792-
if (!is_string($use->var->name)) {
806+
$names[$use->var->name] = true;
807+
}
808+
} else {
809+
foreach ($node->getSubNodeNames() as $subNodeName) {
810+
$subNode = $node->$subNodeName;
811+
if ($subNode instanceof Node) {
812+
foreach (self::collectReadVariableNames($subNode) as $name) {
813+
$names[$name] = true;
814+
}
815+
} elseif (is_array($subNode)) {
816+
foreach ($subNode as $item) {
817+
if (!$item instanceof Node) {
793818
continue;
794819
}
795-
$this->names[$use->var->name] = true;
820+
foreach (self::collectReadVariableNames($item) as $name) {
821+
$names[$name] = true;
822+
}
796823
}
797-
798-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
799824
}
800-
801-
return null;
802825
}
826+
}
803827

804-
};
805-
$traverser = new NodeTraverser();
806-
$traverser->addVisitor($visitor);
807-
$traverser->traverse([$this->expr]);
828+
$result = array_keys($names);
829+
if ($node instanceof Expr) {
830+
$node->setAttribute(self::READ_VARIABLE_NAMES_ATTRIBUTE, $result);
831+
}
808832

809-
return $this->readVariableNames = array_keys($visitor->names);
833+
return $result;
810834
}
811835

812836
}

0 commit comments

Comments
 (0)