PHP源码中@refcount注释的发现与初步分析
在研读PHP源代码的过程中,于大量的*.stub.php文件里留意到一段特殊的标注:@refcount 1。
深入查阅build/gen_stub.php的源码实现后,观察到在处理*.stub.php文件解析环节,针对返回信息的代码逻辑有所体现。
<?php
class ReturnInfo {
const REFCOUNT_0 = "0";
const REFCOUNT_1 = "1";
const REFCOUNT_N = "N";
const REFCOUNTS = [
self::REFCOUNT_0,
self::REFCOUNT_1,
self::REFCOUNT_N,
];
//...
private function setRefcount(?string $refcount): void
{
$type = $this->phpDocType ?? $this->type;
$isScalarType = $type !== null && $type->isScalar();
if ($refcount === null) {
$this->refcount = $isScalarType ? self::REFCOUNT_0 : self::REFCOUNT_N;
return;
}
if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) {
throw new Exception("@refcount must have one of the following values: \"0\", \"1\", \"N\", $refcount given");
}
if ($isScalarType && $refcount !== self::REFCOUNT_0) {
throw new Exception('A scalar return type of "' . $type->__toString() . '" must have a refcount of "' . self::REFCOUNT_0 . '"');
}
if (!$isScalarType && $refcount === self::REFCOUNT_0) {
throw new Exception('A non-scalar return type of "' . $type->__toString() . '" cannot have a refcount of "' . self::REFCOUNT_0 . '"');
}
$this->refcount = $refcount;
}
可以看出,若返回值类型属于scalar范畴,即涵盖整型、浮点型及字符串等基础数据类型,此时refcount会被设定为0;反之则设为N。一旦存在显式注释配置,其优先级最高,将覆盖默认推断。
以函数ob_list_handlers作为具体案例进行分析:
/**
* @return array
* @refcount 1
*/
function ob_list_handlers(): array {}
该函数的预期返回类型为array,因此默认的refcount理应标记为N。然而,由于代码中明确添加了注释@refcount 1,导致最终返回值的引用计数被强制修正为1。
上述代码层面的逻辑尚属清晰,但关于为何要特意设置返回值引用计数这一初衷,目前仍感到困惑不解。
继续向下追踪排查发现,依据返回值的引用计数差异,在构建func_info对象时会产生不同的行为分支。若引用计数标识为1或N,系统将调用相应的宏来初始化func_info结构体;若为0,则跳过该初始化步骤。
前述的代码逻辑同样可以在gen_stub.php文件的第1393行处找到对应实现,具体涉及getOptimizerInfo方法。
public function getOptimizerInfo(): ?string {
if ($this->isMethod()) {
return null;
}
if ($this->alias !== null) {
return null;
}
if ($this->return->refcount !== ReturnInfo::REFCOUNT_1 && $this->return->phpDocType === null) {
return null;
}
$type = $this->return->phpDocType ?? $this->return->type;
if ($type === null) {
return null;
}
return "\tF" . $this->return->refcount . '("' . $this->name->__toString() . '", ' . $type->toOptimizerTypeMask() . "),\n";
}
此过程旨在获取函数原型的refcount信息,进而生成类似F1()或FN()形式的代码片段,最终生成的头文件存放于Zend/Optimizer/zend_func_infos.h目录之下。
static const func_info_t func_infos[] = {
F1("zend_version", MAY_BE_STRING),
FN("func_get_args", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),
F1("get_class_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
F1("get_class_methods", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_included_files", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
FN("set_error_handler", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT|MAY_BE_NULL),
FN("set_exception_handler", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_OBJECT|MAY_BE_NULL),
F1("get_declared_classes", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_declared_traits", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_declared_interfaces", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_defined_functions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),
F1("get_defined_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
F1("get_resource_type", MAY_BE_STRING),
F1("get_loaded_extensions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_defined_constants", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
F1("debug_backtrace", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
F1("get_extension_funcs", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
F1("gc_status", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_FALSE|MAY_BE_ARRAY_OF_TRUE),
F1("bcadd", MAY_BE_STRING),
F1("bcsub", MAY_BE_STRING),
F1("bcmul", MAY_BE_STRING),
F1("bcdiv", MAY_BE_STRING),
F1("bcmod", MAY_BE_STRING),
F1("bcpowmod", MAY_BE_STRING),
F1("bcpow", MAY_BE_STRING),
F1("bcsqrt", MAY_BE_STRING),
FN("bzopen", MAY_BE_RESOURCE|MAY_BE_FALSE),
F1("bzerror", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING),
F1("cal_from_jd", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL),
F1("cal_info", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY),
F1("curl_copy_handle", MAY_BE_OBJECT|MAY_BE_FALSE),
//...
};
随后进一步考察F1与FN这两个宏的具体定义细节。
typedef struct _func_info_t {
const char *name;
unsigned name_len;
uint32_t info;
info_func_t info_func;
} func_info_t;
#define F0(name, info) \
{name, sizeof(name)-1, (info), NULL}
#define F1(name, info) \
{name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}
#define FN(name, info) \
{name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}
#define FC(name, callback) \
{name, sizeof(name)-1, 0, callback}
二者主要区别在于设定的type mask不同。其中F1宏内部嵌入了MAY_BE_RC1标记,而FN宏则组合了MAY_BE_RCN | MAY_BE_RC1标记。
尽管理解上仍有模糊之处,但凭借目录命名线索,推测此举可能与性能优化及JIT技术相关。为此决定继续深挖,追踪这些初始化后的结构体在实际运行中的使用场景。
众所周知,利用|操作符进行位信息设置,而判断是否已设置则需借助&操作符。通过对全局代码搜索& MAY_BE_RCN并筛选与优化相关的部分,定位到了位于zend_jit.c文件第530行的关键代码段:
#ifdef ZEND_JIT_USE_RC_INFERENCE
/* Refcount may be increased by RETURN opcode */
if ((info & MAY_BE_RC1) && !(info & MAY_BE_RCN)) {
for (j = 0; j < ssa->cfg.blocks_count; j++) {
if ((ssa->cfg.blocks[j].flags & ZEND_BB_REACHABLE) &&
ssa->cfg.blocks[j].len > 0) {
const zend_op *opline = op_array->opcodes + ssa->cfg.blocks[j].start + ssa->cfg.blocks[j].len - 1;
if (opline->opcode == ZEND_RETURN) {
if (opline->op1_type == IS_CV && opline->op1.var == EX_NUM_TO_VAR(var)) {
info |= MAY_BE_RCN;
break;
}
}
}
}
}
#endif
仅当返回值引用计数标识为1而非N,且系统启用了返回值引用计数推导功能时,才会触发此段逻辑。此处引入了编译器设计中常见的SSA概念。
在编译器架构中,静态单一赋值形式(简称SSA)是中间表示层的重要属性,它规定每个变量仅允许分配一次,且在使用前必须完成定义。原始IR中的变量会被拆解为多个版本,通常以下标区分,确保每次定义都拥有独立版本。在SSA框架下,use-def链变得显式化,每个节点仅包含一个元素。
因此,前述代码的核心意图在于校验SSA控制流图(CFG)中的块是否具有可达性。若确认可达,则执行条件分支内的优化代码。
整体思路虽已理顺,明白设置refcount旨在辅助优化及配合SSA机制,但在实际开发扩展时,何时应当采用@refcount 1标注仍缺乏明确指导。
总结
至此,关于PHP源码中refcount疑问的探讨告一段落。如需了解更多php源码中refcount相关内容,建议检索本站历史文章或继续阅读后续系列内容,期待未来能得到大家的支持!