refactor(Php): 优化函数调用解析器并重构测试代码

- 引入 getArg 函数简化参数解析逻辑
- 为 abs、ord、chr 和 array_key_exists 函数添加优化支持
- 激活 micro_bench.php 中被注释的性能测试用例
- 为 LeetCode 示例文件添加 main 函数结构
- 在单词接龙算法中改进条件判断的可读性
- 移除 PHP 标签并优化二叉树遍历的条件表达式
pull/1/head v0.0.1
韩天峰 7 months ago
parent 1596c58da6
commit 2963e0b4ad
  1. 36
      examples/micro_bench.php
  2. 16
      src/Php/FuncCallOptimizer.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();
}

@ -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;

Loading…
Cancel
Save