From 9eec3e7483fca41f8b7afa57345ebf06fdd1083b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 5 Jun 2026 15:49:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(aot):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E5=92=8CStmt=5FBlock?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5=E5=9D=97=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在CompilerBase中添加Stmt_Block解析支持 - 新增global-int测试用例验证全局变量整型操作 - 新增globals-null-arithmetic测试用例处理NULL全局变量算术运算 - 修复switch语句缩进格式问题 - 初始化micro_bench中last_time变量 - 注释掉部分性能测试以聚焦核心功能 --- examples/micro_bench.php | 159 +++++++++--------- src/Php/CompilerBase.php | 3 + tests/aot/basic/global-int.phpt | 51 ++++++ .../place-holder/globals-null-arithmetic.phpt | 71 ++++++++ tests/aot/switch/001.phpt | 18 +- 5 files changed, 214 insertions(+), 88 deletions(-) create mode 100644 tests/aot/basic/global-int.phpt create mode 100644 tests/aot/place-holder/globals-null-arithmetic.phpt diff --git a/examples/micro_bench.php b/examples/micro_bench.php index 223d5860..bd72558b 100644 --- a/examples/micro_bench.php +++ b/examples/micro_bench.php @@ -258,22 +258,22 @@ function start_test() function end_test($start, $name, $overhead = null) { - global $total; - global $last_time; - $end = gethrtime(); - ob_end_clean(); - $last_time = $end-$start; - $total += $last_time; - $num = number_format($last_time,3); - $pad = str_repeat(" ", 24-strlen($name)-strlen($num)); - if (is_null($overhead)) { - echo $name.$pad.$num."\n"; - } else { - $num2 = number_format($last_time - $overhead,3); - echo $name.$pad.$num." ".$num2."\n"; - } - ob_start(); - return gethrtime(); + global $total; + global $last_time; + $end = gethrtime(); + ob_end_clean(); + $last_time = $end - $start; + $total += $last_time; + $num = number_format($last_time, 3); + $pad = str_repeat(" ", 24 - strlen($name) - strlen($num)); + if (is_null($overhead)) { + echo $name . $pad . $num . "\n"; + } else { + $num2 = number_format($last_time - $overhead, 3); + echo $name . $pad . $num . " " . $num2 . "\n"; + } + ob_start(); + return gethrtime(); } function total() @@ -294,6 +294,7 @@ function main() { global $total, $last_time, $g_var; $g_var = 0; + $last_time = 0; $t0 = $t = start_test(); empty_loop(N); $t = end_test($t, 'empty_loop'); @@ -304,68 +305,68 @@ function main() $t = end_test($t, 'undef_func()', $overhead); simpleicall(N); $t = end_test($t, 'int_func()', $overhead); - Foo::read_static(N); - $t = end_test($t, '$x = self::$x', $overhead); - Foo::write_static(N); - $t = end_test($t, 'self::$x = 0', $overhead); - Foo::isset_static(N); - $t = end_test($t, 'isset(self::$x)', $overhead); - Foo::empty_static(N); - $t = end_test($t, 'empty(self::$x)', $overhead); - read_static(N); - $t = end_test($t, '$x = Foo::$x', $overhead); - write_static(N); - $t = end_test($t, 'Foo::$x = 0', $overhead); - isset_static(N); - $t = end_test($t, 'isset(Foo::$x)', $overhead); - empty_static(N); - $t = end_test($t, 'empty(Foo::$x)', $overhead); - Foo::call_static(N); - $t = end_test($t, 'self::f()', $overhead); - call_static(N); - $t = end_test($t, 'Foo::f()', $overhead); - $x = new Foo(); - $x->read_prop(N); - $t = end_test($t, '$x = $this->x', $overhead); - $x->write_prop(N); - $t = end_test($t, '$this->x = 0', $overhead); - $x->assign_add_prop(N); - $t = end_test($t, '$this->x += 2', $overhead); - $x->pre_inc_prop(N); - $t = end_test($t, '++$this->x', $overhead); - $x->pre_dec_prop(N); - $t = end_test($t, '--$this->x', $overhead); - $x->post_inc_prop(N); - $t = end_test($t, '$this->x++', $overhead); - $x->post_dec_prop(N); - $t = end_test($t, '$this->x--', $overhead); - $x->isset_prop(N); - $t = end_test($t, 'isset($this->x)', $overhead); - $x->empty_prop(N); - $t = end_test($t, 'empty($this->x)', $overhead); - $x->call(N); - $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); - 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); +// Foo::read_static(N); +// $t = end_test($t, '$x = self::$x', $overhead); +// Foo::write_static(N); +// $t = end_test($t, 'self::$x = 0', $overhead); +// Foo::isset_static(N); +// $t = end_test($t, 'isset(self::$x)', $overhead); +// Foo::empty_static(N); +// $t = end_test($t, 'empty(self::$x)', $overhead); +// read_static(N); +// $t = end_test($t, '$x = Foo::$x', $overhead); +// write_static(N); +// $t = end_test($t, 'Foo::$x = 0', $overhead); +// isset_static(N); +// $t = end_test($t, 'isset(Foo::$x)', $overhead); +// empty_static(N); +// $t = end_test($t, 'empty(Foo::$x)', $overhead); +// Foo::call_static(N); +// $t = end_test($t, 'self::f()', $overhead); +// call_static(N); +// $t = end_test($t, 'Foo::f()', $overhead); +// $x = new Foo(); +// $x->read_prop(N); +// $t = end_test($t, '$x = $this->x', $overhead); +// $x->write_prop(N); +// $t = end_test($t, '$this->x = 0', $overhead); +// $x->assign_add_prop(N); +// $t = end_test($t, '$this->x += 2', $overhead); +// $x->pre_inc_prop(N); +// $t = end_test($t, '++$this->x', $overhead); +// $x->pre_dec_prop(N); +// $t = end_test($t, '--$this->x', $overhead); +// $x->post_inc_prop(N); +// $t = end_test($t, '$this->x++', $overhead); +// $x->post_dec_prop(N); +// $t = end_test($t, '$this->x--', $overhead); +// $x->isset_prop(N); +// $t = end_test($t, 'isset($this->x)', $overhead); +// $x->empty_prop(N); +// $t = end_test($t, 'empty($this->x)', $overhead); +// $x->call(N); +// $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); +// 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); total(); } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 89872cc1..05186cdb 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1505,6 +1505,9 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Stmt_TryCatch': $result = $this->parseTryCatch($v); break; + case 'Stmt_Block': + $result = $this->parseStmts($v->stmts); + break; case 'Stmt_Class': $this->fatalError($v, 'Cannot declare class in function'); break; diff --git a/tests/aot/basic/global-int.phpt b/tests/aot/basic/global-int.phpt new file mode 100644 index 00000000..9518e1e8 --- /dev/null +++ b/tests/aot/basic/global-int.phpt @@ -0,0 +1,51 @@ +--TEST-- +global vars with native types +--FILE-- + +--EXPECT-- +int(5) +int(8) +float(1.5) +float(3.75) diff --git a/tests/aot/place-holder/globals-null-arithmetic.phpt b/tests/aot/place-holder/globals-null-arithmetic.phpt new file mode 100644 index 00000000..de2ae550 --- /dev/null +++ b/tests/aot/place-holder/globals-null-arithmetic.phpt @@ -0,0 +1,71 @@ +--TEST-- +NULL globals in arithmetic should treat NULL as 0 +--FILE-- + +--EXPECT-- +float(0.575) +float(0.575) +float(-0.5) +int(5) +float(0.575) +float(0.575) +float(0.589) +float(-0.5609999999999999) diff --git a/tests/aot/switch/001.phpt b/tests/aot/switch/001.phpt index 37186432..0460525f 100644 --- a/tests/aot/switch/001.phpt +++ b/tests/aot/switch/001.phpt @@ -4,17 +4,17 @@ switch