feat(php): 优化函数调用并添加类存在性检查支持

- 实现 class_exists 函数的静态分析优化,直接返回已知类的存在性
- 添加 get_class 函数支持,为类型化对象生成字符指针
- 优化 function_exists 函数检查逻辑,提前处理原生函数
- 添加 get_class 测试用例验证功能
- 更新基准测试代码,添加 class_exists 性能测试
- 移除未使用的宏定义,优化常量定义方式
pull/1/head
韩天峰 5 months ago
parent af42804d8f
commit 26a0439f42
  1. 9
      examples/micro_bench.php
  2. 20
      src/Php/FuncCallOptimizer.php
  3. 17
      tests/aot/functions/get_class.phpt

@ -18,8 +18,10 @@ function hallo2() {
function simpleicall($n)
{
for ($i = 0; $i < $n; $i++)
$ret = function_exists('hallo');
for ($i = 0; $i < $n; $i++) {
// $ret = function_exists('hallo');
$ret = class_exists('Foo2');
}
}
class Foo {
@ -169,7 +171,6 @@ function create_object($n) {
function read_const($n) {
for ($i = 0; $i < $n; ++$i) {
// $x = TEST;
$x = TEST_CONST_2;
}
}
@ -280,11 +281,11 @@ function total()
const N = 5000000;
const TEST = null;
const TEST_CONST_2 = 1999;
function main()
{
global $total, $last_time, $g_var;
define('TEST_CONST_2', 1999);
$g_var = 0;
$t0 = $t = start_test();
empty_loop(N);

@ -109,6 +109,18 @@ trait FuncCallOptimizer
if ($name === 'function_exists') {
return $this->genFunctionExists($name, $expr);
}
if ($name === 'class_exists') {
$className = $expr->args[0]->value;
if ($this->isScalarString($className) and $this->hasClass($className->value)) {
return 'true';
}
}
if ($name === 'get_class') {
$object = $expr->args[0]->value;
if ($this->isVarExpr($object) and $this->isTypedObject($object->name)) {
return $this->genCharPtr($this->getObjectType($object->name));
}
}
return false;
}
@ -128,9 +140,15 @@ trait FuncCallOptimizer
{
$funcName = $expr->args[0]->value;
if ($this->isScalarString($funcName)) {
$funcName = $this->getLiteralString(strtolower(trim($funcName->value, '\\')));
$nameLower = strtolower(trim($funcName->value, '\\'));
if ($this->findNativeFunction($nameLower)) {
return 'true';
}
$funcName = $this->getLiteralString($nameLower);
return 'php::fn::function_exists(' . $funcName . ', true)';
}
return 'php::fn::function_exists(' . $this->parseIdentifier($funcName) . ')';
}
}

@ -0,0 +1,17 @@
--TEST--
class const 001
--FILE--
<?php
class Worker
{
public const BAR = "string constant";
}
function main()
{
$o = new Worker;
var_dump(get_class($o));
}
?>
--EXPECT--
string(6) "Worker"
Loading…
Cancel
Save