Skip to content

Commit 25d54e8

Browse files
authored
[perf] Skip wrapBinaryOpWithBrackets() for CallLike nodes on BetterStandardPrinter::p() (#8890)
1 parent 6e58253 commit 25d54e8

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for skipping wrapBinaryOpWithBrackets() on CallLike nodes in BetterStandardPrinter::p().
7+
*
8+
* wrapBinaryOpWithBrackets() is a no-op for CallLike nodes, yet it ran its ExprAnalyzer
9+
* instanceof chain for every printed call. The source is call-heavy and printed fresh
10+
* (no original tokens), so every node goes through p().
11+
*
12+
* Run on both branches to compare:
13+
* php bench/better_standard_printer_call_like_bench.php
14+
*
15+
* Reports CPU time (user+sys); CPU is the reliable metric, wall is noisy under concurrent load.
16+
*/
17+
18+
use PhpParser\ParserFactory;
19+
use Rector\DependencyInjection\LazyContainerFactory;
20+
use Rector\PhpParser\Printer\BetterStandardPrinter;
21+
22+
require __DIR__ . '/../vendor/autoload.php';
23+
24+
const STATEMENTS = 500; // pairs of call-heavy + binary-op statements
25+
const REPEAT = 300; // prints
26+
27+
$rectorConfig = (new LazyContainerFactory())->create();
28+
$rectorConfig->boot();
29+
30+
/** @var BetterStandardPrinter $betterStandardPrinter */
31+
$betterStandardPrinter = $rectorConfig->make(BetterStandardPrinter::class);
32+
33+
$source = "<?php\n\n";
34+
for ($i = 0; $i < STATEMENTS; ++$i) {
35+
$source .= "\$a{$i} = foo(\$x->bar(baz(\$i, qux(1, 2)), Some::call(\$y)), new Obj(\$z->m()));\n";
36+
$source .= "\$b{$i} = \$c + \$d * strlen(\$e) && ! \$f;\n";
37+
}
38+
39+
$stmts = (new ParserFactory())->createForNewestSupportedVersion()->parse($source);
40+
41+
$cpuSeconds = static function (): float {
42+
$usage = getrusage();
43+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
44+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
45+
};
46+
47+
// warm up autoloading once, excluded from timing; output hash guards against behaviour change
48+
$printed = $betterStandardPrinter->prettyPrintFile($stmts);
49+
50+
$cpuStart = $cpuSeconds();
51+
for ($i = 0; $i < REPEAT; ++$i) {
52+
$betterStandardPrinter->prettyPrintFile($stmts);
53+
}
54+
55+
$cpu = $cpuSeconds() - $cpuStart;
56+
57+
printf("statements: %d\n", STATEMENTS * 2);
58+
printf("prints: %d\n", REPEAT);
59+
printf("CPU total: %.3f s\n", $cpu);
60+
printf("CPU per print: %.3f ms\n", $cpu / REPEAT * 1000);
61+
printf("output md5: %s\n", md5($printed));

‎src/PhpParser/Printer/BetterStandardPrinter.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ protected function p(
139139
int $lhsPrecedence = self::MAX_PRECEDENCE,
140140
bool $parentFormatPreserved = false
141141
): string {
142-
$this->wrapBinaryOpWithBrackets($node);
143-
144-
$content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);
145-
146142
// remove once its fixed in php-parser, https://github.com/nikic/PHP-Parser/pull/1126
147143
if ($node instanceof CallLike) {
148144
$this->cleanVariadicPlaceHolderTrailingComma($node);
145+
} else {
146+
$this->wrapBinaryOpWithBrackets($node);
149147
}
150148

149+
$content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);
150+
151151
if ($node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES)) {
152152
return '(' . $content . ')';
153153
}

0 commit comments

Comments
 (0)