Skip to content

Commit e1d1818

Browse files
authored
[perf] Avoid nested traversals in ContextNodeVisitor by tracking loop/try/attribute depth (#8741)
1 parent 34821fd commit e1d1818

2 files changed

Lines changed: 201 additions & 45 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for ContextNodeVisitor.
7+
*
8+
* The visitor runs inside the decorating traversal, yet it started a nested traversal of its own
9+
* for every loop/switch (to mark If_/Break_ in the body), every try block (to mark the try stmts)
10+
* and every attribute (to mark Array_/Closure in its args). Nested loops and try blocks are
11+
* re-walked once per enclosing loop/try, and each try block built a new NodeTraverser plus an
12+
* anonymous visitor.
13+
*
14+
* The optimization tracks loop/try/attribute depth in enterNode()/leaveNode() of the main
15+
* traversal instead, so every node is visited once.
16+
*
17+
* This bench parses a class with methods full of nested loops, switch, try/catch and attributes,
18+
* then traverses it with ContextNodeVisitor only.
19+
*
20+
* Run on both branches to compare:
21+
* php bench/context_node_visitor_bench.php
22+
*
23+
* Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy
24+
* under concurrent load.
25+
*/
26+
27+
use PhpParser\NodeTraverser;
28+
use PhpParser\ParserFactory;
29+
use Rector\DependencyInjection\LazyContainerFactory;
30+
use Rector\PhpParser\NodeVisitor\ContextNodeVisitor;
31+
32+
require __DIR__ . '/../vendor/autoload.php';
33+
34+
const METHODS = 60; // methods, each with nested loops, switch, try/catch
35+
const REPEAT = 200; // outer repeats over the whole file
36+
37+
$rectorConfig = new LazyContainerFactory()->create();
38+
$rectorConfig->boot();
39+
40+
$contextNodeVisitor = $rectorConfig->make(ContextNodeVisitor::class);
41+
42+
$source = "<?php\n\nnamespace Bench;\n\nclass SomeClass\n{\n";
43+
for ($i = 0; $i < METHODS; ++$i) {
44+
$source .= " #[Route(path: '/run{$i}', methods: ['GET', 'POST'], options: ['a' => [1, 2]])]\n";
45+
$source .= " public function run{$i}(array \$items, int \$mode)\n {\n";
46+
$source .= " try {\n";
47+
$source .= " foreach (\$items as \$key => \$item) {\n";
48+
$source .= " foreach (\$item as \$value) {\n";
49+
$source .= " if (\$value === null) {\n";
50+
$source .= " break;\n";
51+
$source .= " }\n";
52+
$source .= " for (\$j = 0; \$j < 3; ++\$j) {\n";
53+
$source .= " switch (\$mode) {\n";
54+
$source .= " case 1:\n";
55+
$source .= " \$this->handle(\$key, \$value, [\$j, \$mode]);\n";
56+
$source .= " break;\n";
57+
$source .= " default:\n";
58+
$source .= " if (\$j > 1) {\n";
59+
$source .= " break 2;\n";
60+
$source .= " }\n";
61+
$source .= " }\n";
62+
$source .= " }\n";
63+
$source .= " }\n";
64+
$source .= " }\n";
65+
$source .= " try {\n";
66+
$source .= " \$result = \$this->compute(\$items[0] ?? null, \$mode + 1);\n";
67+
$source .= " } catch (\\RuntimeException \$e) {\n";
68+
$source .= " \$result = null;\n";
69+
$source .= " }\n";
70+
$source .= " } catch (\\Throwable \$throwable) {\n";
71+
$source .= " return null;\n";
72+
$source .= " } finally {\n";
73+
$source .= " \$this->cleanup();\n";
74+
$source .= " }\n\n";
75+
$source .= " return \$result;\n";
76+
$source .= " }\n\n";
77+
}
78+
79+
$source .= "}\n";
80+
81+
$stmts = new ParserFactory()->createForHostVersion()->parse($source);
82+
83+
$nodeTraverser = new NodeTraverser($contextNodeVisitor);
84+
85+
$cpuSeconds = static function (): float {
86+
$usage = getrusage();
87+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
88+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
89+
};
90+
91+
// warm up once, excluded from timing
92+
$nodeTraverser->traverse($stmts);
93+
94+
$wallStart = microtime(true);
95+
$cpuStart = $cpuSeconds();
96+
97+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
98+
$nodeTraverser->traverse($stmts);
99+
}
100+
101+
$cpu = $cpuSeconds() - $cpuStart;
102+
$wall = microtime(true) - $wallStart;
103+
104+
printf('methods: %d, repeats: %d%s', METHODS, REPEAT, PHP_EOL);
105+
printf('CPU: %.4f s (%.3f ms/traverse)%s', $cpu, $cpu / REPEAT * 1000, PHP_EOL);
106+
printf('wall: %.4f s%s', $wall, PHP_EOL);

‎src/PhpParser/NodeVisitor/ContextNodeVisitor.php‎

Lines changed: 95 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,66 @@
2121
use PhpParser\Node\Expr\PropertyFetch;
2222
use PhpParser\Node\Expr\StaticPropertyFetch;
2323
use PhpParser\Node\Expr\Variable;
24+
use PhpParser\Node\FunctionLike;
2425
use PhpParser\Node\Identifier;
2526
use PhpParser\Node\Name\FullyQualified;
2627
use PhpParser\Node\Param;
2728
use PhpParser\Node\Scalar\String_;
2829
use PhpParser\Node\Stmt\Break_;
30+
use PhpParser\Node\Stmt\Catch_;
2931
use PhpParser\Node\Stmt\Class_;
3032
use PhpParser\Node\Stmt\Do_;
3133
use PhpParser\Node\Stmt\Else_;
3234
use PhpParser\Node\Stmt\ElseIf_;
35+
use PhpParser\Node\Stmt\Finally_;
3336
use PhpParser\Node\Stmt\For_;
3437
use PhpParser\Node\Stmt\Foreach_;
35-
use PhpParser\Node\Stmt\Function_;
3638
use PhpParser\Node\Stmt\If_;
3739
use PhpParser\Node\Stmt\Switch_;
3840
use PhpParser\Node\Stmt\TryCatch;
3941
use PhpParser\Node\Stmt\Unset_;
4042
use PhpParser\Node\Stmt\While_;
41-
use PhpParser\NodeVisitor;
4243
use PhpParser\NodeVisitorAbstract;
4344
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
4445
use Rector\NodeTypeResolver\Node\AttributeKey;
45-
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
46-
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
4746

4847
final class ContextNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
4948
{
50-
public function __construct(
51-
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
52-
) {
49+
/**
50+
* Context is tracked by depth during the main traversal, instead of traversing
51+
* the loop, try and attribute children again on every enter
52+
*/
53+
private int $loopDepth = 0;
54+
55+
private int $tryDepth = 0;
56+
57+
private int $attributeDepth = 0;
58+
59+
/**
60+
* @var array<array{int, int}>
61+
*/
62+
private array $outerLoopAndTryDepths = [];
63+
64+
/**
65+
* @param Node[] $nodes
66+
*/
67+
public function beforeTraverse(array $nodes): null
68+
{
69+
$this->loopDepth = 0;
70+
$this->tryDepth = 0;
71+
$this->attributeDepth = 0;
72+
$this->outerLoopAndTryDepths = [];
73+
74+
return null;
5375
}
5476

5577
public function enterNode(Node $node): ?Node
5678
{
79+
$this->decorateEnclosingContext($node);
80+
5781
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
5882
$this->processContextInLoop($node);
83+
++$this->loopDepth;
5984
return null;
6085
}
6186

@@ -73,8 +98,7 @@ public function enterNode(Node $node): ?Node
7398
}
7499

75100
if ($node instanceof TryCatch) {
76-
SimpleNodeTraverser::decorateWithAttributeValue($node->stmts, AttributeKey::IS_IN_TRY_BLOCK, true);
77-
101+
++$this->tryDepth;
78102
return null;
79103
}
80104

@@ -87,7 +111,7 @@ public function enterNode(Node $node): ?Node
87111
}
88112

89113
if ($node instanceof Attribute) {
90-
$this->processContextInAttribute($node);
114+
++$this->attributeDepth;
91115
return null;
92116
}
93117

@@ -134,6 +158,67 @@ public function enterNode(Node $node): ?Node
134158
return null;
135159
}
136160

161+
public function leaveNode(Node $node): null
162+
{
163+
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
164+
--$this->loopDepth;
165+
return null;
166+
}
167+
168+
if ($node instanceof TryCatch) {
169+
--$this->tryDepth;
170+
return null;
171+
}
172+
173+
// back to the try stmts of the parent TryCatch, see decorateEnclosingContext()
174+
if ($node instanceof Catch_ || $node instanceof Finally_) {
175+
++$this->tryDepth;
176+
return null;
177+
}
178+
179+
if ($node instanceof Attribute) {
180+
--$this->attributeDepth;
181+
return null;
182+
}
183+
184+
if ($node instanceof Class_ || $node instanceof FunctionLike) {
185+
[$this->loopDepth, $this->tryDepth] = array_pop($this->outerLoopAndTryDepths) ?? [0, 0];
186+
}
187+
188+
return null;
189+
}
190+
191+
private function decorateEnclosingContext(Node $node): void
192+
{
193+
// catch and finally are not part of the try block of their parent TryCatch
194+
if ($node instanceof Catch_ || $node instanceof Finally_) {
195+
--$this->tryDepth;
196+
}
197+
198+
// nested class or function-like does not belong to outer loop or try block
199+
if ($node instanceof Class_ || $node instanceof FunctionLike) {
200+
$this->outerLoopAndTryDepths[] = [$this->loopDepth, $this->tryDepth];
201+
$this->loopDepth = 0;
202+
$this->tryDepth = 0;
203+
} elseif ($this->tryDepth > 0) {
204+
$node->setAttribute(AttributeKey::IS_IN_TRY_BLOCK, true);
205+
}
206+
207+
if ($this->loopDepth > 0 && ($node instanceof If_ || $node instanceof Break_)) {
208+
$node->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true);
209+
}
210+
211+
if ($this->attributeDepth > 0) {
212+
if ($node instanceof Array_) {
213+
$node->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
214+
}
215+
216+
if ($node instanceof Closure) {
217+
$node->setAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE, true);
218+
}
219+
}
220+
}
221+
137222
private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void
138223
{
139224
if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {
@@ -203,24 +288,6 @@ private function processSetFactoryArgument(MethodCall $methodCall): void
203288
}
204289
}
205290

206-
private function processContextInAttribute(Attribute $attribute): void
207-
{
208-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
209-
$attribute->args,
210-
static function (Node $subNode): null {
211-
if ($subNode instanceof Array_) {
212-
$subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
213-
}
214-
215-
if ($subNode instanceof Closure) {
216-
$subNode->setAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE, true);
217-
}
218-
219-
return null;
220-
}
221-
);
222-
}
223-
224291
private function processContextInIf(If_|Else_|ElseIf_ $node): void
225292
{
226293
foreach ($node->stmts as $stmt) {
@@ -239,22 +306,5 @@ private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): v
239306

240307
$node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
241308
}
242-
243-
$stmts = $node instanceof Switch_ ? $node->cases : $node->stmts;
244-
245-
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
246-
$stmts,
247-
static function (Node $subNode): ?int {
248-
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
249-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
250-
}
251-
252-
if ($subNode instanceof If_ || $subNode instanceof Break_) {
253-
$subNode->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true);
254-
}
255-
256-
return null;
257-
}
258-
);
259309
}
260310
}

0 commit comments

Comments
 (0)