Skip to content

Commit ada148e

Browse files
authored
[perf] Read asserted class name off node in RemoveDeadInstanceOfAssertRector (#8726)
1 parent b595739 commit ada148e

3 files changed

Lines changed: 129 additions & 1 deletion

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Benchmark for RemoveDeadInstanceOfAssertRector.
7+
*
8+
* The rule removes assert($x instanceof Foo) when $x is already of type Foo. To get the asserted
9+
* class type it read getType($instanceof->class), which runs the type resolver over a class name
10+
* node whose name is already known.
11+
*
12+
* The change reads a concrete class name straight off the node (new ObjectType($name)); the
13+
* equals()/isSuperTypeOf() comparison against the expression type is unchanged, so the decision is
14+
* identical. self/static/parent depend on the enclosing class, so those fall back to getType().
15+
*
16+
* This bench parses a file of many functions, each with a typed param and a matching dead
17+
* instanceof assert, attaches scope (as the real run does), then drives refactor() over every
18+
* statement-aware node.
19+
*
20+
* Run on both branches to compare:
21+
* php bench/remove_dead_instanceof_assert_name_type_bench.php
22+
*
23+
* Reports CPU time (user+sys) and wall time; CPU is the reliable metric, wall is noisy under
24+
* concurrent load.
25+
*/
26+
27+
use PhpParser\Node\Stmt\Function_;
28+
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveDeadInstanceOfAssertRector;
29+
use Rector\DependencyInjection\LazyContainerFactory;
30+
use Rector\PhpParser\Node\BetterNodeFinder;
31+
use Rector\Testing\TestingParser\TestingParser;
32+
33+
require __DIR__ . '/../vendor/autoload.php';
34+
35+
const FUNCTIONS = 150; // functions with a dead instanceof assert
36+
const REPEAT = 60; // outer repeats over the whole set
37+
38+
$rectorConfig = (new LazyContainerFactory())->create();
39+
$rectorConfig->boot();
40+
41+
/** @var TestingParser $testingParser */
42+
$testingParser = $rectorConfig->make(TestingParser::class);
43+
44+
/** @var BetterNodeFinder $betterNodeFinder */
45+
$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class);
46+
47+
$rector = $rectorConfig->make(RemoveDeadInstanceOfAssertRector::class);
48+
49+
$source = "<?php\n\nnamespace Bench;\n\n";
50+
for ($i = 0; $i < FUNCTIONS; ++$i) {
51+
$source .= "function run{$i}(\\DateTime \$value): void\n{\n";
52+
$source .= " assert(\$value instanceof \\DateTime);\n}\n\n";
53+
}
54+
55+
$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php';
56+
file_put_contents($sourceFilePath, $source);
57+
58+
$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath);
59+
60+
/** @var Function_[] $functions */
61+
$functions = $betterNodeFinder->findInstanceOf($stmts, Function_::class);
62+
63+
$cpuSeconds = static function (): float {
64+
$usage = getrusage();
65+
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000
66+
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000;
67+
};
68+
69+
$run = static function () use ($rector, $functions): void {
70+
foreach ($functions as $function) {
71+
// clone so the removal in one pass does not empty the body for the next repeat
72+
$rector->refactor(clone $function);
73+
}
74+
};
75+
76+
// warm up autoloading/reflection caches once, excluded from timing
77+
$run();
78+
79+
$wallStart = microtime(true);
80+
$cpuStart = $cpuSeconds();
81+
82+
for ($repeat = 0; $repeat < REPEAT; ++$repeat) {
83+
$run();
84+
}
85+
86+
$cpu = $cpuSeconds() - $cpuStart;
87+
$wall = microtime(true) - $wallStart;
88+
89+
@unlink($sourceFilePath);
90+
91+
$totalCalls = REPEAT * count($functions);
92+
93+
printf('functions: %d, repeats: %d, refactor() calls: %d%s', count($functions), REPEAT, $totalCalls, PHP_EOL);
94+
printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL);
95+
printf('wall: %.4f s%s', $wall, PHP_EOL);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveDeadInstanceOfAssertRector\Fixture;
4+
5+
final class SkipSelfInstanceof
6+
{
7+
private self $item;
8+
9+
public function run(): void
10+
{
11+
assert($this->item instanceof self);
12+
}
13+
}

‎rules/DeadCode/Rector/StmtsAwareInterface/RemoveDeadInstanceOfAssertRector.php‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use PhpParser\Node\Name;
1515
use PhpParser\Node\Stmt\Expression;
1616
use PhpParser\Node\Stmt\Nop;
17+
use PHPStan\Type\ObjectType;
18+
use PHPStan\Type\Type;
19+
use Rector\Enum\ObjectReference;
1720
use Rector\PhpParser\Enum\NodeGroup;
1821
use Rector\Rector\AbstractRector;
1922
use Rector\RuleDoc\CodeSample\CodeSample;
@@ -143,7 +146,7 @@ private function isDeadInstanceOfAssert(Expr $expr): bool
143146
return false;
144147
}
145148

146-
$classType = $this->getType($instanceof->class);
149+
$classType = $this->resolveClassNameType($instanceof->class);
147150
$exprType = $this->getType($instanceof->expr);
148151
if ($classType->equals($exprType)) {
149152
return true;
@@ -152,4 +155,21 @@ private function isDeadInstanceOfAssert(Expr $expr): bool
152155
return $classType->isSuperTypeOf($exprType)
153156
->yes();
154157
}
158+
159+
private function resolveClassNameType(Name $name): Type
160+
{
161+
// a concrete class name is resolvable straight from the node, no type inference needed;
162+
// self/static/parent depend on the enclosing class, so fall back to scope-based type
163+
$className = $this->getName($name);
164+
if ($className === null) {
165+
return $this->getType($name);
166+
}
167+
168+
$loweredClassName = strtolower($className);
169+
if (in_array($loweredClassName, [ObjectReference::SELF, ObjectReference::STATIC, ObjectReference::PARENT], true)) {
170+
return $this->getType($name);
171+
}
172+
173+
return new ObjectType($className);
174+
}
155175
}

0 commit comments

Comments
 (0)