Skip to content

Commit 471c4e6

Browse files
authored
[perf] Match array_key_first/last in a single statement traversal (#416)
1 parent 3380a5e commit 471c4e6

2 files changed

Lines changed: 94 additions & 17 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for DowngradeArrayKeyFirstLastRector.
7+
*
8+
* refactor() used to run ExprInTopStmtMatcher::match() twice per statement - one full subtree
9+
* traversal to find array_key_first(), then a second full traversal to find array_key_last(). For
10+
* the overwhelming majority of statements (which contain neither) that is two full traversals of
11+
* the statement's expression tree where one would do.
12+
*
13+
* Matching either name in a single traversal and dispatching on the matched name halves the matcher
14+
* cost on the common path.
15+
*
16+
* This bench drives refactor() over many expression statements that contain neither call - the
17+
* common case. CPU (user+sys) is the reliable metric; wall is noisy.
18+
*/
19+
20+
use PhpParser\Node\Stmt\Expression;
21+
use Rector\DependencyInjection\LazyContainerFactory;
22+
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
23+
use Rector\PhpParser\Node\BetterNodeFinder;
24+
use Rector\Testing\TestingParser\TestingParser;
25+
26+
require __DIR__ . '/../vendor/autoload.php';
27+
28+
const STMTS = 300;
29+
const REPEAT = 200;
30+
31+
$rectorConfig = (new LazyContainerFactory())->create();
32+
$rectorConfig->boot();
33+
34+
/** @var TestingParser $testingParser */
35+
$testingParser = $rectorConfig->make(TestingParser::class);
36+
37+
/** @var BetterNodeFinder $betterNodeFinder */
38+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
39+
40+
$rector = $rectorConfig->make(DowngradeArrayKeyFirstLastRector::class);
41+
42+
$source = "<?php\n\nnamespace Bench;\n\nfunction run(\$items)\n{\n";
43+
for ($i = 0; $i < STMTS; ++$i) {
44+
// statements with funccalls that are neither array_key_first nor array_key_last
45+
$source .= " \$result{$i} = array_map('trim', array_filter(\$items, 'is_string'));\n";
46+
}
47+
$source .= "}\n";
48+
49+
$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php';
50+
file_put_contents($sourceFilePath, $source);
51+
52+
$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath);
53+
54+
/** @var Expression[] $expressions */
55+
$expressions = $betterNodeFinder->findInstanceOf($stmts, Expression::class);
56+
57+
$cpuSeconds = static function (): float {
58+
$usage = getrusage();
59+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
60+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
61+
};
62+
63+
$run = static function () use ($rector, $expressions): void {
64+
foreach ($expressions as $expression) {
65+
$rector->refactor($expression);
66+
}
67+
};
68+
69+
$run();
70+
71+
$wallStart = microtime(true);
72+
$cpuStart = $cpuSeconds();
73+
74+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
75+
$run();
76+
}
77+
78+
$cpu = $cpuSeconds() - $cpuStart;
79+
$wall = microtime(true) - $wallStart;
80+
81+
@unlink($sourceFilePath);
82+
83+
$totalCalls = REPEAT * count($expressions);
84+
85+
printf('statements: %d, repeats: %d, refactor() calls: %d%s', count($expressions), REPEAT, $totalCalls, PHP_EOL);
86+
printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL);
87+
printf('wall: %.4f s%s', $wall, PHP_EOL);

‎rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,24 @@ public function getNodeTypes(): array
8989
*/
9090
public function refactor(Node $node): ?array
9191
{
92-
$exprArrayKeyFirst = $this->exprInTopStmtMatcher->match(
92+
// single subtree traversal matching either name, instead of one full traversal per name
93+
$funcCall = $this->exprInTopStmtMatcher->match(
9394
$node,
9495
function (Node $subNode): bool {
9596
if (! $subNode instanceof FuncCall) {
9697
return false;
9798
}
9899

99-
return $this->isName($subNode, 'array_key_first');
100+
return $this->isName($subNode, 'array_key_first') || $this->isName($subNode, 'array_key_last');
100101
}
101102
);
102103

103-
if ($exprArrayKeyFirst instanceof FuncCall) {
104-
return $this->refactorArrayKeyFirst($exprArrayKeyFirst, $node);
105-
}
106-
107-
$exprArrayKeyLast = $this->exprInTopStmtMatcher->match(
108-
$node,
109-
function (Node $subNode): bool {
110-
if (! $subNode instanceof FuncCall) {
111-
return false;
112-
}
113-
114-
return $this->isName($subNode, 'array_key_last');
104+
if ($funcCall instanceof FuncCall) {
105+
if ($this->isName($funcCall, 'array_key_first')) {
106+
return $this->refactorArrayKeyFirst($funcCall, $node);
115107
}
116-
);
117108

118-
if ($exprArrayKeyLast instanceof FuncCall) {
119-
return $this->refactorArrayKeyLast($exprArrayKeyLast, $node);
109+
return $this->refactorArrayKeyLast($funcCall, $node);
120110
}
121111

122112
return null;

0 commit comments

Comments
 (0)