From bb9a962b50823144520e8693187492740043d756 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 21 Oct 2026 08:30:06 +0200 Subject: [PATCH] [perf] Check method name before type inference in ExecuteQueryParamsToBindValueRector --- .../execute_query_params_name_guard_bench.php | 86 +++++++++++++++++++ .../ExecuteQueryParamsToBindValueRector.php | 5 +- 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 bench/execute_query_params_name_guard_bench.php diff --git a/bench/execute_query_params_name_guard_bench.php b/bench/execute_query_params_name_guard_bench.php new file mode 100644 index 00000000..fafcccce --- /dev/null +++ b/bench/execute_query_params_name_guard_bench.php @@ -0,0 +1,86 @@ +create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +/** @var BetterNodeFinder $betterNodeFinder */ +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); + +$rector = $rectorConfig->make(ExecuteQueryParamsToBindValueRector::class); + +$source = "handle{$i}(\$value, {$i});\n"; +} +$source .= " }\n}\n"; + +$sourceFilePath = tempnam(sys_get_temp_dir(), 'bench_') . '.php'; +file_put_contents($sourceFilePath, $source); + +$stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); + +/** @var ClassMethod[] $classMethods */ +$classMethods = $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class); + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +$run = static function () use ($rector, $classMethods): void { + foreach ($classMethods as $classMethod) { + $rector->refactor($classMethod); + } +}; + +$run(); + +$wallStart = microtime(true); +$cpuStart = $cpuSeconds(); + +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $run(); +} + +$cpu = $cpuSeconds() - $cpuStart; +$wall = microtime(true) - $wallStart; + +@unlink($sourceFilePath); + +$totalCalls = REPEAT * CALLS; + +printf('body calls: %d, repeats: %d, inner method calls visited: %d%s', CALLS, REPEAT, $totalCalls, PHP_EOL); +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); +printf('wall: %.4f s%s', $wall, PHP_EOL); diff --git a/rules/Dbal40/Rector/StmtsAwareInterface/ExecuteQueryParamsToBindValueRector.php b/rules/Dbal40/Rector/StmtsAwareInterface/ExecuteQueryParamsToBindValueRector.php index dfeb8762..e51bf71b 100644 --- a/rules/Dbal40/Rector/StmtsAwareInterface/ExecuteQueryParamsToBindValueRector.php +++ b/rules/Dbal40/Rector/StmtsAwareInterface/ExecuteQueryParamsToBindValueRector.php @@ -101,11 +101,12 @@ public function refactor(Node $node): ?Node return false; } - if (! $this->isObjectType($node->var, $objectType)) { + // cheap name check first - a non-matching call bails before any type inference + if (! $this->isName($node->name, 'executeQuery')) { return false; } - if (! $this->isName($node->name, 'executeQuery')) { + if (! $this->isObjectType($node->var, $objectType)) { return false; }