From 2963e0b4adf25b2a25ac4ad21402525438a04f84 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 31 Jan 2026 16:35:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Php):=20=E4=BC=98=E5=8C=96=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=B0=83=E7=94=A8=E8=A7=A3=E6=9E=90=E5=99=A8=E5=B9=B6?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 getArg 函数简化参数解析逻辑 - 为 abs、ord、chr 和 array_key_exists 函数添加优化支持 - 激活 micro_bench.php 中被注释的性能测试用例 - 为 LeetCode 示例文件添加 main 函数结构 - 在单词接龙算法中改进条件判断的可读性 - 移除 PHP 标签并优化二叉树遍历的条件表达式 --- examples/micro_bench.php | 36 +++++++++++++++++------------------ src/Php/FuncCallOptimizer.php | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/examples/micro_bench.php b/examples/micro_bench.php index 1ff5a409..27c9af67 100644 --- a/examples/micro_bench.php +++ b/examples/micro_bench.php @@ -339,25 +339,25 @@ function main() $t = end_test($t, '$this->f()', $overhead); $x->read_const(N); $t = end_test($t, '$x = Foo::TEST', $overhead); -// create_object(N); -// $t = end_test($t, 'new Foo()', $overhead); + create_object(N); + $t = end_test($t, 'new Foo()', $overhead); read_const(N); $t = end_test($t, '$x = TEST', $overhead); -// read_auto_global(N); -// $t = end_test($t, '$x = $_GET', $overhead); -// read_global_var(N); -// $t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead); -// read_hash(N); -// $t = end_test($t, '$x = $hash[\'v\']', $overhead); -// read_str_offset(N); -// $t = end_test($t, '$x = $str[0]', $overhead); -// issetor(N); -// $t = end_test($t, '$x = $a ?: null', $overhead); -// issetor2(N); -// $t = end_test($t, '$x = $f ?: tmp', $overhead); -// ternary(N); -// $t = end_test($t, '$x = $f ? $f : $a', $overhead); -// ternary2(N); -// $t = end_test($t, '$x = $f ? $f : tmp', $overhead); + read_auto_global(N); + $t = end_test($t, '$x = $_GET', $overhead); + read_global_var(N); + $t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead); + read_hash(N); + $t = end_test($t, '$x = $hash[\'v\']', $overhead); + read_str_offset(N); + $t = end_test($t, '$x = $str[0]', $overhead); + issetor(N); + $t = end_test($t, '$x = $a ?: null', $overhead); + issetor2(N); + $t = end_test($t, '$x = $f ?: tmp', $overhead); + ternary(N); + $t = end_test($t, '$x = $f ? $f : $a', $overhead); + ternary2(N); + $t = end_test($t, '$x = $f ? $f : tmp', $overhead); total(); } diff --git a/src/Php/FuncCallOptimizer.php b/src/Php/FuncCallOptimizer.php index 68276547..ac16f2b6 100644 --- a/src/Php/FuncCallOptimizer.php +++ b/src/Php/FuncCallOptimizer.php @@ -14,8 +14,11 @@ trait FuncCallOptimizer { protected function parseFuncCallWithOptimizer(string $name, Node\Expr\FuncCall $expr): string|false { + $getArg = function ($i) use ($expr) { + return $this->parseIdentifier($expr->args[$i]->value); + }; if ($name === 'strlen' or $name === 'sizeof' or $name === 'count') { - return 'php::len(' . $this->parseIdentifier($expr->args[0]->value) . ')'; + return 'php::len(' . $getArg(0) . ')'; } if (count($expr->args) == 1) { switch ($name) { @@ -42,7 +45,16 @@ trait FuncCallOptimizer } } if ($name === 'abs') { - return 'php::math::abs(' . $this->parseIdentifier($expr->args[0]->value) . ')'; + return 'php::math::abs(' . $getArg(0) . ')'; + } + if ($name === 'ord') { + return 'php::fn::ord(' . $getArg(0) . ')'; + } + if ($name === 'chr') { + return 'php::fn::chr(' . $this->convertIntExpr($getArg(0)) . ')'; + } + if ($name === 'array_key_exists') { + return $getArg(1) . '.offsetExists(' . $getArg(0) . ')'; } if ($name === 'function_exists') { $funcName = $expr->args[0]->value;