feat(php): 优化全局变量访问和基准测试函数类型声明

- 在CompilerBase.php中添加标量字符串维度检查,直接返回全局变量名称而不是调用php::global
- 为micro_bench.php中的read_global_var、read_hash、read_str_offset和issetor函数添加int类型声明
- 改进全局变量处理逻辑,避免不必要的php::global调用
- 提升代码类型安全性和性能表现
pull/1/head
韩天峰 5 months ago
parent 34e112d697
commit cc1131f455
  1. 8
      examples/micro_bench.php
  2. 7
      src/Php/CompilerBase.php

@ -188,27 +188,27 @@ function read_auto_global($n) {
}
}
function read_global_var($n) {
function read_global_var(int $n) {
for ($i = 0; $i < $n; ++$i) {
$x = $GLOBALS['g_var'];
}
}
function read_hash($n) {
function read_hash(int $n) {
$hash = array('test' => 0);
for ($i = 0; $i < $n; ++$i) {
$x = $hash['test'];
}
}
function read_str_offset($n) {
function read_str_offset(int $n) {
$str = "test";
for ($i = 0; $i < $n; ++$i) {
$x = $str[1];
}
}
function issetor($n) {
function issetor(int $n) {
$val = array(0,1,2,3,4,5,6,7,8,9);
for ($i = 0; $i < $n; ++$i) {
$x = $val ?: null;

@ -2284,6 +2284,13 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($node->dim === null) {
$this->fatalError($node, 'Cannot use [] for GLOBALS');
}
if ($this->isScalarString($node->dim)) {
$name = $node->dim->value;
if (!$this->hasGlobalVar($name)) {
$this->addGlobalVar($name, self::TYPE_VAR);
}
return $name;
}
return 'php::global(' . $this->parseIdentifier($node->dim) . ')';
}
if (!$this->hasVar($var)) {

Loading…
Cancel
Save